Skip to content

Commit

Permalink
MDL-44658 Make userid,fieldid unique in user_info_data
Browse files Browse the repository at this point in the history
  • Loading branch information
danielneis committed Apr 15, 2014
1 parent 1131095 commit 3703256
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="userfieldidx" UNIQUE="false" FIELDS="userid, fieldid"/>
<INDEX NAME="userfieldidx" UNIQUE="true" FIELDS="userid, fieldid"/>
</INDEXES>
</TABLE>
<TABLE NAME="question_categories" COMMENT="Categories are for grouping questions">
Expand Down
32 changes: 32 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3592,5 +3592,37 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2014040800.00);
}

if ($oldversion < 2014041500.01) {

$table = new xmldb_table('user_info_data');

$sql = 'SELECT DISTINCT info.id
FROM {user_info_data} info
INNER JOIN {user_info_data} older
ON info.fieldid = older.fieldid
AND info.userid = older.userid
AND older.id < info.id';
$transaction = $DB->start_delegated_transaction();
$rs = $DB->get_recordset_sql($sql);
foreach ($rs as $rec) {
$DB->delete_records('user_info_data', array('id' => $rec->id));
}
$transaction->allow_commit();

$oldindex = new xmldb_index('userid_fieldid', XMLDB_INDEX_NOTUNIQUE, array('userid', 'fieldid'));
if ($dbman->index_exists($table, $oldindex)) {
$dbman->drop_index($table, $oldindex);
}

$newindex = new xmldb_index('userid_fieldid', XMLDB_INDEX_UNIQUE, array('userid', 'fieldid'));

if (!$dbman->index_exists($table, $newindex)) {
$dbman->add_index($table, $newindex);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2014041500.01);
}

return true;
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2014041500.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2014041500.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down

0 comments on commit 3703256

Please sign in to comment.