Merge pull request #642 from opf/fix/delete_invalid_work_package_custom_values

[FIX] Delete invalid work package custom values
pull/650/head
sschu 11 years ago
commit b52c453baf
  1. 1
      app/models/journal_manager.rb
  2. 36
      db/migrate/migration_utils/customizable_utils.rb
  3. 18
      spec/models/work_package/work_package_acts_as_journalized_spec.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 }

@ -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,40 @@ 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
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
result = select_all <<-SQL
SELECT tmp.customized_id,

@ -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 }

Loading…
Cancel
Save