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
[22048] Destroy journals after destroying journalized
ulfertsa842f44bda9c
diff --git a/app/models/journal.rb b/app/models/journal.rbindex fc601268d50..bc40008b193 100644--- a/app/models/journal.rb+++ b/app/models/journal.rb@@ -48,21 +48,12 @@ class Journal < ApplicationRecordhas_many :attachable_journals, class_name: 'Journal::AttachableJournal', dependent: :destroyhas_many :customizable_journals, class_name: 'Journal::CustomizableJournal', dependent: :destroy+ before_destroy :destroy_data+# Scopes to all journals excluding the initial journal - useful for change# logs like the history on issue#showscope :changing, -> { where(['version > 1']) }- # TODO: check if this can be removed- # Overrides the +user=+ method created by the polymorphic +belongs_to+ user association.- # Based on the class of the object given, either the +user+ association columns or the- # +user_name+ string column is populated.- def user=(value)- case value- when ActiveRecord::Base then super(value)- else self.user = User.find_by_login(value)- end- end-# In conjunction with the included Comparable module, allows comparison of journal records# based on their corresponding version numbers, creation timestamps and IDs.def <=>(other)@@ -120,6 +111,10 @@ class Journal < ApplicationRecordprivate+ def destroy_data+ data.destroy+ end+def predecessor@predecessor ||= self.class.where(journable_type: journable_type, journable_id: journable_id)@@ -127,8 +122,4 @@ class Journal < ApplicationRecord.order("#{self.class.table_name}.version DESC").firstend-- def journalized_object_type- "#{journaled_type.gsub('Journal', '')}".constantize- endenddiff --git a/app/services/journals/create_service.rb b/app/services/journals/create_service.rbindex 1e1086c9c99..9a5e9926838 100644--- a/app/services/journals/create_service.rb+++ b/app/services/journals/create_service.rb@@ -385,7 +385,7 @@ module Journalsenddef journable_data_sql_addition- journable.class.vestal_journals_options[:data_sql]&.call(journable) || ''+ journable.class.aaj_options[:data_sql]&.call(journable) || ''enddef text_column_namesdiff --git a/db/migrate/20200924085508_cleanup_orphaned_journal_data.rb b/db/migrate/20200924085508_cleanup_orphaned_journal_data.rbnew file mode 100644index 00000000000..22d887d6ba5--- /dev/null+++ b/db/migrate/20200924085508_cleanup_orphaned_journal_data.rb@@ -0,0 +1,37 @@+class CleanupOrphanedJournalData < ActiveRecord::Migration[6.0]+ def up+ cleanup_orphaned_journals('attachable_journals')+ cleanup_orphaned_journals('customizable_journals')+ cleanup_orphaned_journals('attachment_journals')+ cleanup_orphaned_journals('changeset_journals')+ cleanup_orphaned_journals('message_journals')+ cleanup_orphaned_journals('news_journals')+ cleanup_orphaned_journals('wiki_content_journals')+ cleanup_orphaned_journals('work_package_journals')+ end++ # No down needed as this only cleans up data that should have been deleted anyway.++ private++ def cleanup_orphaned_journals(table)+ execute <<~SQL+ DELETE+ FROM+ #{table}+ WHERE+ #{table}.id IN (+ SELECT+ #{table}.id+ FROM+ #{table}+ LEFT OUTER JOIN+ journals+ ON+ journals.id = #{table}.journal_id+ WHERE+ journals.id IS NULL+ )+ SQL+ end+enddiff --git a/lib/plugins/acts_as_journalized/lib/acts/journalized/creation.rb b/lib/plugins/acts_as_journalized/lib/acts/journalized/creation.rbindex 4abc754eb04..ac7e9b59176 100644--- a/lib/plugins/acts_as_journalized/lib/acts/journalized/creation.rb+++ b/lib/plugins/acts_as_journalized/lib/acts/journalized/creation.rb@@ -79,29 +79,6 @@ module Acts::Journalizedendend- # Class methods added to ActiveRecord::Base to facilitate the creation of new journals.- module ClassMethods- # Overrides the basal +prepare_journaled_options+ method defined in VestalVersions::Options- # to extract the <tt>:calculate</tt> option into +vestal_journals_options+.- def prepare_journaled_options(options)- result = super(options)-- assign_vestal = lambda do |key, array|- return unless result[key]-- vestal_journals_options[key] = if array- Array(result.delete(key)).map(&:to_s).uniq- else- result.delete(key)- end- end-- assign_vestal.call(:data_sql, false)-- result- end- end-module InstanceMethods# Returns an array of column names that are journaled.def journaled_columns_namesdiff --git a/lib/plugins/acts_as_journalized/lib/acts/journalized/options.rb b/lib/plugins/acts_as_journalized/lib/acts/journalized/options.rbindex 01c143ec121..6d6c25cc382 100644--- a/lib/plugins/acts_as_journalized/lib/acts/journalized/options.rb+++ b/lib/plugins/acts_as_journalized/lib/acts/journalized/options.rb@@ -85,55 +85,28 @@ module Acts::Journalized# 1. Those passed directly to the +journaled+ method# 2. Those specified in an initializer +configure+ block# 3. Default values specified in +prepare_journaled_options+- #- # The method is overridden in feature modules that require specific options outside the- # standard +has_many+ associations.def prepare_journaled_options(options)- result_options = options_with_defaults(options)-- class_attribute :vestal_journals_options- self.vestal_journals_options = result_options.dup-- result_options.merge!(- extend: Array(result_options[:extend]).unshift(Versions)- )-- result_options+ class_attribute :aaj_options+ self.aaj_options = options_with_defaults(options)endprivate- def options_with_defaults(options)- journal_options = split_option_hashes(options)+ # Returns all of the provided options suitable for the+ # has_many :journals+ # association created by aaj+ def has_many_journals_options+ aaj_options+ .slice(*ActiveRecord::Associations::Builder::HasMany.send(:valid_options, {}))+ end- result_options = journal_options.symbolize_keys- result_options.reverse_merge!(+ def options_with_defaults(options)+ {class_name: Journal.name,- dependent: :delete_all,+ dependent: :destroy,foreign_key: :journable_id,as: :journable- )-- result_options- end-- # Splits an option has into three hashes:- ## => [{ options prefixed with "activity_" }, { options prefixed with "event_" }, { other options }]- def split_option_hashes(options)- journal_hash = {}-- options.each_pair do |k, v|- case k.to_s- when /\Aactivity_(.+)\z/- raise "Configuring activity via acts_as_journalized is no longer supported."- when /\Aevent_(.+)\z/- raise "Configuring events via acts_as_journalized is no longer supported."- else- journal_hash[k.to_sym] = v- end- end-- journal_hash+ }.merge(options.symbolize_keys)endendenddiff --git a/lib/plugins/acts_as_journalized/lib/acts_as_journalized.rb b/lib/plugins/acts_as_journalized/lib/acts_as_journalized.rbindex cb0578675b7..7f0a0650e46 100644--- a/lib/plugins/acts_as_journalized/lib/acts_as_journalized.rb+++ b/lib/plugins/acts_as_journalized/lib/acts_as_journalized.rb@@ -73,11 +73,11 @@ module Actsinclude_aaj_modules- journal_hash = prepare_journaled_options(options)+ prepare_journaled_options(options)has_many :journals, -> {order("#{Journal.table_name}.version ASC")- }, **journal_hash+ }, **has_many_journals_optionsendprivatediff --git a/spec/features/work_packages/tabs/activity_revisions_spec.rb b/spec/features/work_packages/tabs/activity_revisions_spec.rbindex 8cbd3c21bf4..9290f391a10 100644--- a/spec/features/work_packages/tabs/activity_revisions_spec.rb+++ b/spec/features/work_packages/tabs/activity_revisions_spec.rb@@ -8,7 +8,7 @@ describe 'Activity tab', js: true, selenium: true dowork_package.update(attributes.merge(updated_at: at))note_journal = work_package.journals.last- note_journal.update(created_at: at, user: attributes[:user])+ note_journal.update(created_at: at, user: user)endlet(:project) { FactoryBot.create :project_with_types, public: true }diff --git a/spec/features/work_packages/tabs/activity_tab_spec.rb b/spec/features/work_packages/tabs/activity_tab_spec.rbindex c25713c1a76..14bc26acc66 100644--- a/spec/features/work_packages/tabs/activity_tab_spec.rb+++ b/spec/features/work_packages/tabs/activity_tab_spec.rb@@ -8,7 +8,7 @@ describe 'Activity tab', js: true, selenium: true dowork_package.update(attributes.merge(updated_at: at))note_journal = work_package.journals.last- note_journal.update(created_at: at, user: attributes[:user])+ note_journal.update(created_at: at, user: user)endlet(:project) { FactoryBot.create :project_with_types, public: true }diff --git a/spec/models/work_package/work_package_acts_as_journalized_spec.rb b/spec/models/work_package/work_package_acts_as_journalized_spec.rbindex bd5e4c27bd2..b06a1d62049 100644--- a/spec/models/work_package/work_package_acts_as_journalized_spec.rb+++ b/spec/models/work_package/work_package_acts_as_journalized_spec.rb@@ -458,6 +458,52 @@ describe WorkPackage, type: :model doendend+ context 'on #destroy' do+ let(:project) { FactoryBot.create(:project) }+ let(:type) { FactoryBot.create(:type) }+ let(:custom_field) do+ FactoryBot.create(:int_wp_custom_field).tap do |cf|+ project.work_package_custom_fields << cf+ type.custom_fields << cf+ end+ end+ let(:work_package) do+ FactoryBot.create(:work_package,+ project: project,+ type: type,+ custom_field_values: { custom_field.id => 5 },+ attachments: [attachment])+ end+ let(:attachment) { FactoryBot.build(:attachment) }+ let!(:journal) { work_package.journals.first }+ let!(:customizable_journals) { journal.customizable_journals }+ let!(:attachable_journals) { journal.attachable_journals }++ before do+ work_package.destroy+ end++ it 'removes the journal' do+ expect(Journal.find_by(id: journal.id))+ .to be_nil+ end++ it 'removes the journal data' do+ expect(Journal::WorkPackageJournal.find_by(journal_id: journal.id))+ .to be_nil+ end++ it 'removes the customizable journals' do+ expect(Journal::CustomizableJournal.find_by(id: customizable_journals.map(&:id)))+ .to be_nil+ end++ it 'removes the attachable journals' do+ expect(Journal::AttachableJournal.find_by(id: attachable_journals.map(&:id)))+ .to be_nil+ end+ end+describe 'Acts as journalized' dobefore(:each) do@type ||= FactoryBot.create(:type_feature)