MDL-60167 upgrade: Fix duplicate site registrations
authorMarina Glancy <marina@moodle.com>
Thu, 21 Sep 2017 06:28:12 +0000 (14:28 +0800)
committerMarina Glancy <marina@moodle.com>
Sun, 24 Sep 2017 10:46:41 +0000 (18:46 +0800)
lib/db/upgrade.php
version.php

index df03600..194277c 100644 (file)
@@ -2245,7 +2245,12 @@ function xmldb_main_upgrade($oldversion) {
             ['hub', '%' . $DB->sql_like_escape('_' . $cleanoldhuburl)]);
         foreach ($entries as $entry) {
             $newname = substr($entry->name, 0, -strlen($cleanoldhuburl)) . $cleannewhuburl;
-            $DB->update_record('config_plugins', ['id' => $entry->id, 'name' => $newname]);
+            try {
+                $DB->update_record('config_plugins', ['id' => $entry->id, 'name' => $newname]);
+            } catch (dml_exception $e) {
+                // Entry with new name already exists, remove the one with an old name.
+                $DB->delete_records('config_plugins', ['id' => $entry->id]);
+            }
         }
 
         // Update published courses.
@@ -2494,5 +2499,19 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2017091201.00);
     }
 
+    if ($oldversion < 2017092201.00) {
+
+        // Remove duplicate registrations.
+        $newhuburl = "https://moodle.net";
+        $registrations = $DB->get_records('registration_hubs', ['huburl' => $newhuburl], 'confirmed DESC, id ASC');
+        if (count($registrations) > 1) {
+            $reg = array_shift($registrations);
+            $DB->delete_records_select('registration_hubs', 'huburl = ? AND id <> ?', [$newhuburl, $reg->id]);
+        }
+
+        // Main savepoint reached.
+        upgrade_main_savepoint(true, 2017092201.00);
+    }
+
     return true;
 }
index 0e612ed..8b87b99 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2017092200.00;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2017092201.00;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.