MDL-66551 core_user: remove orphaned calendar subscriptions
authorSimey Lameze <simey@moodle.com>
Thu, 29 Aug 2019 03:02:58 +0000 (11:02 +0800)
committerSimey Lameze <simey@moodle.com>
Thu, 5 Sep 2019 02:21:05 +0000 (10:21 +0800)
lib/db/upgrade.php
version.php

index b6c81d2..16f2a29 100644 (file)
@@ -3464,5 +3464,23 @@ function xmldb_main_upgrade($oldversion) {
         // Main savepoint reached.
         upgrade_main_savepoint(true, 2019073100.00);
     }
+
+    if ($oldversion < 2019083000.01) {
+        // Delete "orphaned" subscriptions.
+        $sql = "SELECT DISTINCT es.userid
+                  FROM {event_subscriptions} es
+             LEFT JOIN {user} u ON u.id = es.userid
+                 WHERE u.deleted = 1 OR u.id IS NULL";
+        $deletedusers = $DB->get_field_sql($sql);
+        if ($deletedusers) {
+            list($sql, $params) = $DB->get_in_or_equal($deletedusers);
+
+            // Delete orphaned subscriptions.
+            $DB->execute("DELETE FROM {event_subscriptions} WHERE userid " . $sql, $params);
+        }
+
+        upgrade_main_savepoint(true, 2019083000.01);
+    }
+
     return true;
 }
index 1a5a4ba..0b39658 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2019083000.00;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2019083000.01;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.