Merge pull request #9710 from opf/fix/cleanup_notifications_before_foreign_key
remove invalid notifications before setting constraintpull/9711/head
commit
1bc38c81da
@ -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…
Reference in new issue