mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 05:02:24 +08:00
30990006a9
This reduces chances of errors where consumers of strings mutate inputs and reduces memory usage of the app. Test suite passes now, but there may be some stuff left, so we will run a few sites on a branch prior to merging
29 lines
855 B
Ruby
29 lines
855 B
Ruby
# frozen_string_literal: true
|
|
|
|
class UpdateUsersCaseInsensitiveEmails < ActiveRecord::Migration[4.2]
|
|
def up
|
|
execute "DROP INDEX index_users_on_email"
|
|
|
|
# Find duplicate emails.
|
|
results = execute <<SQL
|
|
SELECT id, email, count
|
|
FROM (SELECT id, email,
|
|
row_number() OVER(PARTITION BY lower(email) ORDER BY id asc) AS count
|
|
FROM users) dups
|
|
WHERE dups.count > 1
|
|
SQL
|
|
|
|
results.each do |row|
|
|
execute "UPDATE users SET email = '#{row['email'].downcase}#{row['count']}' WHERE id = #{row['id']}"
|
|
end
|
|
|
|
execute "UPDATE users SET email = lower(email)"
|
|
execute "CREATE UNIQUE INDEX index_users_on_email ON users ((lower(email)));"
|
|
end
|
|
|
|
def down
|
|
execute "DROP INDEX index_users_on_email"
|
|
execute "CREATE UNIQUE INDEX index_users_on_email ON users (email);"
|
|
end
|
|
end
|