diff --git a/lib/redmine_contracts/budget_plugin_migration.rb b/lib/redmine_contracts/budget_plugin_migration.rb index ab71634..a941cdf 100644 --- a/lib/redmine_contracts/budget_plugin_migration.rb +++ b/lib/redmine_contracts/budget_plugin_migration.rb @@ -38,6 +38,9 @@ module RedmineContracts def self.migrate(old_data) @@data = YAML.load(old_data) + # Map old deliverable ids to the new ones + @deliverable_mapper = {} + ActiveRecord::Base.transaction do @@data.each do |old_deliverable| @@ -74,8 +77,14 @@ module RedmineContracts append_old_deliverable_to_notes(old_deliverable, deliverable) deliverable.save! + + @deliverable_mapper[old_deliverable['id']] = deliverable.id end end + + @deliverable_mapper.each do |old, new| + Issue.update_all(["deliverable_id = ?", new], ["deliverable_id = ?", old]) + end end def self.data diff --git a/test/fixtures/budget_plugin_migration/budget.yml b/test/fixtures/budget_plugin_migration/budget.yml index 52129b3..eba71ef 100644 --- a/test/fixtures/budget_plugin_migration/budget.yml +++ b/test/fixtures/budget_plugin_migration/budget.yml @@ -43,7 +43,7 @@ cost_per_hour: 85.0 subject: Version 1.0 materials: 0.0 - id: 3 + id: 4 type: FixedDeliverable project_manager_signoff: true profit: diff --git a/test/integration/budget_plugin_migration_test.rb b/test/integration/budget_plugin_migration_test.rb index 58023c5..0ad9ef1 100644 --- a/test/integration/budget_plugin_migration_test.rb +++ b/test/integration/budget_plugin_migration_test.rb @@ -172,6 +172,28 @@ class BudgetPluginMigrationTest < ActionController::IntegrationTest end end + context "converting issue ids" do + setup do + @issue1 = Issue.generate_for_project!(@project_two, :deliverable_id => 2) + @issue2 = Issue.generate_for_project!(@project_two, :deliverable_id => 4) + + end + + should "keep the same Deliverable assigned" do + RedmineContracts::BudgetPluginMigration.migrate(@data) + + assert_equal "Deliverable 2", @issue1.reload.deliverable.title + assert_equal "Version 1.0", @issue2.reload.deliverable.title + end + + should "handle primary key differences between the old and new Deliverables" do + RedmineContracts::BudgetPluginMigration.migrate(@data) + + # The "third" deliverable has an id of 4 + assert_equal "Version 1.0", @issue2.reload.deliverable.title + end + end + end end