Skip to content

Commit

Permalink
Merge branch 'w16_MDL-38972_m25_oraindex' of git://github.com/skodak/…
Browse files Browse the repository at this point in the history
…moodle
  • Loading branch information
stronk7 committed Apr 24, 2013
2 parents bdeda33 + 2d97513 commit 01159be
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lib/ddl/tests/ddl_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,51 @@ public function testAddNonUniqueIndex() {
$index->set_attributes(XMLDB_INDEX_NOTUNIQUE, array('course', 'name'));
$dbman->add_index($table, $index);
$this->assertTrue($dbman->index_exists($table, $index));

try {
$dbman->add_index($table, $index);
$this->fail('Exception expected for duplicate indexes');
} catch (Exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
}

$index = new xmldb_index('third');
$index->set_attributes(XMLDB_INDEX_NOTUNIQUE, array('course'));
try {
$dbman->add_index($table, $index);
$this->fail('Exception expected for duplicate indexes');
} catch (Exception $e) {
$this->assertInstanceOf('ddl_exception', $e);
}

$table = new xmldb_table('test_table_cust0');
$table->add_field('id', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('onenumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('name', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'Moodle');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('onenumber', XMLDB_KEY_FOREIGN, array('onenumber'));

try {
$table->add_index('onenumber', XMLDB_INDEX_NOTUNIQUE, array('onenumber'));
$this->fail('Coding exception expected');
} catch (Exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}

$table = new xmldb_table('test_table_cust0');
$table->add_field('id', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('onenumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('name', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'Moodle');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_index('onenumber', XMLDB_INDEX_NOTUNIQUE, array('onenumber'));

try {
$table->add_key('onenumber', XMLDB_KEY_FOREIGN, array('onenumber'));
$this->fail('Coding exception expected');
} catch (Exception $e) {
$this->assertInstanceOf('coding_exception', $e);
}

}

public function testFindIndexName() {
Expand Down
2 changes: 2 additions & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ information provided here is intended especially for developers.
Database (DML) layer:
* $DB->sql_empty() is deprecated, you have to use sql parameters with empty values instead,
please note hardcoding of empty strings in SQL queries breaks execution in Oracle database.
* Indexes must not be defined on the same columns as keys, this is now reported as fatal problem.
Please note that internally we create indexes instead of foreign keys.

YUI changes:
* M.util.help_icon has been deprecated. Code should be updated to use moodle-core-popuphelp
Expand Down
20 changes: 20 additions & 0 deletions lib/xmldb/xmldb_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ public function addKey($key, $after=null) {
throw new coding_exception('Duplicate key '.$key->getName().' specified in table '.$this->getName());
}

// Make sure there are no duplicate keys because the indexes would collide.
$newfields = $key->getFields();
$allindexes = $this->getIndexes();
foreach ($allindexes as $index) {
$fields = $index->getFields();
if ($fields === $newfields) {
throw new coding_exception('Index '.$index->getName().' collides with key'.$key->getName().' specified in table '.$this->getName());
}
}

// Calculate the previous and next keys
$prevkey = null;
$nextkey = null;
Expand Down Expand Up @@ -177,6 +187,16 @@ public function addIndex($index, $after=null) {
throw new coding_exception('Duplicate index '.$index->getName().' specified in table '.$this->getName());
}

// Make sure there are no duplicate keys because the indexes would collide.
$newfields = $index->getFields();
$allkeys = $this->getKeys();
foreach ($allkeys as $key) {
$fields = $key->getFields();
if ($fields === $newfields) {
throw new coding_exception('Key '.$key->getName().' collides with index'.$index->getName().' specified in table '.$this->getName());
}
}

// Calculate the previous and next indexes
$previndex = null;
$nextindex = null;
Expand Down

0 comments on commit 01159be

Please sign in to comment.