Skip to content
Snippets Groups Projects
Commit 8b0fc579 authored by Dries Buytaert's avatar Dries Buytaert
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
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -2372,6 +2372,18 @@ function db_field_names($fields) { ...@@ -2372,6 +2372,18 @@ function db_field_names($fields) {
return Database::getConnection()->schema()->fieldNames($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. * Check if a table exists.
*/ */
......
...@@ -318,6 +318,10 @@ public function fieldSetNoDefault($table, $field) { ...@@ -318,6 +318,10 @@ 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) { 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) . ')');
} }
......
...@@ -376,6 +376,10 @@ public function fieldSetNoDefault($table, $field) { ...@@ -376,6 +376,10 @@ 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();
}
/** /**
* Add a primary key. * Add a primary key.
* *
......
...@@ -337,6 +337,18 @@ abstract public function fieldSetDefault($table, $field, $default); ...@@ -337,6 +337,18 @@ abstract public function fieldSetDefault($table, $field, $default);
*/ */
abstract public function fieldSetNoDefault($table, $field); 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. * Add a primary key.
* *
......
...@@ -433,6 +433,10 @@ public function addIndex($table, $name, $fields) { ...@@ -433,6 +433,10 @@ public function addIndex($table, $name, $fields) {
} }
} }
public function indexExists($table, $name) {
return ($this->connection->query("PRAGMA index_info($name)")->fetchField() != '');
}
/** /**
* Drop an index. * Drop an index.
* *
......
...@@ -128,4 +128,15 @@ class SchemaTestCase extends DrupalWebTestCase { ...@@ -128,4 +128,15 @@ class SchemaTestCase extends DrupalWebTestCase {
$this->assertEqual($comment, $description, t('The comment matches the schema description.')); $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'));
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment