Commit ff3e9af6 authored by Grzegorz Bizon's avatar Grzegorz Bizon

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
parents 3d56d93f e97671b8
---
title: Fix migration for removing orphaned issues.moved_to_id values in MySQL and PostgreSQL
merge_request:
author:
type: fixed
...@@ -15,8 +15,20 @@ class IssuesMovedToIdForeignKey < ActiveRecord::Migration ...@@ -15,8 +15,20 @@ class IssuesMovedToIdForeignKey < ActiveRecord::Migration
self.table_name = 'issues' self.table_name = 'issues'
def self.with_orphaned_moved_to_issues def self.with_orphaned_moved_to_issues
where('NOT EXISTS (SELECT true FROM issues WHERE issues.id = issues.moved_to_id)') 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') .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
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: 20171114150259 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