summaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-05-08 16:39:29 +0200
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-05-08 16:39:29 +0200
commit479a15e722d45172dbd7c47ef4a3c4fae9051fbe (patch)
treeb6ca35290a543441593c83eb08c7b63d84f4c65a /db
parent0a46bf70b658e0a63fe980b77d215c8a9f4f4dc5 (diff)
downloadgitlab-ce-479a15e722d45172dbd7c47ef4a3c4fae9051fbe.tar.gz
Add database foreign key between pipelines and builds
Diffstat (limited to 'db')
-rw-r--r--db/migrate/20180420010016_add_pipeline_build_foreign_key.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/db/migrate/20180420010016_add_pipeline_build_foreign_key.rb b/db/migrate/20180420010016_add_pipeline_build_foreign_key.rb
new file mode 100644
index 00000000000..385d51b8da6
--- /dev/null
+++ b/db/migrate/20180420010016_add_pipeline_build_foreign_key.rb
@@ -0,0 +1,24 @@
+class AddPipelineBuildForeignKey < ActiveRecord::Migration
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+
+ disable_ddl_transaction!
+
+ def up
+ execute <<~SQL
+ DELETE FROM ci_builds WHERE NOT EXISTS
+ (SELECT true FROM ci_pipelines WHERE ci_pipelines.id = ci_builds.commit_id)
+ SQL
+
+ return if foreign_key_exists?(:ci_builds, :ci_pipelines, column: :commit_id)
+
+ add_concurrent_foreign_key(:ci_builds, :ci_pipelines, column: :commit_id)
+ end
+
+ def down
+ return unless foreign_key_exists?(:ci_builds, :ci_pipelines, column: :commit_id)
+
+ remove_foreign_key(:ci_builds, column: :commit_id)
+ end
+end