diff options
author | Grzegorz Bizon <grzegorz@gitlab.com> | 2017-12-27 19:14:36 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzegorz@gitlab.com> | 2017-12-27 19:14:36 +0300 |
commit | ff3e9af6fdac37ae71ea526bbb9aaf4b7d2ce876 (patch) | |
tree | 75ece9c970cfa82f76c8ed6b8f5dbfd4ded60dd9 /db | |
parent | 3d56d93fc53308ad384a0aa22df970b58ea543e9 (diff) | |
parent | e97671b81259a2e8bc02babbe94f5ab7aa19fa93 (diff) |
Merge branch 'sh-fix-mysql-migration-10-3' into 'master'
Fix migration for removing orphaned issues.moved_to_id values in MySQL and PostgreSQL
Closes #41498
See merge request gitlab-org/gitlab-ce!16141
Diffstat (limited to 'db')
-rw-r--r-- | db/migrate/20171106151218_issues_moved_to_id_foreign_key.rb | 16 |
1 files changed, 14 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..6395462384b 100644 --- a/db/migrate/20171106151218_issues_moved_to_id_foreign_key.rb +++ b/db/migrate/20171106151218_issues_moved_to_id_foreign_key.rb @@ -15,8 +15,20 @@ class IssuesMovedToIdForeignKey < ActiveRecord::Migration self.table_name = 'issues' def self.with_orphaned_moved_to_issues - where('NOT EXISTS (SELECT true FROM issues WHERE issues.id = issues.moved_to_id)') - .where('moved_to_id IS NOT NULL') + if Gitlab::Database.postgresql? + # Be careful to use a second table here for comparison otherwise we'll null + # out all rows that don't have id == moved.to_id! + where('NOT EXISTS (SELECT true FROM issues B WHERE issues.moved_to_id = B.id)') + .where('moved_to_id IS NOT NULL') + else + # MySQL doesn't allow modification of the same table in a subquery, + # and using a temporary table isn't automatically guaranteed to work + # due to the MySQL query optimizer. See + # https://dev.mysql.com/doc/refman/5.7/en/update.html for more + # details. + joins('LEFT JOIN issues AS b ON issues.moved_to_id = b.id') + .where('issues.moved_to_id IS NOT NULL AND b.id IS NULL') + end end end |