From 0459910b53ea4db22e3335f73c9102e384508046 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Mon, 26 Oct 2020 23:32:10 +0100 Subject: [PATCH] MDL-65959 core_badges: fix unique index in upgrade.php There was an error when creating the unique index. Apart from that, the code has been reorganised to copy data in the new structure only if password field hasn't been removed. --- lib/db/upgrade.php | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 5237245212b..e9be5101e14 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2878,36 +2878,38 @@ function xmldb_main_upgrade($oldversion) { if ($oldversion < 2020102300.02) { $table = new xmldb_table('badge_backpack'); - $uniquekey = new xmldb_key('backpackcredentials', XMLDB_KEY_UNIQUE, ['userid', 'externalbackpackid']); - // All external backpack providers/hosts are now exclusively stored in badge_external_backpack. - // All credentials are stored in badge_backpack and are unique per user, backpack. - if (!$dbman->find_key_name($table, $uniquekey)) { + // There is no key_exists, so test the equivalent index. + $oldindex = new xmldb_index('backpackcredentials', XMLDB_KEY_UNIQUE, ['userid', 'externalbackpackid']); + if (!$dbman->index_exists($table, $oldindex)) { + // All external backpack providers/hosts are now exclusively stored in badge_external_backpack. + // All credentials are stored in badge_backpack and are unique per user, backpack. + $uniquekey = new xmldb_key('backpackcredentials', XMLDB_KEY_UNIQUE, ['userid', 'externalbackpackid']); $dbman->add_key($table, $uniquekey); } - // If there is a current backpack set then copy it across to the new structure. - if ($CFG->badges_defaultissuercontact) { - // Get the currently used site backpacks. - $records = $DB->get_records_select('badge_external_backpack', "password IS NOT NULL AND password != ''"); - $backpack = [ - 'userid' => '0', - 'email' => $CFG->badges_defaultissuercontact, - 'backpackuid' => -1 - ]; - - // Create records corresponding to the site backpacks. - foreach ($records as $record) { - $backpack['password'] = $record->password; - $backpack['externalbackpackid'] = $record->id; - $DB->insert_record('badge_backpack', (object) $backpack); - } - } - // Drop the password field as this is moved to badge_backpack. $table = new xmldb_table('badge_external_backpack'); $field = new xmldb_field('password', XMLDB_TYPE_CHAR, '50'); if ($dbman->field_exists($table, $field)) { + // If there is a current backpack set then copy it across to the new structure. + if ($CFG->badges_defaultissuercontact) { + // Get the currently used site backpacks. + $records = $DB->get_records_select('badge_external_backpack', "password IS NOT NULL AND password != ''"); + $backpack = [ + 'userid' => '0', + 'email' => $CFG->badges_defaultissuercontact, + 'backpackuid' => -1 + ]; + + // Create records corresponding to the site backpacks. + foreach ($records as $record) { + $backpack['password'] = $record->password; + $backpack['externalbackpackid'] = $record->id; + $DB->insert_record('badge_backpack', (object) $backpack); + } + } + $dbman->drop_field($table, $field); }