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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
make 2022 migration work independently of changes in the future
Markus Kahl947b7243aa23
diff --git a/.rubocop.yml b/.rubocop.ymlindex 2a5fd5ed434..8d15d414c7f 100644--- a/.rubocop.yml+++ b/.rubocop.yml@@ -23,6 +23,9 @@ AllCops:Exclude:- '**/node_modules/**/*'+FactoryBot/ConsistentParenthesesStyle:+ Enabled: false+FactoryBot/SyntaxMethods:Enabled: truediff --git a/db/migrate/20220818074150_fix_invalid_journals.rb b/db/migrate/20220818074150_fix_invalid_journals.rbindex 922d0e0a108..9a1c8b64ae6 100644--- a/db/migrate/20220818074150_fix_invalid_journals.rb+++ b/db/migrate/20220818074150_fix_invalid_journals.rb@@ -34,7 +34,8 @@ class FixInvalidJournals < ActiveRecord::Migration[7.0]# rubocop:disable Rails/Outputputs "Cleaning up broken journals on #{journable_type}"# rubocop:enable Rails/Output- relation.destroy_all++ destroy_journals relationendend@@ -42,18 +43,44 @@ class FixInvalidJournals < ActiveRecord::Migration[7.0]# nothing to doend+ def destroy_journals(journals)+ journal_ids = journals.pluck(:id)++ Journal::AttachableJournal.where(journal_id: journal_ids).delete_all+ Journal::CustomizableJournal.where(journal_id: journal_ids).delete_all++ journals.delete_all++ # We delete manually the related journals here rather than+ # relying on .destroy. This is because in the future (from+ # the migration's perspective) new such journals may appear+ # which do not yet exist at the point of this migration.+ #+ # This is to avoid errors such as the following:+ # PG::UndefinedTable: ERROR: relation "storages_file_links_journals" does not exist+ # LINE 9: WHERE a.attrelid = '"storages_file_links_journals"'::regcla...+ end+def get_broken_journals- Journal- .pluck('DISTINCT(journable_type)')- .compact- .to_h do |journable_type|- journal_class = journable_type.constantize.journal_class+ journable_types+ .index_with { |journable_type| get_journal_relation(journable_type) }+ end- relation = Journal- .where(journable_type:)- .where.not(data_type: journal_class.to_s)+ def journable_types+ Journal.pluck('DISTINCT(journable_type)').compact+ end- [journable_type, relation]- end+ def get_journal_relation(journable_type)+ journal_class = journable_type.constantize.journal_class++ 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++ # the journable type (e.g. WikiContent) doesn't exist anymore+ # so we can remove all remaining journals for it+ Journal.where(journable_type:)endenddiff --git a/spec/migrations/fix_invalid_journals_spec.rb b/spec/migrations/fix_invalid_journals_spec.rbnew file mode 100644index 00000000000..a2f7b27e718--- /dev/null+++ b/spec/migrations/fix_invalid_journals_spec.rb@@ -0,0 +1,84 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require 'spec_helper'+require Rails.root.join("db/migrate/20220818074150_fix_invalid_journals.rb")++RSpec.describe FixInvalidJournals, type: :model do+ # Silencing migration logs, since we are not interested in that during testing+ subject(:run_migration) { ActiveRecord::Migration.suppress_messages { described_class.new.up } }++ let(:attachment) { create :attachment }+ let(:custom_field) { create :custom_field }+ let!(:work_package_journals) { create_list :work_package_journal, 3 }+ 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+ ('Foo', 'Journal::BarJournal', 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}, 'foo')+ ")++ ActiveRecord::Base.connection.execute("+ INSERT INTO customizable_journals+ (journal_id, custom_field_id, value)+ VALUES+ (#{journal.id}, #{custom_field.id}, 'foo')+ ")++ journal+ end++ before do+ run_migration+ end++ it 'removes invalid journals' 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+ expect(Journal.find(work_package_journals.map(&:id))).to eq work_package_journals+ end+end