From 8b0fc579113af6be4357f881b2d12cd885af3d46 Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Tue, 15 Dec 2009 08:30:53 +0000
Subject: [PATCH] - Patch #360854 by sammys, Crell: added missing
 db_index_exists(), required to provide proper upgrade path from Drupal 6 to
 Drupal 7.

---
 includes/database/database.inc       | 12 ++++++++++++
 includes/database/mysql/schema.inc   | 24 ++++++++++++++----------
 includes/database/pgsql/schema.inc   | 24 ++++++++++++++----------
 includes/database/schema.inc         | 12 ++++++++++++
 includes/database/sqlite/schema.inc  | 16 ++++++++++------
 modules/simpletest/tests/schema.test | 11 +++++++++++
 6 files changed, 73 insertions(+), 26 deletions(-)

diff --git a/includes/database/database.inc b/includes/database/database.inc
index fb324eded31c..e972258ed269 100644
--- a/includes/database/database.inc
+++ b/includes/database/database.inc
@@ -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.
  */
diff --git a/includes/database/mysql/schema.inc b/includes/database/mysql/schema.inc
index 4fa20abf8ff8..b87bb16d306f 100644
--- a/includes/database/mysql/schema.inc
+++ b/includes/database/mysql/schema.inc
@@ -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()) {
diff --git a/includes/database/pgsql/schema.inc b/includes/database/pgsql/schema.inc
index e2aefe10812a..578c074bb89c 100644
--- a/includes/database/pgsql/schema.inc
+++ b/includes/database/pgsql/schema.inc
@@ -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');
diff --git a/includes/database/schema.inc b/includes/database/schema.inc
index 61f698685879..af7ed4dbea87 100644
--- a/includes/database/schema.inc
+++ b/includes/database/schema.inc
@@ -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.
    *
diff --git a/includes/database/sqlite/schema.inc b/includes/database/sqlite/schema.inc
index 893f54be7e8d..ff3f35c6857b 100644
--- a/includes/database/sqlite/schema.inc
+++ b/includes/database/sqlite/schema.inc
@@ -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);
   }
 
   /**
diff --git a/modules/simpletest/tests/schema.test b/modules/simpletest/tests/schema.test
index da48b9395b37..637bc0b6fcfe 100644
--- a/modules/simpletest/tests/schema.test
+++ b/modules/simpletest/tests/schema.test
@@ -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'));
+  }
 }
-- 
GitLab