diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2016-06-20 16:39:39 +0300 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2016-06-20 17:16:38 +0300 |
commit | dc2670f2d2aaa28d2d9975fb5c6c724b4d1d62c9 (patch) | |
tree | 59e7a87bfe30943f2a808b80156bd5bd203d0615 /db | |
parent | 95aaf66ec3d2852c75451ba32123568b2bbff384 (diff) |
Merge branch '18697-uniqueness-key-validation' into 'master'
Remove Duplicated keys adding UNIQUE index to fingerprint
See merge request !4787
Diffstat (limited to 'db')
-rw-r--r-- | db/migrate/20160616102642_remove_duplicated_keys.rb | 19 | ||||
-rw-r--r-- | db/migrate/20160616103005_remove_keys_fingerprint_index_if_exists.rb | 21 | ||||
-rw-r--r-- | db/migrate/20160616103948_add_unique_index_to_keys_fingerprint.rb | 13 | ||||
-rw-r--r-- | db/schema.rb | 3 |
4 files changed, 55 insertions, 1 deletions
diff --git a/db/migrate/20160616102642_remove_duplicated_keys.rb b/db/migrate/20160616102642_remove_duplicated_keys.rb new file mode 100644 index 00000000000..00a45d7fe73 --- /dev/null +++ b/db/migrate/20160616102642_remove_duplicated_keys.rb @@ -0,0 +1,19 @@ +# rubocop:disable all +class RemoveDuplicatedKeys < ActiveRecord::Migration + def up + select_all("SELECT fingerprint FROM #{quote_table_name(:keys)} GROUP BY fingerprint HAVING COUNT(*) > 1").each do |row| + fingerprint = connection.quote(row['fingerprint']) + execute(%Q{ + DELETE FROM keys + WHERE fingerprint = #{fingerprint} + AND id != ( + SELECT id FROM ( + SELECT max(id) AS id + FROM keys + WHERE fingerprint = #{fingerprint} + ) max_ids + ) + }) + end + end +end diff --git a/db/migrate/20160616103005_remove_keys_fingerprint_index_if_exists.rb b/db/migrate/20160616103005_remove_keys_fingerprint_index_if_exists.rb new file mode 100644 index 00000000000..4bb4204cebd --- /dev/null +++ b/db/migrate/20160616103005_remove_keys_fingerprint_index_if_exists.rb @@ -0,0 +1,21 @@ +class RemoveKeysFingerprintIndexIfExists < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + disable_ddl_transaction! + + # https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/250 + # That MR was added on gitlab-ee so we need to check if the index + # already exists because we want to do is create an unique index instead. + + def up + if index_exists?(:keys, :fingerprint) + remove_index :keys, :fingerprint + end + end + + def down + unless index_exists?(:keys, :fingerprint) + add_concurrent_index :keys, :fingerprint + end + end +end diff --git a/db/migrate/20160616103948_add_unique_index_to_keys_fingerprint.rb b/db/migrate/20160616103948_add_unique_index_to_keys_fingerprint.rb new file mode 100644 index 00000000000..e35af38aac3 --- /dev/null +++ b/db/migrate/20160616103948_add_unique_index_to_keys_fingerprint.rb @@ -0,0 +1,13 @@ +class AddUniqueIndexToKeysFingerprint < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + disable_ddl_transaction! + + def up + add_concurrent_index :keys, :fingerprint, unique: true + end + + def down + remove_index :keys, :fingerprint + end +end diff --git a/db/schema.rb b/db/schema.rb index 5a27e9d5cdc..4c1eb8cc1b3 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: 20160616084004) do +ActiveRecord::Schema.define(version: 20160616103948) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -507,6 +507,7 @@ ActiveRecord::Schema.define(version: 20160616084004) do end add_index "keys", ["created_at", "id"], name: "index_keys_on_created_at_and_id", using: :btree + add_index "keys", ["fingerprint"], name: "index_keys_on_fingerprint", unique: true, using: :btree add_index "keys", ["user_id"], name: "index_keys_on_user_id", using: :btree create_table "label_links", force: :cascade do |t| |