}
public function testDropIndex() {
+ $DB = $this->tdb; // do not use global $DB!
+
$dbman = $this->tdb->get_manager();
$table = $this->create_deftable('test_table1');
$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() {
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);
/// 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;
$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);
}
} 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;