Commit 5cacdc4e authored by Stan Hu's avatar Stan Hu

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
parent a5a0f3f7
---
title: Fix migration for removing orphaned issues.moved_to_id values in MySQL
merge_request:
author:
type: fixed
...@@ -21,8 +21,10 @@ class IssuesMovedToIdForeignKey < ActiveRecord::Migration ...@@ -21,8 +21,10 @@ class IssuesMovedToIdForeignKey < ActiveRecord::Migration
end end
def up def up
Issue.with_orphaned_moved_to_issues.each_batch(of: 100) do |batch| if Gitlab::Database.postgresql?
batch.update_all(moved_to_id: nil) postgresql_remove_orphaned_moved_to_ids
else
mysql_remove_orphaned_moved_to_ids
end end
add_concurrent_foreign_key( add_concurrent_foreign_key(
...@@ -41,4 +43,23 @@ class IssuesMovedToIdForeignKey < ActiveRecord::Migration ...@@ -41,4 +43,23 @@ class IssuesMovedToIdForeignKey < ActiveRecord::Migration
remove_foreign_key_without_error(:issues, column: :moved_to_id) remove_foreign_key_without_error(:issues, column: :moved_to_id)
remove_concurrent_index(:issues, :moved_to_id) remove_concurrent_index(:issues, :moved_to_id)
end end
private
def postgresql_remove_orphaned_moved_to_ids
Issue.with_orphaned_moved_to_issues.each_batch(of: 100) do |batch|
batch.update_all(moved_to_id: nil)
end
end
# MySQL doesn't allow modification of the same table in a subquery. See
# https://dev.mysql.com/doc/refman/5.7/en/update.html for more details.
def mysql_remove_orphaned_moved_to_ids
execute <<~SQL
UPDATE issues AS a
LEFT JOIN issues AS b ON a.moved_to_id = b.id
SET a.moved_to_id = NULL
WHERE a.moved_to_id IS NOT NULL AND b.id IS NULL;
SQL
end
end end
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment