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

Destroy invalid journals

Oliver Günther5e71992ae7f5
diff --git a/db/migrate/20220818074150_fix_invalid_journals.rb b/db/migrate/20220818074150_fix_invalid_journals.rb
new file mode 100644
index 00000000000..e7441d75ade
--- /dev/null
+++ b/db/migrate/20220818074150_fix_invalid_journals.rb
@@ -0,0 +1,31 @@
+class FixInvalidJournals < ActiveRecord::Migration[7.0]
+ def up
+ get_broken_journals.each do |journable_type, relation|
+ next unless relation.any?
+
+ # rubocop:disable Rails/Output
+ puts "Cleaning up broken journals on #{journable_type}"
+ # rubocop:enable Rails/Output
+ relation.destroy_all
+ end
+ end
+
+ def down
+ # nothing to do
+ end
+
+ def get_broken_journals
+ Journal
+ .pluck('DISTINCT(journable_type)')
+ .compact
+ .to_h do |journable_type|
+ journal_class = journable_type.constantize.journal_class
+
+ relation = Journal
+ .where(journable_type:)
+ .where.not(data_type: journal_class.to_s)
+
+ [journable_type, relation]
+ end
+ end
+end