- Patch #360854 by sammys, Crell: added missing db_index_exists(), required to provide proper upgrade path from Drupal 6 to Drupal 7.

merge-requests/26/head
Dries Buytaert 2009-12-15 08:30:53 +00:00
parent c3080a0401
commit 8b0fc57911
6 changed files with 73 additions and 26 deletions

View File

@ -2372,6 +2372,18 @@ function db_field_names($fields) {
return Database::getConnection()->schema()->fieldNames($fields);
}
/**
* Check if an index exists.
*
* @param $name
* Index name.
* @return
* TRUE if the given index exists, otherwise FALSE.
*/
function db_index_exists($name) {
return Database::getConnection()->schema()->indexExists($name);
}
/**
* Check if a table exists.
*/

View File

@ -269,11 +269,11 @@ class DatabaseSchema_mysql extends DatabaseSchema {
}
public function renameTable($table, $new_name) {
$this->connection->query('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}');
$this->connection->query('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}');
}
public function dropTable($table) {
$this->connection->query('DROP TABLE {' . $table . '}');
$this->connection->query('DROP TABLE {' . $table . '}');
}
public function addField($table, $field, $spec, $keys_new = array()) {
@ -300,7 +300,7 @@ class DatabaseSchema_mysql extends DatabaseSchema {
}
public function dropField($table, $field) {
$this->connection->query('ALTER TABLE {' . $table . '} DROP `' . $field . '`');
$this->connection->query('ALTER TABLE {' . $table . '} DROP `' . $field . '`');
}
public function fieldSetDefault($table, $field, $default) {
@ -315,31 +315,35 @@ class DatabaseSchema_mysql extends DatabaseSchema {
}
public function fieldSetNoDefault($table, $field) {
$this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN `' . $field . '` DROP DEFAULT');
$this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN `' . $field . '` DROP DEFAULT');
}
public function indexExists($table, $name) {
return $this->connection->query('SHOW INDEX FROM {' . $table . "} WHERE key_name = '$name'")->fetchField();
}
public function addPrimaryKey($table, $fields) {
$this->connection->query('ALTER TABLE {' . $table . '} ADD PRIMARY KEY (' . $this->createKeySql($fields) . ')');
$this->connection->query('ALTER TABLE {' . $table . '} ADD PRIMARY KEY (' . $this->createKeySql($fields) . ')');
}
public function dropPrimaryKey($table) {
$this->connection->query('ALTER TABLE {' . $table . '} DROP PRIMARY KEY');
$this->connection->query('ALTER TABLE {' . $table . '} DROP PRIMARY KEY');
}
public function addUniqueKey($table, $name, $fields) {
$this->connection->query('ALTER TABLE {' . $table . '} ADD UNIQUE KEY `' . $name . '` (' . $this->createKeySql($fields) . ')');
$this->connection->query('ALTER TABLE {' . $table . '} ADD UNIQUE KEY `' . $name . '` (' . $this->createKeySql($fields) . ')');
}
public function dropUniqueKey($table, $name) {
$this->connection->query('ALTER TABLE {' . $table . '} DROP KEY `' . $name . '`');
$this->connection->query('ALTER TABLE {' . $table . '} DROP KEY `' . $name . '`');
}
public function addIndex($table, $name, $fields) {
$this->connection->query('ALTER TABLE {' . $table . '} ADD INDEX `' . $name . '` (' . $this->createKeySql($fields) . ')');
$this->connection->query('ALTER TABLE {' . $table . '} ADD INDEX `' . $name . '` (' . $this->createKeySql($fields) . ')');
}
public function dropIndex($table, $name) {
$this->connection->query('ALTER TABLE {' . $table . '} DROP INDEX `' . $name . '`');
$this->connection->query('ALTER TABLE {' . $table . '} DROP INDEX `' . $name . '`');
}
public function changeField($table, $field, $field_new, $spec, $keys_new = array()) {

View File

@ -275,11 +275,11 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
}
function renameTable($table, $new_name) {
$this->connection->query('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}');
$this->connection->query('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}');
}
public function dropTable($table) {
$this->connection->query('DROP TABLE {' . $table . '}');
$this->connection->query('DROP TABLE {' . $table . '}');
}
/**
@ -320,14 +320,14 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
->execute();
}
if ($fixnull) {
$this->connection->query("ALTER TABLE {" . $table . "} ALTER $field SET NOT NULL");
$this->connection->query("ALTER TABLE {" . $table . "} ALTER $field SET NOT NULL");
}
if (isset($new_keys)) {
$this->_createKeys($table, $new_keys);
}
// Add column comment.
if (!empty($spec['description'])) {
$this->connection->query('COMMENT ON COLUMN {' . $table . '}.' . $field . ' IS ' . $this->prepareComment($spec['description']));
$this->connection->query('COMMENT ON COLUMN {' . $table . '}.' . $field . ' IS ' . $this->prepareComment($spec['description']));
}
}
@ -340,7 +340,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
* The field to be dropped.
*/
public function dropField($table, $field) {
$this->connection->query('ALTER TABLE {' . $table . '} DROP COLUMN "' . $field . '"');
$this->connection->query('ALTER TABLE {' . $table . '} DROP COLUMN "' . $field . '"');
}
/**
@ -373,7 +373,11 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
* The field to be altered.
*/
public function fieldSetNoDefault($table, $field) {
$this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field . '" DROP DEFAULT');
$this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field . '" DROP DEFAULT');
}
public function indexExists($table, $name) {
return $this->connection->query("SELECT COUNT(indexname) FROM pg_indexes WHERE indexname = '$name'")->fetchField();
}
/**
@ -385,7 +389,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
* Fields for the primary key.
*/
public function addPrimaryKey($table, $fields) {
$this->connection->query('ALTER TABLE {' . $table . '} ADD PRIMARY KEY (' . implode(',', $fields) . ')');
$this->connection->query('ALTER TABLE {' . $table . '} ADD PRIMARY KEY (' . implode(',', $fields) . ')');
}
/**
@ -395,7 +399,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
* The table to be altered.
*/
public function dropPrimaryKey($table) {
$this->connection->query('ALTER TABLE {' . $table . '} DROP CONSTRAINT {' . $table . '}_pkey');
$this->connection->query('ALTER TABLE {' . $table . '} DROP CONSTRAINT {' . $table . '}_pkey');
}
/**
@ -437,7 +441,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
* An array of field names.
*/
public function addIndex($table, $name, $fields) {
$this->connection->query($this->_createIndexSql($table, $name, $fields));
$this->connection->query($this->_createIndexSql($table, $name, $fields));
}
/**
@ -534,7 +538,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema {
$this->connection->query("UPDATE {" . $table . "} SET $field_new = CAST(" . $field . "_old as " . $typecast . ")");
if ($not_null) {
$this->connection->query("ALTER TABLE {" . $table . "} ALTER $field_new SET NOT NULL");
$this->connection->query("ALTER TABLE {" . $table . "} ALTER $field_new SET NOT NULL");
}
$this->dropField($table, $field . '_old');

View File

@ -337,6 +337,18 @@ abstract class DatabaseSchema implements QueryPlaceholderInterface {
*/
abstract public function fieldSetNoDefault($table, $field);
/**
* Checks if an index exists.
*
* @param $table
* Name of the table.
* @param $name
* Name of the index.
* @return
* Index name if the table exists. Otherwise FALSE.
*/
abstract public function indexExists($table, $name);
/**
* Add a primary key.
*

View File

@ -220,7 +220,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* The new name for the table.
*/
public function renameTable($table, $new_name) {
$this->connection->query('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}');
$this->connection->query('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}');
}
/**
@ -230,7 +230,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* The table to be dropped.
*/
public function dropTable($table) {
$this->connection->query('DROP TABLE {' . $table . '}');
$this->connection->query('DROP TABLE {' . $table . '}');
}
/**
@ -429,10 +429,14 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
$schema['indexes'][$name] = $fields;
$statements = $this->createIndexSql($table, $schema);
foreach ($statements as $statement) {
$this->connection->query($statement);
$this->connection->query($statement);
}
}
public function indexExists($table, $name) {
return ($this->connection->query("PRAGMA index_info($name)")->fetchField() != '');
}
/**
* Drop an index.
*
@ -442,7 +446,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* The name of the index.
*/
public function dropIndex($table, $name) {
$this->connection->query('DROP INDEX ' . '{' . $table . '}_' . $name);
$this->connection->query('DROP INDEX ' . '{' . $table . '}_' . $name);
}
/**
@ -459,7 +463,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
$schema['unique keys'][$name] = $fields;
$statements = $this->createIndexSql($table, $schema);
foreach ($statements as $statement) {
$this->connection->query($statement);
$this->connection->query($statement);
}
}
@ -472,7 +476,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema {
* The name of the key.
*/
public function dropUniqueKey($table, $name) {
$this->connection->query('DROP INDEX ' . '{' . $table . '}_' . $name);
$this->connection->query('DROP INDEX ' . '{' . $table . '}_' . $name);
}
/**

View File

@ -128,4 +128,15 @@ class SchemaTestCase extends DrupalWebTestCase {
$this->assertEqual($comment, $description, t('The comment matches the schema description.'));
}
}
/**
* 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'));
}
}