summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/database/pgsql/schema.inc2
-rw-r--r--includes/database/sqlite/schema.inc37
-rw-r--r--modules/simpletest/tests/schema.test25
3 files changed, 48 insertions, 16 deletions
diff --git a/includes/database/pgsql/schema.inc b/includes/database/pgsql/schema.inc
index 53392c170..f36ae2c32 100644
--- a/includes/database/pgsql/schema.inc
+++ b/includes/database/pgsql/schema.inc
@@ -378,7 +378,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
}
public function indexExists($table, $name) {
- $index_name = $table . '_' . $name . '_idx';
+ $index_name = '{' . $table . '}_' . $name . '_idx';
return $this->connection->query("SELECT COUNT(indexname) FROM pg_indexes WHERE indexname = '$index_name'")->fetchField();
}
diff --git a/includes/database/sqlite/schema.inc b/includes/database/sqlite/schema.inc
index ff3f35c68..974d014d7 100644
--- a/includes/database/sqlite/schema.inc
+++ b/includes/database/sqlite/schema.inc
@@ -135,9 +135,12 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
protected function createFieldSql($name, $spec) {
if (!empty($spec['auto_increment'])) {
$sql = $name . " INTEGER PRIMARY KEY AUTOINCREMENT";
+ if (!empty($spec['unsigned'])) {
+ $sql .= ' CHECK (' . $name . '>= 0)';
+ }
}
else {
- $sql = $name . " " . $spec['sqlite_type'];
+ $sql = $name . ' ' . $spec['sqlite_type'];
if (in_array($spec['type'], array('varchar', 'char', 'text')) && isset($spec['length'])) {
$sql .= '(' . $spec['length'] . ')';
@@ -147,6 +150,10 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
$sql .= ' NOT NULL';
}
+ if (!empty($spec['unsigned'])) {
+ $sql .= ' CHECK (' . $name . '>= 0)';
+ }
+
if (isset($spec['default'])) {
if (is_string($spec['default'])) {
$spec['default'] = "'" . $spec['default'] . "'";
@@ -220,7 +227,27 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* The new name for the table.
*/
public function renameTable($table, $new_name) {
+ $schema = $this->introspectSchema($table);
+
$this->connection->query('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}');
+
+ // Drop the indexes, there is no RENAME INDEX command in SQLite.
+ if (!empty($schema['unique keys'])) {
+ foreach ($schema['unique keys'] as $key => $fields) {
+ $this->dropIndex($table, $key);
+ }
+ }
+ if (!empty($schema['indexes'])) {
+ foreach ($schema['indexes'] as $index => $fields) {
+ $this->dropIndex($table, $index);
+ }
+ }
+
+ // Recreate the indexes.
+ $statements = $this->createIndexSql($new_name, $schema);
+ foreach ($statements as $statement) {
+ $this->connection->query($statement);
+ }
}
/**
@@ -269,7 +296,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
$this->createTable($new_table, $new_schema);
- $select = $this->connection->select($table)->fields($new_schema['fields']);
+ $select = $this->connection->select($table)->fields($table, array_keys($new_schema['fields']));
$this->connection->insert($new_table)
->from($select)
->execute();
@@ -340,9 +367,11 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
);
}
}
- $n = strlen($table) + 1;
+ // Get length of table name and underscore.
+ $n = strlen($this->connection->prefixTables('{' . $table . '}')) + 1;
foreach ($indexes as $index) {
$name = $index['name'];
+ // Get index name without prefix.
$index_name = substr($name, $n);
$result = $this->connection->query("PRAGMA index_info('$name')");
foreach ($result as $row) {
@@ -434,7 +463,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
}
public function indexExists($table, $name) {
- return ($this->connection->query("PRAGMA index_info($name)")->fetchField() != '');
+ return ($this->connection->query('PRAGMA index_info({' . $table . '}_' . $name . ')')->fetchField() != '');
}
/**
diff --git a/modules/simpletest/tests/schema.test b/modules/simpletest/tests/schema.test
index 1cf48e933..7f7571ef9 100644
--- a/modules/simpletest/tests/schema.test
+++ b/modules/simpletest/tests/schema.test
@@ -61,8 +61,22 @@ class SchemaTestCase extends DrupalWebTestCase {
// The insert should fail again.
$this->assertFalse($this->tryInsert(), t('Insert without a default failed.'));
+ // Test for fake index.
+ $index_exists = Database::getConnection()->schema()->indexExists('test_table', 'test_field');
+ $this->assertFalse($index_exists, t('Fake index does not exists'));
+ // Add index.
+ db_add_index('test_table', 'test_field', array('test_field'));
+ // Test for created index.
+ $index_exists = Database::getConnection()->schema()->indexExists('test_table', 'test_field');
+ $this->assertTrue($index_exists, t('Index created.'));
+
// Rename the table.
db_rename_table('test_table', 'test_table2');
+
+ // Index should be renamed.
+ $index_exists = Database::getConnection()->schema()->indexExists('test_table2', 'test_field');
+ $this->assertTrue($index_exists, t('Index was renamed.'));
+
// We need the default so that we can insert after the rename.
db_field_set_default('test_table2', 'test_field', 0);
$this->assertFalse($this->tryInsert(), t('Insert into the old table failed.'));
@@ -130,17 +144,6 @@ class SchemaTestCase extends DrupalWebTestCase {
}
/**
- * Test index status.
- */
- function testCheckIndex() {
- $node_changed_index = Database::getConnection()->schema()->indexExists('node', 'node_changed');
- $this->assertTrue($node_changed_index, t('Node index exists'));
-
- $node_fake_index = Database::getConnection()->schema()->indexExists('node', 'node_not_exists');
- $this->assertFalse($node_fake_index, t('Fake index does not exists'));
- }
-
- /**
* Tests creating unsigned columns and data integrity thereof.
*/
function testUnsignedColumns() {