add uniqueness constraint on user names

This commit is contained in:
r888888888
2017-04-13 18:04:06 -07:00
parent 62fde705ce
commit 76b5031bbf
3 changed files with 23 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment'))
ActiveRecord::Base.connection.execute("set statement_timeout = 0")
candidates = User.group("lower(name)").having("count(*) > 1").pluck("lower(name)")
candidates.each do |name|
users = User.where("lower(name) = ?", name).order("id").to_a
users.slice(1, 100).each do |user|
user.name = "dup_#{user.name}_#{user.id}"
user.save!
end
end