From 7088d80cad11d9dbc9ec13fbf04dde8d77de8e33 Mon Sep 17 00:00:00 2001 From: Hagen Schink Date: Tue, 12 Nov 2013 12:46:50 +0100 Subject: [PATCH 1/4] Normalizes predecessor data too --- app/models/journal_manager.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/journal_manager.rb b/app/models/journal_manager.rb index 4bb53271b3..39b926cdeb 100644 --- a/app/models/journal_manager.rb +++ b/app/models/journal_manager.rb @@ -51,6 +51,7 @@ class JournalManager predecessor = journable.journals.last.data.journaled_attributes current = normalize_newlines(current) + predecessor = normalize_newlines(predecessor) return predecessor.map{|k,v| current[k.to_s] != v} .inject(false) { |r, c| r || c } From 60f56c0795b74c2198df0a29401ac665e1068167 Mon Sep 17 00:00:00 2001 From: Hagen Schink Date: Tue, 12 Nov 2013 12:57:15 +0100 Subject: [PATCH 2/4] Deletes invalid work package custom values --- .../migration_utils/customizable_utils.rb | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/db/migrate/migration_utils/customizable_utils.rb b/db/migrate/migration_utils/customizable_utils.rb index 1b5993c5f9..05d46b9cfd 100644 --- a/db/migrate/migration_utils/customizable_utils.rb +++ b/db/migrate/migration_utils/customizable_utils.rb @@ -19,6 +19,8 @@ module Migration::Utils :last_version) def add_missing_customizable_journals + delete_invalid_work_package_custom_values + result = missing_custom_values repair_journals(result) @@ -66,6 +68,27 @@ module Migration::Utils private + # Removes all work package custom values that are not referenced by the + # work package's project AND type. + def delete_invalid_work_package_custom_values + delete <<-SQL + DELETE FROM custom_values AS cvd + WHERE EXISTS + ( + SELECT w.id, cf.id, cfp.project_id, p.name, cft.type_id + FROM work_packages AS w + JOIN custom_values AS cv ON (w.id = cv.customized_id AND cv.customized_type = 'WorkPackage') + JOIN custom_fields AS cf ON (cv.custom_field_id = cf.id) + JOIN projects AS p ON (w.project_id = p.id) + LEFT JOIN custom_fields_projects AS cfp ON (cv.custom_field_id = cfp.custom_field_id AND w.project_id = cfp.project_id) + LEFT JOIN custom_fields_types AS cft ON (cv.custom_field_id = cft.custom_field_id AND w.type_id = cft.type_id) + WHERE (cfp.project_id IS NULL + OR cft.type_id IS NULL) + AND cv.id = cvd.id + ); + SQL + end + def missing_custom_values result = select_all <<-SQL SELECT tmp.customized_id, From e077115f4af865047b18c967451ee5083603eedf Mon Sep 17 00:00:00 2001 From: Hagen Schink Date: Tue, 12 Nov 2013 16:03:43 +0100 Subject: [PATCH 3/4] Fixes delete statement for MySQL --- .../migration_utils/customizable_utils.rb | 45 ++++++++++++------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/db/migrate/migration_utils/customizable_utils.rb b/db/migrate/migration_utils/customizable_utils.rb index 05d46b9cfd..b8d2a0867e 100644 --- a/db/migrate/migration_utils/customizable_utils.rb +++ b/db/migrate/migration_utils/customizable_utils.rb @@ -71,22 +71,35 @@ module Migration::Utils # Removes all work package custom values that are not referenced by the # work package's project AND type. def delete_invalid_work_package_custom_values - delete <<-SQL - DELETE FROM custom_values AS cvd - WHERE EXISTS - ( - SELECT w.id, cf.id, cfp.project_id, p.name, cft.type_id - FROM work_packages AS w - JOIN custom_values AS cv ON (w.id = cv.customized_id AND cv.customized_type = 'WorkPackage') - JOIN custom_fields AS cf ON (cv.custom_field_id = cf.id) - JOIN projects AS p ON (w.project_id = p.id) - LEFT JOIN custom_fields_projects AS cfp ON (cv.custom_field_id = cfp.custom_field_id AND w.project_id = cfp.project_id) - LEFT JOIN custom_fields_types AS cft ON (cv.custom_field_id = cft.custom_field_id AND w.type_id = cft.type_id) - WHERE (cfp.project_id IS NULL - OR cft.type_id IS NULL) - AND cv.id = cvd.id - ); - SQL + if mysql? + delete <<-SQL + DELETE cv.* FROM custom_values AS cv + JOIN work_packages AS w ON (w.id = cv.customized_id AND cv.customized_type = 'WorkPackage') + JOIN custom_fields AS cf ON (cv.custom_field_id = cf.id) + JOIN projects AS p ON (w.project_id = p.id) + LEFT JOIN custom_fields_projects AS cfp ON (cv.custom_field_id = cfp.custom_field_id AND w.project_id = cfp.project_id) + LEFT JOIN custom_fields_types AS cft ON (cv.custom_field_id = cft.custom_field_id AND w.type_id = cft.type_id) + WHERE cfp.project_id IS NULL + OR cft.type_id IS NULL + SQL + else + delete <<-SQL + DELETE FROM custom_values AS cvd + WHERE EXISTS + ( + SELECT w.id, cf.id, cfp.project_id, p.name, cft.type_id + FROM work_packages AS w + JOIN custom_values AS cv ON (w.id = cv.customized_id AND cv.customized_type = 'WorkPackage') + JOIN custom_fields AS cf ON (cv.custom_field_id = cf.id) + JOIN projects AS p ON (w.project_id = p.id) + LEFT JOIN custom_fields_projects AS cfp ON (cv.custom_field_id = cfp.custom_field_id AND w.project_id = cfp.project_id) + LEFT JOIN custom_fields_types AS cft ON (cv.custom_field_id = cft.custom_field_id AND w.type_id = cft.type_id) + WHERE (cfp.project_id IS NULL + OR cft.type_id IS NULL) + AND cv.id = cvd.id + ); + SQL + end end def missing_custom_values From e8bab8021668a4751514480507e5a87c26a92369 Mon Sep 17 00:00:00 2001 From: Hagen Schink Date: Tue, 12 Nov 2013 16:22:30 +0100 Subject: [PATCH 4/4] Fixes spec --- .../work_package_acts_as_journalized_spec.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) 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.rb index 1dcc819852..54cc8c8869 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 @@ -106,14 +106,16 @@ describe WorkPackage do end context 'when there is a legacy journal containing non-escaped newlines' do - before do - work_package_1.save - # force the latest journal to match the description with unescaped newline characters - legacy_journal = work_package_1.journals.last - legacy_journal.data.update_column :description, changed_description - # rollback work package description to normalized newlines - work_package_1.update_attributes description: description - end + let!(:work_package_journal_1) { FactoryGirl.create(:work_package_journal, + journable_id: work_package_1.id, + version: 2, + data: FactoryGirl.build(:journal_work_package_journal, + description: description)) } + let!(:work_package_journal_2) { FactoryGirl.create(:work_package_journal, + journable_id: work_package_1.id, + version: 3, + data: FactoryGirl.build(:journal_work_package_journal, + description: changed_description)) } subject { work_package_1.journals.last.details }