-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
user emails must be present and unique
- Loading branch information
1 parent
2e61d2b
commit 51a3a98
Showing
4 changed files
with
69 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
namespace :users do | ||
task :remove_null_emails => :environment do | ||
team_user = User.find_by_email("[email protected]") | ||
count_team_devices = team_user.devices.length | ||
count_users = User.count | ||
count_moved_devices = 0 | ||
count_deleted_users = 0 | ||
User.where("email IS NULL or trim(email) = ''").each do |user| | ||
puts "user #{user.id} (#{user.username}) has blank email, moving devices to team account:" | ||
user.devices.each do |device| | ||
device.owner = team_user | ||
device.save(validate: false) | ||
count_team_devices += 1 | ||
count_moved_devices += 1 | ||
puts "\t - Device #{device.id} moved to user: #{team_user.id}" | ||
end | ||
user.destroy! | ||
count_users -= 1 | ||
count_deleted_users += 1 | ||
end | ||
puts "Check moved devices: #{team_user.reload.devices.length} (should be #{count_team_devices}, #{count_moved_devices} moved)" | ||
puts "Check remaining users: #{User.count} (should be #{count_users}, #{count_deleted_users} deleted)" | ||
end | ||
|
||
task :deduplicate_emails => :environment do | ||
count_deleted_users = 0 | ||
count_users = User.count | ||
count_devices = Device.count | ||
count_moved_devices = 0 | ||
ActiveRecord::Base.connection.execute( | ||
"SELECT email FROM users GROUP BY email HAVING count(id) > 1" | ||
).each do |record| | ||
email = record["email"] | ||
users = User.where(email: email).to_a | ||
puts "Found #{users.length} users for email: #{email}: (#{users.map(&:id).join(",")})" | ||
move_to_user = users.shift | ||
puts " - Moving devices from users (#{users.map(&:id).join(",")}) to user #{move_to_user.id}" | ||
users.each do |user| | ||
user.devices.each do |device| | ||
device.owner = move_to_user | ||
device.save(validate: false) | ||
count_moved_devices += 1 | ||
puts " - Moved device #{device.id} to user: #{move_to_user.id}" | ||
end | ||
end | ||
puts " - Deleting users: #{users.map(&:id).join(",")}" | ||
users.each do |user| | ||
user.destroy! | ||
count_deleted_users += 1 | ||
end | ||
end | ||
puts "Check moved devices: #{Device.count} (should be #{count_devices}, #{count_moved_devices} moved)" | ||
puts "Check deleted users: #{User.count} (should be #{count_users - count_deleted_users}, #{count_deleted_users} deleted)" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,19 @@ | |
expect(build(:user, avatar: 'http://i.imgur.com/SZD8ADL.JPEG')).to be_valid | ||
end | ||
|
||
it "is invalid with an email which isn't an email adddress" do | ||
expect(build(:user, email: "not an email")).to be_invalid | ||
end | ||
|
||
it "is invalid without an email" do | ||
expect(build(:user, email: nil)).to be_invalid | ||
end | ||
|
||
it "is invalid with an email which is already taken" do | ||
create(:user, email: "[email protected]") | ||
expect(build(:user, email: "[email protected]")).to be_invalid | ||
end | ||
|
||
it "has a location" do | ||
user = create(:user, country_code: 'es', city: 'Barcelona') | ||
expect(user.country.to_s).to eq("Spain") | ||
|