diff options
author | Valery Sizov <valery@gitlab.com> | 2015-05-08 08:30:05 +0000 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2015-05-08 12:28:53 +0300 |
commit | 708b507328a8efefeef71b108f9067e822896587 (patch) | |
tree | 79633e5c89a57c11e0d0031d9aacb7f5fee55184 | |
parent | 87dd2dca604807257e25c580e5a05e4eb5d5d50a (diff) | |
download | gitlab-ce-708b507328a8efefeef71b108f9067e822896587.tar.gz |
Merge branch 'tag_dup' into 'master'
Remove tag duplicates
Prevention of getting this error:
```
ActiveRecord::RecordNotUnique: PG::Error: ERROR: could not create unique index "index_tags_on_name"
DETAIL: Key (name)=(XSS') is duplicated.
: CREATE UNIQUE INDEX "index_tags_on_name" ON "tags" ("name")
```
This migration fails if there are duplicates:
https://github.com/mbleigh/acts-as-taggable-on/blob/v3.5.0/db/migrate/2_add_missing_unique_indices.rb#L3
See merge request !623
-rw-r--r-- | db/migrate/20150425164647_remove_duplicate_tags.rb | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/db/migrate/20150425164647_remove_duplicate_tags.rb b/db/migrate/20150425164647_remove_duplicate_tags.rb new file mode 100644 index 00000000000..1a9152cb965 --- /dev/null +++ b/db/migrate/20150425164647_remove_duplicate_tags.rb @@ -0,0 +1,16 @@ +class RemoveDuplicateTags < ActiveRecord::Migration + def up + select_all("SELECT name, COUNT(id) as cnt FROM tags GROUP BY name HAVING COUNT(id) > 1").each do |tag| + duplicate_ids = select_all("SELECT id FROM tags WHERE name = '#{tag["name"]}'").map{|tag| tag["id"]} + origin_tag_id = duplicate_ids.first + duplicate_ids.delete origin_tag_id + + execute("UPDATE taggings SET tag_id = #{origin_tag_id} WHERE tag_id IN(#{duplicate_ids.join(",")})") + execute("DELETE FROM tags WHERE id IN(#{duplicate_ids.join(",")})") + end + end + + def down + + end +end |