Commit 8b0fc579 authored by Dries's avatar Dries
Browse files

- Patch #360854 by sammys, Crell: added missing db_index_exists(), required to...

- Patch #360854 by sammys, Crell: added missing db_index_exists(), required to provide proper upgrade path from Drupal 6 to Drupal 7.
parent c3080a04
......@@ -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.
*/
......
......@@ -269,11 +269,11 @@ protected function createKeysSqlHelper($fields) {
}
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 @@ public function addField($table, $field, $spec, $keys_new = array()) {
}
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 @@ public function fieldSetDefault($table, $field, $default) {
}
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()) {
......
......@@ -275,11 +275,11 @@ protected function _createKeySql($fields) {
}
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 @@ public function addField($table, $field, $spec, $new_keys = array()) {
->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 @@ public function addField($table, $field, $spec, $new_keys = array()) {
* 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 @@ public function fieldSetDefault($table, $field, $default) {
* 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 @@ public function fieldSetNoDefault($table, $field) {
* 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 @@ public function addPrimaryKey($table, $fields) {
* 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 @@ public function dropUniqueKey($table, $name) {
* 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 @@ public function changeField($table, $field, $field_new, $spec, $new_keys = array
$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');
......
......@@ -337,6 +337,18 @@ abstract public function fieldSetDefault($table, $field, $default);
*/
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.
*
......
......@@ -220,7 +220,7 @@ public function getFieldTypeMap() {
* 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 @@ public function renameTable($table, $new_name) {
* 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 @@ public function addIndex($table, $name, $fields) {
$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 @@ public function addIndex($table, $name, $fields) {
* 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 @@ public function addUniqueKey($table, $name, $fields) {
$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 @@ public function addUniqueKey($table, $name, $fields) {
* The name of the key.
*/
public function dropUniqueKey($table, $name) {
$this->connection->query('DROP INDEX ' . '{' . $table . '}_' . $name);
$this->connection->query('DROP INDEX ' . '{' . $table . '}_' . $name);
}
/**
......
......@@ -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'));
}
}
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