From 930ef87ff809ed019a5fbd0ebc810f6e8f0317e8 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Thu, 21 Sep 2017 14:28:12 +0800 Subject: [PATCH] MDL-60167 upgrade: Fix duplicate site registrations --- lib/db/upgrade.php | 21 ++++++++++++++++++++- version.php | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index f09a596cc38..73c84eac149 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2576,7 +2576,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. @@ -2612,5 +2617,19 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2016120505.01); } + if ($oldversion < 2016120505.04) { + + // 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, 2016120505.04); + } + return true; } diff --git a/version.php b/version.php index 696f2c5f5f2..8979fc49310 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2016120505.03; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2016120505.04; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.