MDL-65959 core_badges: fix unique index in upgrade.php
authorSara Arjona <sara@moodle.com>
Mon, 26 Oct 2020 22:32:10 +0000 (23:32 +0100)
committerSara Arjona <sara@moodle.com>
Mon, 26 Oct 2020 22:56:04 +0000 (23:56 +0100)
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

index 424abe2..9ff08c3 100644 (file)
@@ -2878,36 +2878,38 @@ function xmldb_main_upgrade($oldversion) {
 
     if ($oldversion < 2021052500.33) {
         $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);
         }