Skip to content

Commit

Permalink
MDL-33018 support varchar_pattern_ops on unique indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jul 6, 2012
1 parent 661dd35 commit 068cf0e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/ddl/postgres_sql_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ public function getCreateIndexSQL($xmldb_table, $xmldb_index) {
$hints = $xmldb_index->getHints();
$fields = $xmldb_index->getFields();
if (in_array('varchar_pattern_ops', $hints) and count($fields) == 1) {
// Add the pattern index and keep the normal one.
// Add the pattern index and keep the normal one, keep unique only the standard index to improve perf.
foreach ($sqls as $sql) {
$field = reset($fields);
$count = 0;
$newindex = preg_replace("/^CREATE INDEX ([a-z0-9_]+) ON ([a-z0-9_]+) \($field\)$/", "CREATE INDEX \\1_pattern ON \\2 USING btree ($field varchar_pattern_ops)", $sql, -1, $count);
$newindex = preg_replace("/^CREATE( UNIQUE)? INDEX ([a-z0-9_]+) ON ([a-z0-9_]+) \($field\)$/", "CREATE INDEX \\2_pattern ON \\3 USING btree ($field varchar_pattern_ops)", $sql, -1, $count);
if ($count != 1) {
debugging('Unexpected getCreateIndexSQL() structure.');
continue;
Expand Down
15 changes: 15 additions & 0 deletions lib/ddl/tests/ddl_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,21 @@ public function test_index_hints() {
$table = new xmldb_table('testtable');
$index = new xmldb_index('path', XMLDB_INDEX_NOTUNIQUE, array('path'), array('varchar_pattern_ops'));
$this->assertTrue($dbman->index_exists($table, $index));

// Try unique indexes too.
$dbman->drop_table($this->tables[$tablename]);

$table = new xmldb_table('testtable');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('path', XMLDB_TYPE_CHAR, 255, null, XMLDB_NOTNULL, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_index('path', XMLDB_INDEX_UNIQUE, array('path'), array('varchar_pattern_ops'));
$dbman->create_table($table);
$this->tables[$tablename] = $table;

$table = new xmldb_table('testtable');
$index = new xmldb_index('path', XMLDB_INDEX_UNIQUE, array('path'), array('varchar_pattern_ops'));
$this->assertTrue($dbman->index_exists($table, $index));
}

public function test_index_max_bytes() {
Expand Down

0 comments on commit 068cf0e

Please sign in to comment.