diff options
| author | Andreas Brandl <abrandl@gitlab.com> | 2018-04-20 14:00:15 +0000 |
|---|---|---|
| committer | Yorick Peterse - OOO until May 4th <yorickpeterse@gmail.com> | 2018-04-20 14:00:15 +0000 |
| commit | 9ea2fc85a3387e7bbb10558b3f2302f05abe5b64 (patch) | |
| tree | 3a56fa83b93423314915c65d6ae549c98d78cacc /db | |
| parent | 87e592dc0a81d94efac157dfd7382df4e5b2a223 (diff) | |
| download | gitlab-ce-9ea2fc85a3387e7bbb10558b3f2302f05abe5b64.tar.gz | |
Atomic internal ids for all models
Diffstat (limited to 'db')
| -rw-r--r-- | db/migrate/20180416155103_add_further_scope_columns_to_internal_id_table.rb | 15 | ||||
| -rw-r--r-- | db/migrate/20180417090132_add_index_constraints_to_internal_id_table.rb | 40 | ||||
| -rw-r--r-- | db/schema.rb | 7 |
3 files changed, 60 insertions, 2 deletions
diff --git a/db/migrate/20180416155103_add_further_scope_columns_to_internal_id_table.rb b/db/migrate/20180416155103_add_further_scope_columns_to_internal_id_table.rb new file mode 100644 index 00000000000..37e2d19e022 --- /dev/null +++ b/db/migrate/20180416155103_add_further_scope_columns_to_internal_id_table.rb @@ -0,0 +1,15 @@ +class AddFurtherScopeColumnsToInternalIdTable < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + def up + change_column_null :internal_ids, :project_id, true + add_column :internal_ids, :namespace_id, :integer, null: true + end + + def down + change_column_null :internal_ids, :project_id, false + remove_column :internal_ids, :namespace_id + end +end diff --git a/db/migrate/20180417090132_add_index_constraints_to_internal_id_table.rb b/db/migrate/20180417090132_add_index_constraints_to_internal_id_table.rb new file mode 100644 index 00000000000..582b89a3948 --- /dev/null +++ b/db/migrate/20180417090132_add_index_constraints_to_internal_id_table.rb @@ -0,0 +1,40 @@ +class AddIndexConstraintsToInternalIdTable < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + def up + add_concurrent_index :internal_ids, [:usage, :namespace_id], unique: true, where: 'namespace_id IS NOT NULL' + + replace_index(:internal_ids, [:usage, :project_id], name: 'index_internal_ids_on_usage_and_project_id') do + add_concurrent_index :internal_ids, [:usage, :project_id], unique: true, where: 'project_id IS NOT NULL' + end + + add_concurrent_foreign_key :internal_ids, :namespaces, column: :namespace_id, on_delete: :cascade + end + + def down + remove_concurrent_index :internal_ids, [:usage, :namespace_id] + + replace_index(:internal_ids, [:usage, :project_id], name: 'index_internal_ids_on_usage_and_project_id') do + add_concurrent_index :internal_ids, [:usage, :project_id], unique: true + end + + remove_foreign_key :internal_ids, column: :namespace_id + end + + private + def replace_index(table, columns, name:) + temporary_name = "#{name}_old" + + if index_exists?(table, columns, name: name) + rename_index table, name, temporary_name + end + + yield + + remove_concurrent_index_by_name table, temporary_name + end +end diff --git a/db/schema.rb b/db/schema.rb index 1d272bdb779..df621956c80 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -896,12 +896,14 @@ ActiveRecord::Schema.define(version: 20180418053107) do add_index "identities", ["user_id"], name: "index_identities_on_user_id", using: :btree create_table "internal_ids", id: :bigserial, force: :cascade do |t| - t.integer "project_id", null: false + t.integer "project_id" t.integer "usage", null: false t.integer "last_value", null: false + t.integer "namespace_id" end - add_index "internal_ids", ["usage", "project_id"], name: "index_internal_ids_on_usage_and_project_id", unique: true, using: :btree + add_index "internal_ids", ["usage", "namespace_id"], name: "index_internal_ids_on_usage_and_namespace_id", unique: true, where: "(namespace_id IS NOT NULL)", using: :btree + add_index "internal_ids", ["usage", "project_id"], name: "index_internal_ids_on_usage_and_project_id", unique: true, where: "(project_id IS NOT NULL)", using: :btree create_table "issue_assignees", id: false, force: :cascade do |t| t.integer "user_id", null: false @@ -2113,6 +2115,7 @@ ActiveRecord::Schema.define(version: 20180418053107) do add_foreign_key "gpg_signatures", "gpg_keys", on_delete: :nullify add_foreign_key "gpg_signatures", "projects", on_delete: :cascade add_foreign_key "group_custom_attributes", "namespaces", column: "group_id", on_delete: :cascade + add_foreign_key "internal_ids", "namespaces", name: "fk_162941d509", on_delete: :cascade add_foreign_key "internal_ids", "projects", on_delete: :cascade add_foreign_key "issue_assignees", "issues", name: "fk_b7d881734a", on_delete: :cascade add_foreign_key "issue_assignees", "users", name: "fk_5e0c8d9154", on_delete: :cascade |
