diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2017-11-14 18:58:56 +0300 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2017-11-20 14:45:33 +0300 |
commit | 936e9e895000c9f9ae39713c969b325f6b59c0c3 (patch) | |
tree | 344efe089e606db31edea70a63140162a9c5e78b /db/migrate/20171114160005_merge_requests_assignee_id_foreign_key.rb | |
parent | 202ab62874bb3d828ab48017550660d461e8cf6b (diff) |
Clean up schema of the "merge_requests" table
This adds various foreign keys and indexes to the "merge_requests" table
as outlined in https://gitlab.com/gitlab-org/gitlab-ce/issues/31825.
Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/31825
Diffstat (limited to 'db/migrate/20171114160005_merge_requests_assignee_id_foreign_key.rb')
-rw-r--r-- | db/migrate/20171114160005_merge_requests_assignee_id_foreign_key.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/db/migrate/20171114160005_merge_requests_assignee_id_foreign_key.rb b/db/migrate/20171114160005_merge_requests_assignee_id_foreign_key.rb new file mode 100644 index 00000000000..1a242f01051 --- /dev/null +++ b/db/migrate/20171114160005_merge_requests_assignee_id_foreign_key.rb @@ -0,0 +1,39 @@ +# See http://doc.gitlab.com/ce/development/migration_style_guide.html +# for more information on how to write migrations for GitLab. + +class MergeRequestsAssigneeIdForeignKey < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + # Set this constant to true if this migration requires downtime. + DOWNTIME = false + + disable_ddl_transaction! + + class MergeRequest < ActiveRecord::Base + include EachBatch + + self.table_name = 'merge_requests' + + def self.with_orphaned_assignees + where('NOT EXISTS (SELECT true FROM users WHERE merge_requests.assignee_id = users.id)') + .where('assignee_id IS NOT NULL') + end + end + + def up + MergeRequest.with_orphaned_assignees.each_batch(of: 100) do |batch| + batch.update_all(assignee_id: nil) + end + + add_concurrent_foreign_key( + :merge_requests, + :users, + column: :assignee_id, + on_delete: :nullify + ) + end + + def down + remove_foreign_key(:merge_requests, column: :assignee_id) + end +end |