Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-05-08 17:39:29 +0300
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-05-08 17:39:29 +0300
commit479a15e722d45172dbd7c47ef4a3c4fae9051fbe (patch)
treeb6ca35290a543441593c83eb08c7b63d84f4c65a /db
parent0a46bf70b658e0a63fe980b77d215c8a9f4f4dc5 (diff)
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