From 1b90f532c339e1479fef87d31d470ab7bbf9586b Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Fri, 8 Jul 2022 22:50:42 +0200 Subject: [PATCH] Add migration to remove orphaned notifications --- .../20220708204200_remove_orphaned_notifications.rb | 9 +++++++++ db/schema.rb | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20220708204200_remove_orphaned_notifications.rb diff --git a/db/migrate/20220708204200_remove_orphaned_notifications.rb b/db/migrate/20220708204200_remove_orphaned_notifications.rb new file mode 100644 index 00000000..c55d8ad6 --- /dev/null +++ b/db/migrate/20220708204200_remove_orphaned_notifications.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class RemoveOrphanedNotifications < ActiveRecord::Migration[6.1] + def up + execute "DELETE FROM notifications WHERE target_type = 'Appendable' AND target_id NOT IN (SELECT id FROM appendables)" + + execute "DELETE FROM notifications WHERE target_type = 'Relationship' AND target_id NOT IN (SELECT id FROM relationships)" + end +end diff --git a/db/schema.rb b/db/schema.rb index f4a958cb..8101d9d9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_07_08_203722) do +ActiveRecord::Schema.define(version: 2022_07_08_204200) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql"