MDL-22804 hyphens in index names - Now the big-four handle/quote them properly
authorEloy Lafuente <stronk7@moodle.org>
Wed, 16 Jun 2010 16:15:23 +0000 (16:15 +0000)
committerEloy Lafuente <stronk7@moodle.org>
Wed, 16 Jun 2010 16:15:23 +0000 (16:15 +0000)
lib/ddl/simpletest/testddl.php
lib/ddl/sql_generator.php

index ae9d6da..efb6c9a 100755 (executable)
@@ -1025,6 +1025,8 @@ class ddl_test extends UnitTestCase {
     }
 
     public function testDropIndex() {
+        $DB = $this->tdb; // do not use global $DB!
+
         $dbman = $this->tdb->get_manager();
 
         $table = $this->create_deftable('test_table1');
@@ -1034,6 +1036,24 @@ class ddl_test extends UnitTestCase {
 
         $dbman->drop_index($table, $index);
         $this->assertFalse($dbman->find_index_name($table, $index));
+
+        // Test we are able to drop indexes having hyphens. MDL-22804
+        // Create index with hyphens (by hand)
+        $indexname = 'test-index-with-hyphens';
+        switch ($DB->get_dbfamily()) {
+            case 'mysql':
+                $indexname = '`' . $indexname . '`';
+                break;
+            default:
+                $indexname = '"' . $indexname . '"';
+        }
+        $stmt = "CREATE INDEX {$indexname} ON {$DB->get_prefix()}test_table1 (course, name)";
+        $DB->change_database_structure($stmt);
+        $this->assertTrue($dbman->find_index_name($table, $index));
+        // Index created, let's drop it using db manager stuff
+        $index = new xmldb_index('indexname', XMLDB_INDEX_NOTUNIQUE, array('course', 'name'));
+        $dbman->drop_index($table, $index);
+        $this->assertFalse($dbman->find_index_name($table, $index));
     }
 
     public function testAddUniqueKey() {
@@ -1140,8 +1160,8 @@ class ddl_test extends UnitTestCase {
         switch ($DB->get_dbfamily()) {
             case 'mysql': // It's ENUM field for mysql
                 $stmt = "ALTER TABLE {$DB->get_prefix()}test_table_cust0 MODIFY type ENUM ('general', 'qanda', 'moodle') NOT NULL DEFAULT 'general'";
-                    break;
-                default: // It's check constraint for "normal" DBs
+                break;
+            default: // It's check constraint for "normal" DBs
                 $stmt = "ALTER TABLE {$DB->get_prefix()}test_table_cust0 ADD CONSTRAINT ttcu0_ck CHECK (type IN ('general', 'qanda', 'moodle'))";
         }
         $DB->change_database_structure($stmt);
index b1ae422..1957dbe 100644 (file)
@@ -887,7 +887,7 @@ abstract class sql_generator {
 
     /// Replace TABLENAME and INDEXNAME as needed
         $dropsql = str_replace('TABLENAME', $this->getTableName($xmldb_table), $this->drop_index_sql);
-        $dropsql = str_replace('INDEXNAME', $dbindexname, $dropsql);
+        $dropsql = str_replace('INDEXNAME', $this->getEncQuoted($dbindexname), $dropsql);
 
         $results[] = $dropsql;
 
@@ -908,8 +908,8 @@ abstract class sql_generator {
         $dbindexname = $this->mdb->get_manager()->find_index_name($xmldb_table, $xmldb_index);
     /// Replace TABLENAME and INDEXNAME as needed
         $renamesql = str_replace('TABLENAME', $this->getTableName($xmldb_table), $this->rename_index_sql);
-        $renamesql = str_replace('OLDINDEXNAME', $dbindexname, $renamesql);
-        $renamesql = str_replace('NEWINDEXNAME', $newname, $renamesql);
+        $renamesql = str_replace('OLDINDEXNAME', $this->getEncQuoted($dbindexname), $renamesql);
+        $renamesql = str_replace('NEWINDEXNAME', $this->getEncQuoted($newname), $renamesql);
 
         return array($renamesql);
     }
@@ -1004,8 +1004,8 @@ abstract class sql_generator {
         } else {
         /// Always lowercase
             $input = strtolower($input);
-        /// if reserved or quote_all, quote it
-            if ($this->quote_all || in_array($input, $this->reserved_words)) {
+        /// if reserved or quote_all or has hyphens, quote it
+            if ($this->quote_all || in_array($input, $this->reserved_words) || strpos($input, '-') !== false) {
                 $input = $this->quote_string . $input . $this->quote_string;
             }
             return $input;