summaryrefslogtreecommitdiff
path: root/db/migrate/20171114161914_merge_requests_source_project_id_foreign_key.rb
diff options
context:
space:
mode:
Diffstat (limited to 'db/migrate/20171114161914_merge_requests_source_project_id_foreign_key.rb')
-rw-r--r--db/migrate/20171114161914_merge_requests_source_project_id_foreign_key.rb45
1 files changed, 45 insertions, 0 deletions
diff --git a/db/migrate/20171114161914_merge_requests_source_project_id_foreign_key.rb b/db/migrate/20171114161914_merge_requests_source_project_id_foreign_key.rb
new file mode 100644
index 00000000000..2965e580c84
--- /dev/null
+++ b/db/migrate/20171114161914_merge_requests_source_project_id_foreign_key.rb
@@ -0,0 +1,45 @@
+# See http://doc.gitlab.com/ce/development/migration_style_guide.html
+# for more information on how to write migrations for GitLab.
+
+class MergeRequestsSourceProjectIdForeignKey < 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_source_projects
+ where('NOT EXISTS (SELECT true FROM projects WHERE merge_requests.source_project_id = projects.id)')
+ .where('source_project_id IS NOT NULL')
+ end
+ end
+
+ def up
+ MergeRequest.with_orphaned_source_projects.each_batch(of: 100) do |batch|
+ batch.update_all(source_project_id: nil)
+ end
+
+ # We need to allow NULL values so we can nullify the column when the source
+ # project is removed. We _don't_ want to remove the merge request, instead
+ # the application will keep them but close them.
+ change_column_null(:merge_requests, :source_project_id, true)
+
+ add_concurrent_foreign_key(
+ :merge_requests,
+ :projects,
+ column: :source_project_id,
+ on_delete: :nullify
+ )
+ end
+
+ def down
+ remove_foreign_key_without_error(:merge_requests, column: :source_project_id)
+ change_column_null(:merge_requests, :source_project_id, false)
+ end
+end