diff options
-rw-r--r-- | app/models/user.rb | 11 | ||||
-rw-r--r-- | db/migrate/20200907195410_add_index_to_users_username.rb | 8 | ||||
-rw-r--r-- | db/schema.rb | 4 |
3 files changed, 22 insertions, 1 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index 6fa6bb369..d20922993 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -101,6 +101,7 @@ class User < ApplicationRecord scope :matches_email, ->(value) { where(arel_table[:email].matches("#{value}%")) } scope :matches_ip, ->(value) { left_joins(:session_activations).where('users.current_sign_in_ip <<= ?', value).or(left_joins(:session_activations).where('users.last_sign_in_ip <<= ?', value)).or(left_joins(:session_activations).where('session_activations.ip <<= ?', value)) } scope :emailable, -> { confirmed.enabled.joins(:account).merge(Account.searchable) } + scope :lower_username, ->(username) { where('lower(users.username) = lower(?)', username) } before_validation :sanitize_languages before_create :set_approved @@ -465,4 +466,14 @@ class User < ApplicationRecord value = [account.username, username.downcase, email, invite_request.text].compact.map(&:downcase).join("\u{F0666}") Digest::SHA512.hexdigest(value).upcase end + + class << self + def find_by_lower_username(username) + lower_username(username).first + end + + def find_by_lower_username!(username) + lower_username(username).first! + end + end end diff --git a/db/migrate/20200907195410_add_index_to_users_username.rb b/db/migrate/20200907195410_add_index_to_users_username.rb new file mode 100644 index 000000000..06452e0dd --- /dev/null +++ b/db/migrate/20200907195410_add_index_to_users_username.rb @@ -0,0 +1,8 @@ +class AddIndexToUsersUsername < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + add_index :users, :username, unique: true, algorithm: :concurrently + add_index :users, 'lower(username)', unique: true, algorithm: :concurrently, name: 'index_on_users_username_lowercase' + end +end diff --git a/db/schema.rb b/db/schema.rb index 87cca4ea1..fbcf8e636 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_09_04_201028) do +ActiveRecord::Schema.define(version: 2020_09_07_195410) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -990,12 +990,14 @@ ActiveRecord::Schema.define(version: 2020_09_04_201028) do t.string "username" t.string "kobold" t.string "webauthn_id" + t.index "lower((username)::text)", name: "index_on_users_username_lowercase", unique: true t.index ["account_id"], name: "index_users_on_account_id" t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["created_by_application_id"], name: "index_users_on_created_by_application_id" t.index ["email"], name: "index_users_on_email", unique: true t.index ["remember_token"], name: "index_users_on_remember_token", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true + t.index ["username"], name: "index_users_on_username", unique: true end create_table "web_push_subscriptions", force: :cascade do |t| |