2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
class ResolveDuplicateGroupNames < ActiveRecord::Migration[4.2]
|
2014-12-16 20:07:15 +08:00
|
|
|
|
|
|
|
def up
|
2018-06-19 14:13:14 +08:00
|
|
|
results = DB.query_single 'SELECT id FROM groups
|
2014-12-16 20:07:15 +08:00
|
|
|
WHERE name ILIKE
|
|
|
|
(SELECT lower(name)
|
|
|
|
FROM groups
|
|
|
|
GROUP BY lower(name)
|
|
|
|
HAVING count(*) > 1);'
|
|
|
|
|
2019-12-10 08:48:27 +08:00
|
|
|
groups = Group.where id: results
|
2014-12-16 20:07:15 +08:00
|
|
|
groups.group_by { |g| g.name.downcase }.each do |key, value|
|
|
|
|
value.each_with_index do |dup, index|
|
2017-07-28 09:20:09 +08:00
|
|
|
dup.update! name: "#{dup.name[0..18]}_#{index + 1}" if index > 0
|
2014-12-16 20:07:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
# does not reverse changes
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|