Commit 088735b39ae92117aa914c2116f5410892cf89dd
Exists in
master
and in
22 other branches
Merge branch 'tags-case' into master
See merge request !563
Showing
1 changed file
with
19 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,19 @@ | @@ -0,0 +1,19 @@ | ||
1 | +class FixTagsCaseDifferences < ActiveRecord::Migration | ||
2 | + def up | ||
3 | + tags = ActsAsTaggableOn::Tag.joins('LEFT JOIN tags as b on LOWER(tags.name) = b.name').where('b.id is null') | ||
4 | + tags.find_each do |tag| | ||
5 | + unless ActsAsTaggableOn::Tag.exists?(:name => tag.name.mb_chars.downcase) | ||
6 | + ActsAsTaggableOn::Tag.create(:name => tag.name.mb_chars.downcase) | ||
7 | + end | ||
8 | + end | ||
9 | + | ||
10 | + execute("UPDATE taggings SET tag_id = new.id FROM taggings AS t INNER JOIN tags AS old ON t.tag_id = old.id INNER JOIN tags AS new ON LOWER(old.name) = new.name WHERE old.id != new.id AND taggings.id = t.id") | ||
11 | + | ||
12 | + execute("UPDATE tags SET taggings_count = (SELECT COUNT(*) FROM taggings WHERE taggings.tag_id = tags.id)") | ||
13 | + execute("DELETE FROM tags WHERE taggings_count = 0") | ||
14 | + end | ||
15 | + | ||
16 | + def down | ||
17 | + say 'This migration is irreversible.' | ||
18 | + end | ||
19 | +end |