Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

handle notification deletion on journal cleanup

ulferts3cfdc9f417c9
diff --git a/db/migrate/20230306083203_non_null_data_reference_on_journals.rb b/db/migrate/20230306083203_non_null_data_reference_on_journals.rb
index 99c0740122f..d47cf0aab35 100644
--- a/db/migrate/20230306083203_non_null_data_reference_on_journals.rb
+++ b/db/migrate/20230306083203_non_null_data_reference_on_journals.rb
@@ -1,10 +1,43 @@
class NonNullDataReferenceOnJournals < ActiveRecord::Migration[7.0]
def change
reversible do |direction|
- direction.up { execute "DELETE FROM journals WHERE data_id IS NULL or data_type IS NULL" }
+ direction.up do
+ change_on_delete_on_notification_fk(true)
+ cleanup_invalid_journals
+ end
+ direction.down do
+ change_on_delete_on_notification_fk(false)
+ end
end
+ change_non_null_data_columns
+ end
+
+ private
+
+ def change_non_null_data_columns
change_column_null :journals, :data_id, false
change_column_null :journals, :data_type, false
end
+
+ def cleanup_invalid_journals
+ execute <<~SQL.squish
+ DELETE FROM journals
+ WHERE data_id IS NULL OR data_type IS NULL
+ SQL
+ end
+
+ def change_on_delete_on_notification_fk(cascade)
+ options = if cascade
+ { on_delete: :cascade }
+ else
+ {}
+ end
+
+ remove_foreign_key :notifications, :journals
+
+ add_foreign_key :notifications,
+ :journals,
+ **options
+ end
end