diff options
author | Andreas Brandl <abrandl@gitlab.com> | 2018-01-31 10:53:03 +0100 |
---|---|---|
committer | Andreas Brandl <abrandl@gitlab.com> | 2018-01-31 22:37:44 +0100 |
commit | b4d8facb14520b2c98cf7313ca47fddc42561a4b (patch) | |
tree | 560c81e62b281d02ac3ac9f9b2b0705698030caa | |
parent | 37e81f545b44bbbbbe440b4481f0b63b7a3505d2 (diff) | |
download | gitlab-ce-b4d8facb14520b2c98cf7313ca47fddc42561a4b.tar.gz |
Add missing unique constraint to trending_projects.
-rw-r--r-- | db/migrate/20180131101040_add_unique_constraint_to_trending_projects_project_id.rb | 43 | ||||
-rw-r--r-- | db/schema.rb | 6 |
2 files changed, 46 insertions, 3 deletions
diff --git a/db/migrate/20180131101040_add_unique_constraint_to_trending_projects_project_id.rb b/db/migrate/20180131101040_add_unique_constraint_to_trending_projects_project_id.rb new file mode 100644 index 00000000000..a7a346743b9 --- /dev/null +++ b/db/migrate/20180131101040_add_unique_constraint_to_trending_projects_project_id.rb @@ -0,0 +1,43 @@ +class AddUniqueConstraintToTrendingProjectsProjectId < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + # Don't drop-and-create index so we don't have a gap + # Instead: + # 1) Add new index with temporary name + # 2) Drop old index + # 3) Rename new index back to original name + + def up + add_concurrent_index :trending_projects, :project_id, unique: true, name: temp_index_name + + remove_foreign_key :trending_projects, :projects + remove_concurrent_index :trending_projects, :project_id, name: actual_index_name + add_concurrent_foreign_key :trending_projects, :projects, column: :project_id, on_delete: :cascade + + rename_index :trending_projects, temp_index_name, actual_index_name + end + + def down + add_concurrent_index :trending_projects, :project_id, name: temp_index_name + + remove_foreign_key :trending_projects, :projects + remove_concurrent_index :trending_projects, :project_id, name: actual_index_name + add_concurrent_foreign_key :trending_projects, :projects, column: :project_id, on_delete: :cascade + + rename_index :trending_projects, temp_index_name, actual_index_name + end + + private + + def temp_index_name + 'index_trending_projects_on_project_id_new' + end + + def actual_index_name + 'index_trending_projects_on_project_id' + end +end diff --git a/db/schema.rb b/db/schema.rb index 4e82a688725..e7f0401031b 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: 20180115201419) do +ActiveRecord::Schema.define(version: 20180131101040) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -1727,7 +1727,7 @@ ActiveRecord::Schema.define(version: 20180115201419) do t.integer "project_id", null: false end - add_index "trending_projects", ["project_id"], name: "index_trending_projects_on_project_id", using: :btree + add_index "trending_projects", ["project_id"], name: "index_trending_projects_on_project_id", unique: true, using: :btree create_table "u2f_registrations", force: :cascade do |t| t.text "certificate" @@ -2038,7 +2038,7 @@ ActiveRecord::Schema.define(version: 20180115201419) do add_foreign_key "timelogs", "issues", name: "fk_timelogs_issues_issue_id", on_delete: :cascade add_foreign_key "timelogs", "merge_requests", name: "fk_timelogs_merge_requests_merge_request_id", on_delete: :cascade add_foreign_key "todos", "projects", name: "fk_45054f9c45", on_delete: :cascade - add_foreign_key "trending_projects", "projects", on_delete: :cascade + add_foreign_key "trending_projects", "projects", name: "fk_09feecd872", on_delete: :cascade add_foreign_key "u2f_registrations", "users" add_foreign_key "user_custom_attributes", "users", on_delete: :cascade add_foreign_key "user_synced_attributes_metadata", "users", on_delete: :cascade |