From 5cacdc4ec1f63338fab923ddb657fdba0a7e6300 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Tue, 26 Dec 2017 22:22:18 -0800 Subject: Fix migration for removing orphaned issues.moved_to_id values in MySQL According to https://dev.mysql.com/doc/refman/5.7/en/update.html, "You cannot update a table and select from the same table in a subquery." Attempting to do so results in the error: ``` Mysql2::Error: Table 'issues' is specified twice, both as a target for 'UPDATE' and as a separate source for data ``` Instead, we can use a LEFT JOIN on the same table to make MySQL do the right thing. Closes #41498 --- .../issues_moved_to_id_foreign_key_spec.rb | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 spec/migrations/issues_moved_to_id_foreign_key_spec.rb (limited to 'spec/migrations') diff --git a/spec/migrations/issues_moved_to_id_foreign_key_spec.rb b/spec/migrations/issues_moved_to_id_foreign_key_spec.rb new file mode 100644 index 00000000000..009207ef214 --- /dev/null +++ b/spec/migrations/issues_moved_to_id_foreign_key_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' +require Rails.root.join('db', 'migrate', '20171106151218_issues_moved_to_id_foreign_key.rb') + +# The schema version has to be far enough in advance to have the +# only_mirror_protected_branches column in the projects table to create a +# project via FactoryBot. +describe IssuesMovedToIdForeignKey, :migration, schema: 20171109115718 do + let!(:issue_first) { create(:issue, moved_to_id: issue_second.id) } + let!(:issue_second) { create(:issue, moved_to_id: issue_third.id) } + let!(:issue_third) { create(:issue) } + + subject { described_class.new } + + it 'removes the orphaned moved_to_id' do + subject.down + + issue_third.update_attributes(moved_to_id: 100000) + + subject.up + + expect(issue_first.reload.moved_to_id).to eq(issue_second.id) + expect(issue_second.reload.moved_to_id).to eq(issue_third.id) + expect(issue_third.reload.moved_to_id).to be_nil + end +end -- cgit v1.2.1