add uniqueness constraint on user names
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
class AddUniqueNameConstraintToUsers < ActiveRecord::Migration
|
||||||
|
def up
|
||||||
|
remove_index :users, :name
|
||||||
|
execute "create unique index index_users_on_name on users(lower(name))"
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -7579,3 +7579,5 @@ INSERT INTO schema_migrations (version) VALUES ('20170330230231');
|
|||||||
|
|
||||||
INSERT INTO schema_migrations (version) VALUES ('20170413000209');
|
INSERT INTO schema_migrations (version) VALUES ('20170413000209');
|
||||||
|
|
||||||
|
INSERT INTO schema_migrations (version) VALUES ('20170414005856');
|
||||||
|
|
||||||
|
|||||||
15
script/fixes/045_dedup_users.rb
Normal file
15
script/fixes/045_dedup_users.rb
Normal 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
|
||||||
Reference in New Issue
Block a user