summaryrefslogtreecommitdiff
path: root/spec/migrations
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2017-12-26 22:22:18 -0800
committerStan Hu <stanhu@gmail.com>2017-12-26 22:29:15 -0800
commit5cacdc4ec1f63338fab923ddb657fdba0a7e6300 (patch)
tree2a005389514cd918af7dc49b3c02fdf909181b8d /spec/migrations
parenta5a0f3f725c4f5c6d11d33e18493d93e07e53183 (diff)
downloadgitlab-ce-5cacdc4ec1f63338fab923ddb657fdba0a7e6300.tar.gz
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
Diffstat (limited to 'spec/migrations')
-rw-r--r--spec/migrations/issues_moved_to_id_foreign_key_spec.rb25
1 files changed, 25 insertions, 0 deletions
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