Commit 6192e19f authored by webchick's avatar webchick
Browse files

#720620 by andypost: Fixed indexExists() for pgsql and sqlite does not prefix tablename.

parent fa02c4f4
......@@ -378,7 +378,7 @@ public function fieldSetNoDefault($table, $field) {
}
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();
}
......
......@@ -135,9 +135,12 @@ protected function processField($field) {
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 @@ protected function createFieldSql($name, $spec) {
$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 @@ public function getFieldTypeMap() {
* 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 @@ protected function alterTable($table, $new_schema) {
$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 @@ protected function introspectSchema($table) {
);
}
}
$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 @@ public function addIndex($table, $name, $fields) {
}
public function indexExists($table, $name) {
return ($this->connection->query("PRAGMA index_info($name)")->fetchField() != '');
return ($this->connection->query('PRAGMA index_info({' . $table . '}_' . $name . ')')->fetchField() != '');
}
/**
......
......@@ -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.'));
......@@ -129,17 +143,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.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment