diff --git a/db/migrate/20210922123908_notification_foreign_key_constraint.rb b/db/migrate/20210922123908_notification_foreign_key_constraint.rb index 44d126b5b8..19914217a2 100644 --- a/db/migrate/20210922123908_notification_foreign_key_constraint.rb +++ b/db/migrate/20210922123908_notification_foreign_key_constraint.rb @@ -1,6 +1,23 @@ class NotificationForeignKeyConstraint < ActiveRecord::Migration[6.1] def change + reversible do |dir| + dir.up do + cleanup_invalid_notifications + end + end + add_foreign_key :notifications, :journals add_index :notifications, :journal_id end + + private + + def cleanup_invalid_notifications + execute <<~SQL.squish + DELETE FROM notifications to_delete + USING notifications to_identify + LEFT JOIN journals ON to_identify.journal_id = journals.id + WHERE to_delete.id = to_identify.id AND journals.id IS NULL + SQL + end end