discourse/db/migrate/20141216112341_resolve_dupl...

Failed to ignore revisions in .git-blame-ignore-revs.

26 lines
706 B
Ruby
Raw Normal View History

# frozen_string_literal: true
class ResolveDuplicateGroupNames < ActiveRecord::Migration[4.2]
def up
results = DB.query_single 'SELECT id FROM groups
WHERE name ILIKE
(SELECT lower(name)
FROM groups
GROUP BY lower(name)
HAVING count(*) > 1);'
groups = Group.where id: results
groups.group_by { |g| g.name.downcase }.each do |key, value|
value.each_with_index do |dup, index|
2017-07-27 21:20:09 -04:00
dup.update! name: "#{dup.name[0..18]}_#{index + 1}" if index > 0
end
end
end
def down
# does not reverse changes
end
end