Merge pull request #9710 from opf/fix/cleanup_notifications_before_foreign_key

remove invalid notifications before setting constraint
pull/9711/head
Markus Kahl 3 years ago committed by GitHub
commit 1bc38c81da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      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

Loading…
Cancel
Save