summaryrefslogtreecommitdiff
path: root/db/migrate/20170508190732_add_foreign_key_to_ci_variables.rb
diff options
context:
space:
mode:
Diffstat (limited to 'db/migrate/20170508190732_add_foreign_key_to_ci_variables.rb')
-rw-r--r--db/migrate/20170508190732_add_foreign_key_to_ci_variables.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/db/migrate/20170508190732_add_foreign_key_to_ci_variables.rb b/db/migrate/20170508190732_add_foreign_key_to_ci_variables.rb
new file mode 100644
index 00000000000..20ecaa2c36c
--- /dev/null
+++ b/db/migrate/20170508190732_add_foreign_key_to_ci_variables.rb
@@ -0,0 +1,24 @@
+class AddForeignKeyToCiVariables < ActiveRecord::Migration
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+
+ disable_ddl_transaction!
+
+ def up
+ execute <<~SQL
+ DELETE FROM ci_variables
+ WHERE NOT EXISTS (
+ SELECT true
+ FROM projects
+ WHERE projects.id = ci_variables.project_id
+ )
+ SQL
+
+ add_concurrent_foreign_key(:ci_variables, :projects, column: :project_id)
+ end
+
+ def down
+ remove_foreign_key(:ci_variables, column: :project_id)
+ end
+end