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

Avoid having to constantize the data_type

Oliver Günther8e3e04ec6f22
diff --git a/db/migrate/20220818074150_fix_invalid_journals.rb b/db/migrate/20220818074150_fix_invalid_journals.rb
index 9a1c8b64ae6..ee71a279973 100644
--- a/db/migrate/20220818074150_fix_invalid_journals.rb
+++ b/db/migrate/20220818074150_fix_invalid_journals.rb
@@ -34,8 +34,7 @@ class FixInvalidJournals < ActiveRecord::Migration[7.0]
# rubocop:disable Rails/Output
puts "Cleaning up broken journals on #{journable_type}"
# rubocop:enable Rails/Output
-
- destroy_journals relation
+ destroy_journals(relation)
end
end
@@ -62,25 +61,16 @@ class FixInvalidJournals < ActiveRecord::Migration[7.0]
end
def get_broken_journals
- journable_types
- .index_with { |journable_type| get_journal_relation(journable_type) }
- end
-
- def journable_types
- Journal.pluck('DISTINCT(journable_type)').compact
- end
-
- def get_journal_relation(journable_type)
- journal_class = journable_type.constantize.journal_class
+ Journal
+ .pluck('DISTINCT(journable_type)')
+ .compact
+ .to_h do |journable_type|
- Journal.where(journable_type:).where.not(data_type: journal_class.to_s)
- rescue NameError # the class has been removed in the meantime
- # rubocop:disable Rails/Output
- puts "Journable type '#{journable_type}' no longer exists. Removing all its journals."
- # rubocop:enable Rails/Output
+ relation = Journal
+ .where(journable_type:)
+ .where.not(data_type: "Journal::#{journable_type}Journal")
- # the journable type (e.g. WikiContent) doesn't exist anymore
- # so we can remove all remaining journals for it
- Journal.where(journable_type:)
+ [journable_type, relation]
+ end
end
end
diff --git a/spec/migrations/fix_invalid_journals_spec.rb b/spec/migrations/fix_invalid_journals_spec.rb
index a2f7b27e718..8fa0b7c3e49 100644
--- a/spec/migrations/fix_invalid_journals_spec.rb
+++ b/spec/migrations/fix_invalid_journals_spec.rb
@@ -36,14 +36,15 @@ RSpec.describe FixInvalidJournals, type: :model do
let(:attachment) { create :attachment }
let(:custom_field) { create :custom_field }
let!(:work_package_journals) { create_list :work_package_journal, 3 }
- let!(:invalid_journal) do
+
+ let!(:valid_journal) do
user = create :user
ActiveRecord::Base.connection.execute("
INSERT INTO journals
(journable_type, data_type, data_id, user_id, created_at, updated_at, validity_period)
VALUES
- ('Foo', 'Journal::BarJournal', 0, #{user.id}, '2023-07-01', '2023-07-01', '[\"2021-05-03 12:53:28.245599+00\",)')
+ ('WikiContent', 'Journal::WikiContentJournal', 0, #{user.id}, '2023-07-01', '2023-07-01', '[\"2021-05-03 12:53:28.245599+00\",)')
")
journal = Journal.last
@@ -65,20 +66,51 @@ RSpec.describe FixInvalidJournals, type: :model do
journal
end
+ let!(:invalid_journal) do
+ user = create :user
+
+ ActiveRecord::Base.connection.execute("
+ INSERT INTO journals
+ (journable_type, data_type, data_id, user_id, created_at, updated_at, validity_period)
+ VALUES
+ ('WikiContent', 'Journal::FooJournal', 0, #{user.id}, '2023-07-01',
+ '2023-07-01', '[\"2021-05-03 12:53:28.245599+00\",)')
+ ")
+
+ journal = Journal.last
+
+ ActiveRecord::Base.connection.execute("
+ INSERT INTO attachable_journals
+ (journal_id, attachment_id, filename)
+ VALUES
+ (#{journal.id}, #{attachment.id}, 'bar')
+ ")
+
+ ActiveRecord::Base.connection.execute("
+ INSERT INTO customizable_journals
+ (journal_id, custom_field_id, value)
+ VALUES
+ (#{journal.id}, #{custom_field.id}, 'bar')
+ ")
+
+ journal
+ end
+
before do
run_migration
end
- it 'removes invalid journals' do
+ it 'removes invalid journals and its associations', :aggregate_failures do
expect(Journal.find_by(id: invalid_journal.id)).to be_nil
- end
- it 'removes related journals' do
expect(Journal::AttachableJournal.find_by(journal_id: invalid_journal.id)).to be_nil
expect(Journal::CustomizableJournal.find_by(journal_id: invalid_journal.id)).to be_nil
- end
- it 'keeps valid journals' do
+ # Keeps the valid, outdated journal
+ expect(Journal.find_by(id: valid_journal.id)).to be_present
+ expect(Journal::AttachableJournal.find_by(journal_id: valid_journal.id)).to be_present
+ expect(Journal::CustomizableJournal.find_by(journal_id: valid_journal.id)).to be_present
+
expect(Journal.find(work_package_journals.map(&:id))).to eq work_package_journals
end
end