diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-07-18 11:36:37 +0200 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2017-07-18 12:07:12 +0200 |
commit | 2deeac56438a5056444563596433b7bb448160fe (patch) | |
tree | 280513dfcbeec87c74d0fd36a58ac4cb602095a0 /db | |
parent | 4aab52b20f9af44f03f2452875eb43bb40f2a6c6 (diff) | |
download | gitlab-ce-2deeac56438a5056444563596433b7bb448160fe.tar.gz |
Use batching to clear orphans in head_pipeline migrationbackstage/gb/add-index-and-foreign-key-to-merge-requests
Diffstat (limited to 'db')
-rw-r--r-- | db/migrate/20170713104829_add_foreign_key_to_merge_requests.rb | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/db/migrate/20170713104829_add_foreign_key_to_merge_requests.rb b/db/migrate/20170713104829_add_foreign_key_to_merge_requests.rb index 8a4e1062314..c25d4fd5986 100644 --- a/db/migrate/20170713104829_add_foreign_key_to_merge_requests.rb +++ b/db/migrate/20170713104829_add_foreign_key_to_merge_requests.rb @@ -5,15 +5,24 @@ class AddForeignKeyToMergeRequests < ActiveRecord::Migration disable_ddl_transaction! + class MergeRequest < ActiveRecord::Base + self.table_name = 'merge_requests' + include ::EachBatch + end + def up - execute <<-SQL.strip_heredoc - UPDATE merge_requests SET head_pipeline_id = null - WHERE NOT EXISTS ( + scope = <<-SQL.strip_heredoc + head_pipeline_id IS NOT NULL + AND NOT EXISTS ( SELECT 1 FROM ci_pipelines WHERE ci_pipelines.id = merge_requests.head_pipeline_id ) SQL + MergeRequest.where(scope).each_batch(of: 1000) do |merge_requests| + merge_requests.update_all(head_pipeline_id: nil) + end + unless foreign_key_exists?(:merge_requests, :head_pipeline_id) add_concurrent_foreign_key(:merge_requests, :ci_pipelines, column: :head_pipeline_id, on_delete: :nullify) |