diff options
author | Z.J. van de Weg <git@zjvandeweg.nl> | 2017-05-08 21:52:27 +0200 |
---|---|---|
committer | Z.J. van de Weg <git@zjvandeweg.nl> | 2017-05-11 14:52:47 +0200 |
commit | 3eb8435fec3cd7311544a256a3205f31d1dc3a0c (patch) | |
tree | b365ae6f43343f352a7214ad85a31c82c862dc9d /db | |
parent | 383883382bac97477eb8448bef3ffac15d7ceacf (diff) | |
download | gitlab-ce-3eb8435fec3cd7311544a256a3205f31d1dc3a0c.tar.gz |
Add a foreign key to ci_variables to projects
Variables shouldn't exist without a project, so a foreign key was added
with CASCADE'ing effects.
Diffstat (limited to 'db')
-rw-r--r-- | db/migrate/20170508153950_add_not_null_contraints_to_ci_variables.rb (renamed from db/post_migrate/20170508153950_add_not_null_contraints_to_ci_variables.rb) | 0 | ||||
-rw-r--r-- | db/migrate/20170508190732_add_foreign_key_to_ci_variables.rb | 25 | ||||
-rw-r--r-- | db/schema.rb | 7 |
3 files changed, 29 insertions, 3 deletions
diff --git a/db/post_migrate/20170508153950_add_not_null_contraints_to_ci_variables.rb b/db/migrate/20170508153950_add_not_null_contraints_to_ci_variables.rb index 41c687a4f6e..41c687a4f6e 100644 --- a/db/post_migrate/20170508153950_add_not_null_contraints_to_ci_variables.rb +++ b/db/migrate/20170508153950_add_not_null_contraints_to_ci_variables.rb 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..13837324787 --- /dev/null +++ b/db/migrate/20170508190732_add_foreign_key_to_ci_variables.rb @@ -0,0 +1,25 @@ +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 + ) + OR ci_variables.project_id IS NULL + SQL + + add_concurrent_foreign_key(:ci_variables, :projects, column: :project_id) + end + + def down + remove_foreign_key(:ci_variables, column: :project_id) + end +end diff --git a/db/schema.rb b/db/schema.rb index 6eaab4832e9..60077ffd812 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170508170547) do +ActiveRecord::Schema.define(version: 20170508190732) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -347,12 +347,12 @@ ActiveRecord::Schema.define(version: 20170508170547) do add_index "ci_triggers", ["project_id"], name: "index_ci_triggers_on_project_id", using: :btree create_table "ci_variables", force: :cascade do |t| - t.string "key" + t.string "key", null: false t.text "value" t.text "encrypted_value" t.string "encrypted_value_salt" t.string "encrypted_value_iv" - t.integer "project_id" + t.integer "project_id", null: false end add_index "ci_variables", ["project_id"], name: "index_ci_variables_on_project_id", using: :btree @@ -1417,6 +1417,7 @@ ActiveRecord::Schema.define(version: 20170508170547) do add_foreign_key "ci_pipelines", "ci_pipelines", column: "auto_canceled_by_id", name: "fk_262d4c2d19", on_delete: :nullify add_foreign_key "ci_trigger_requests", "ci_triggers", column: "trigger_id", name: "fk_b8ec8b7245", on_delete: :cascade add_foreign_key "ci_triggers", "users", column: "owner_id", name: "fk_e8e10d1964", on_delete: :cascade + add_foreign_key "ci_variables", "projects", name: "fk_ada5eb64b3", on_delete: :cascade add_foreign_key "container_repositories", "projects" add_foreign_key "issue_assignees", "issues", on_delete: :cascade add_foreign_key "issue_assignees", "users", on_delete: :cascade |