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
avoid destroying journals, instead, change the data
ulferts0a7b3a6b2027
diff --git a/app/services/journals/create_service.rb b/app/services/journals/create_service.rbindex 21d39066902..5ac1ac584b8 100644--- a/app/services/journals/create_service.rb+++ b/app/services/journals/create_service.rb@@ -49,11 +49,11 @@ module Journalsdef call(notes: '')Journal.transaction do- journal = create_journal(notes)+ predecessor = strip_predecessor(notes)+ journal = create_journal(predecessor, notes)return ServiceResult.success unless journal- destroy_predecessor(journal)reload_journalstouch_journable(journal)@@ -63,22 +63,29 @@ module Journalsprivate- def destroy_predecessor(journal)- predecessor = journal.previous+ # If the journalizing happens within the configured aggregation time, is carried out by the same user+ # and only the predecessor or the journal to be created has notes, the changes are aggregated.+ # Instead of removing the predecessor, it is stripped of all data to later be filled with the current+ # snapshot. That way, references to the journal, including ones users have, are kept intact.+ def strip_predecessor(notes)+ predecessor = journable.last_journal- if aggregatable?(predecessor, journal)- notify_aggregation_destruction(predecessor, journal)+ if aggregatable?(predecessor, notes)+ # TODO: check if this can be moved into the joined sql+ predecessor.data.delete+ predecessor.customizable_journals.delete_all+ predecessor.attachable_journals.delete_all- predecessor.destroy-- take_over_journal_details(predecessor, journal)+ predecessor+ else+ nilendend- def create_journal(notes)+ def create_journal(predecessor, notes)Rails.logger.debug { "Inserting new journal for #{journable_type} ##{journable.id}" }- create_sql = create_journal_sql(notes)+ create_sql = create_journal_sql(predecessor, notes)# We need to ensure that the result is genuine. Otherwise,# calling the service repeatedly for the same journable@@ -134,16 +141,16 @@ module Journals## If a journal is created, all entries in the custom_values table associated to the journable are recreated as entries# in the customizable_journals table. Again, newlines are normalized.- def create_journal_sql(notes)+ def create_journal_sql(predecessor, notes)<<~SQLWITH max_journals AS (- #{select_max_journal_sql}+ #{select_max_journal_sql(predecessor)}), changes AS (#{select_changed_sql}), insert_data AS (#{insert_data_sql(notes)}), inserted_journal AS (- #{insert_journal_sql(notes)}+ #{update_or_insert_journal_sql(predecessor, notes)}), insert_attachable AS (#{insert_attachable_sql}), insert_customizable AS (@@ -154,6 +161,43 @@ module JournalsSQLend+ def update_or_insert_journal_sql(predecessor, notes)+ if predecessor+ update_journal_sql(predecessor, notes)+ else+ insert_journal_sql(notes)+ end+ end++ def update_journal_sql(predecessor, notes)+ # If there is a predecessor, we don't want to create a new one, we simply rewrite it.+ # The original data of that predecessor (data e.g. work_package_journals, customizable_journals, attachable_journals)+ # has been deleted before but the notes need to taken over and the timestamps updated as if the+ # journal would have been created.+ #+ # A lot of the data does not need to be set anew, since we only aggregate if that data stays the same+ # (e.g. the user_id).+ journal_sql = <<~SQL+ UPDATE+ journals+ SET+ notes = :notes,+ created_at = #{journal_timestamp_sql(notes, ':created_at')},+ updated_at = #{journal_timestamp_sql(notes, ':updated_at')},+ data_id = insert_data.id+ FROM insert_data+ WHERE journals.id = :predecessor_id+ RETURNING+ journals.*+ SQL++ sanitize(journal_sql,+ notes: notes.present? ? notes : predecessor.notes,+ created_at: journable_timestamp,+ updated_at: journable_timestamp,+ predecessor_id: predecessor.id)+ end+def insert_journal_sql(notes)journal_sql = <<~SQLINSERT INTO@@ -184,15 +228,15 @@ module JournalsRETURNING *SQL- ::OpenProject::SqlSanitization.sanitize(journal_sql,- notes:,- journable_id: journable.id,- activity_type: journable.activity_type,- journable_type:,- user_id: user.id,- created_at: journable_timestamp,- updated_at: journable_timestamp,- data_type: journable.class.journal_class.name)+ sanitize(journal_sql,+ notes:,+ journable_id: journable.id,+ activity_type: journable.activity_type,+ journable_type:,+ user_id: user.id,+ created_at: journable_timestamp,+ updated_at: journable_timestamp,+ data_type: journable.class.journal_class.name)enddef insert_data_sql(notes)@@ -217,8 +261,8 @@ module JournalsRETURNING *SQL- ::OpenProject::SqlSanitization.sanitize(data_sql,- journable_id: journable.id)+ sanitize(data_sql,+ journable_id: journable.id)enddef insert_attachable_sql@@ -240,9 +284,9 @@ module JournalsAND attachments.container_type = :journable_class_nameSQL- ::OpenProject::SqlSanitization.sanitize(attachable_sql,- journable_id: journable.id,- journable_class_name: journable.class.name)+ sanitize(attachable_sql,+ journable_id: journable.id,+ journable_class_name: journable.class.name)enddef insert_customizable_sql@@ -266,13 +310,24 @@ module JournalsAND custom_values.value != ''SQL- ::OpenProject::SqlSanitization.sanitize(customizable_sql,- journable_id: journable.id,- journable_class_name: journable.class.name)+ sanitize(customizable_sql,+ journable_id: journable.id,+ journable_class_name: journable.class.name)end- def select_max_journal_sql- max_journal_sql = <<~SQL+ def select_max_journal_sql(predecessor)+ journal_version_sql = if predecessor+ sanitize "SELECT MAX(version) FROM journals WHERE journable_id = :journable_id AND journable_type = :journable_type AND version < :predecessor_version",+ journable_id: journable.id,+ journable_type:,+ predecessor_version: predecessor.version+ else+ sanitize "SELECT MAX(version) FROM journals WHERE journable_id = :journable_id AND journable_type = :journable_type",+ journable_id: journable.id,+ journable_type:+ end++ max_journal_sql = <<~SQLSELECT:journable_id journable_id,:journable_type journable_type,@@ -286,12 +341,12 @@ module JournalsONjournals.journable_id = :journable_idAND journals.journable_type = :journable_type- AND journals.version IN (SELECT MAX(version) FROM journals WHERE journable_id = :journable_id AND journable_type = :journable_type)+ AND journals.version IN (#{journal_version_sql})SQL- ::OpenProject::SqlSanitization.sanitize(max_journal_sql,- journable_id: journable.id,- journable_type:)+ sanitize(max_journal_sql,+ journable_id: journable.id,+ journable_type:)enddef select_changed_sql@@ -332,9 +387,9 @@ module JournalsOR (attachable_journals.attachment_id IS NULL AND attachments.id IS NOT NULL)SQL- ::OpenProject::SqlSanitization.sanitize(attachable_changes_sql,- journable_id: journable.id,- container_type: journable.class.name)+ sanitize(attachable_changes_sql,+ journable_id: journable.id,+ container_type: journable.class.name)enddef customizable_changes_sql@@ -360,9 +415,9 @@ module Journals#{normalize_newlines_sql('custom_values.value')})SQL- ::OpenProject::SqlSanitization.sanitize(customizable_changes_sql,- customized_type: journable.class.name,- journable_id: journable.id)+ sanitize(customizable_changes_sql,+ customized_type: journable.class.name,+ journable_id: journable.id)enddef data_changes_sql@@ -383,8 +438,8 @@ module Journals#{journable_table_name}.id = :journable_id AND (#{data_changes_condition_sql})SQL- ::OpenProject::SqlSanitization.sanitize(data_changes_sql,- journable_id: journable.id)+ sanitize(data_changes_sql,+ journable_id: journable.id)enddef only_if_created_sql@@ -482,12 +537,12 @@ module Journalsjournable.update_columns(timestamps) if timestamps.any?end- def aggregatable?(predecessor, journal)+ def aggregatable?(predecessor, notes)predecessor.present? &&Setting.journal_aggregation_time_minutes.to_i > 0 &&- predecessor.created_at >= journal.created_at - Setting.journal_aggregation_time_minutes.to_i.minutes &&- predecessor.user_id == journal.user_id &&- (predecessor.notes.empty? || journal.notes.empty?)+ predecessor.created_at >= Time.zone.now - Setting.journal_aggregation_time_minutes.to_i.minutes &&+ predecessor.user_id == user.id &&+ (predecessor.notes.empty? || notes.empty?)enddef notify_aggregation_destruction(predecessor, journal)@@ -503,5 +558,8 @@ module Journalsjournal.update_columns(version: predecessor.version)endend++ delegate :sanitize,+ to: ::OpenProject::SqlSanitizationendenddiff --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 66e19a0e405..96633db2404 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@@ -37,22 +37,19 @@ describe WorkPackage, type: :model doendlet(:status) { create :default_status }let(:priority) { create :priority }- let(:work_package) do- create(:work_package,- project_id: project.id,- type:,- description: 'Description',- priority:,- status:,- duration: 1)+ let!(:work_package) do+ User.execute_as current_user do+ create(:work_package,+ project_id: project.id,+ type:,+ description: 'Description',+ priority:,+ status:,+ duration: 1)+ endend- let(:current_user) { create(:user) }- before do- login_as(current_user)-- work_package- end+ current_user { create(:user) }context 'for work package creation' doit { expect(Journal.all.count).to eq(1) }@@ -548,26 +545,31 @@ describe WorkPackage, type: :model docontext 'when adding another change without comment' dobefore dowork_package.reload # need to update the lock_version, avoiding StaleObjectError- changes = { subject: 'foo' }-- work_package.attributes = changes+ work_package.subject = 'foo'+ work_package.assigned_to = current_userwork_package.save!end- it 'leads to a single journal with the comment of the replaced journal and the state of the second' do- expect(subject.count).to be 1+ it 'leads to a single journal with the comment of the replaced journal and the state both combined' do+ expect(subject.count).to eq 1expect(subject.first.notes).to eql notesexpect(subject.first.data.subject).to eql 'foo'++ expect(subject.first.data.assigned_to)+ .to eql current_user++ expect(subject.first.data.status_id)+ .to eql new_status.idendendendend- context 'as a different author' do+ context 'with a different author' dolet(:new_author) { user2 }it 'leads to two journals' do