From b60717604c8f7b2cb089d4fd30cc282d4f77a85f Mon Sep 17 00:00:00 2001 From: Valery Sizov Date: Thu, 7 May 2015 18:17:58 +0300 Subject: [PATCH 1/3] remove tag duplicates --- .../20150425164647_remove_duplicate_tags.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 db/migrate/20150425164647_remove_duplicate_tags.rb diff --git a/db/migrate/20150425164647_remove_duplicate_tags.rb b/db/migrate/20150425164647_remove_duplicate_tags.rb new file mode 100644 index 0000000000..1a9152cb96 --- /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 From 548cec6ea46f0d66a60b564b2b72281b3c800fdd Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Thu, 7 May 2015 16:26:45 -0700 Subject: [PATCH 2/3] Change the MySQL collation type to utf8_bin to ensure case-sensitive tags are supported --- ...llation_for_tag_names.acts_as_taggable_on_engine.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 db/migrate/20150425164646_gitlab_change_collation_for_tag_names.acts_as_taggable_on_engine.rb diff --git a/db/migrate/20150425164646_gitlab_change_collation_for_tag_names.acts_as_taggable_on_engine.rb b/db/migrate/20150425164646_gitlab_change_collation_for_tag_names.acts_as_taggable_on_engine.rb new file mode 100644 index 0000000000..281c88d2a7 --- /dev/null +++ b/db/migrate/20150425164646_gitlab_change_collation_for_tag_names.acts_as_taggable_on_engine.rb @@ -0,0 +1,10 @@ +# This migration is a duplicate of 20150425164651_change_collation_for_tag_names.acts_as_taggable_on_engine.rb +# It shold be applied before the index additions to ensure that `name` is case sensitive. + +class GitlabChangeCollationForTagNames < ActiveRecord::Migration + def up + if ActsAsTaggableOn::Utils.using_mysql? + execute("ALTER TABLE tags MODIFY name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin;") + end + end +end From f6c0ef5553fde03ae3a04ba48733455a5415a3ac Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Fri, 8 May 2015 00:27:47 -0700 Subject: [PATCH 3/3] Conditionally remove indices that may not exist in pre-GitLab v6.7 installations Closes #1593 --- ...ssing_unique_indices.acts_as_taggable_on_engine.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/db/migrate/20150425164648_add_missing_unique_indices.acts_as_taggable_on_engine.rb b/db/migrate/20150425164648_add_missing_unique_indices.acts_as_taggable_on_engine.rb index 4ca676f6c7..c1b7868151 100644 --- a/db/migrate/20150425164648_add_missing_unique_indices.acts_as_taggable_on_engine.rb +++ b/db/migrate/20150425164648_add_missing_unique_indices.acts_as_taggable_on_engine.rb @@ -3,8 +3,15 @@ class AddMissingUniqueIndices < ActiveRecord::Migration def self.up add_index :tags, :name, unique: true - remove_index :taggings, :tag_id - remove_index :taggings, [:taggable_id, :taggable_type, :context] + # pre-GitLab v6.7.0 may not have these indices since there were no + # migrations for them + if index_exists?(:taggings, :tag_id) + remove_index :taggings, :tag_id + end + + if index_exists?(:taggings, [:taggable_id, :taggable_type, :context]) + remove_index :taggings, [:taggable_id, :taggable_type, :context] + end add_index :taggings, [:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type], unique: true, name: 'taggings_idx'