diff options
author | Stan Hu <stanhu@gmail.com> | 2017-12-26 22:22:18 -0800 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2017-12-26 22:29:15 -0800 |
commit | 5cacdc4ec1f63338fab923ddb657fdba0a7e6300 (patch) | |
tree | 2a005389514cd918af7dc49b3c02fdf909181b8d /db/migrate | |
parent | a5a0f3f725c4f5c6d11d33e18493d93e07e53183 (diff) | |
download | gitlab-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 'db/migrate')
-rw-r--r-- | db/migrate/20171106151218_issues_moved_to_id_foreign_key.rb | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/db/migrate/20171106151218_issues_moved_to_id_foreign_key.rb b/db/migrate/20171106151218_issues_moved_to_id_foreign_key.rb index 8d2ceb8cc18..18c2b294c1e 100644 --- a/db/migrate/20171106151218_issues_moved_to_id_foreign_key.rb +++ b/db/migrate/20171106151218_issues_moved_to_id_foreign_key.rb @@ -21,8 +21,10 @@ class IssuesMovedToIdForeignKey < ActiveRecord::Migration end def up - Issue.with_orphaned_moved_to_issues.each_batch(of: 100) do |batch| - batch.update_all(moved_to_id: nil) + if Gitlab::Database.postgresql? + postgresql_remove_orphaned_moved_to_ids + else + mysql_remove_orphaned_moved_to_ids end add_concurrent_foreign_key( @@ -41,4 +43,23 @@ class IssuesMovedToIdForeignKey < ActiveRecord::Migration remove_foreign_key_without_error(:issues, column: :moved_to_id) remove_concurrent_index(:issues, :moved_to_id) 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 |