Merge branch 'MDL-66551-master' of git://github.com/lameze/moodle
[moodle.git] / lib / db / upgrade.php
index abb4128..1aa73b8 100644 (file)
@@ -3519,5 +3519,22 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2019083000.02);
     }
 
+    if ($oldversion < 2019083000.04) {
+        // 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.04);
+    }
+
     return true;
 }