diff options
author | Andreas Brandl <abrandl@gitlab.com> | 2018-04-20 17:00:15 +0300 |
---|---|---|
committer | Yorick Peterse - OOO until May 4th <yorickpeterse@gmail.com> | 2018-04-20 17:00:15 +0300 |
commit | 9ea2fc85a3387e7bbb10558b3f2302f05abe5b64 (patch) | |
tree | 3a56fa83b93423314915c65d6ae549c98d78cacc /db/migrate/20180417090132_add_index_constraints_to_internal_id_table.rb | |
parent | 87e592dc0a81d94efac157dfd7382df4e5b2a223 (diff) |
Atomic internal ids for all models
Diffstat (limited to 'db/migrate/20180417090132_add_index_constraints_to_internal_id_table.rb')
-rw-r--r-- | db/migrate/20180417090132_add_index_constraints_to_internal_id_table.rb | 40 |
1 files changed, 40 insertions, 0 deletions
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 |