about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFire Demon <firedemon@creature.cafe>2020-09-07 19:05:37 -0500
committerFire Demon <firedemon@creature.cafe>2020-09-08 03:37:07 -0500
commit57f74d8da4f4906fde07caa6cb05cbb824edcbc4 (patch)
treed90c6c273cab6f31b3a93d428c15903025fade51
parentc05f4a88260e5aeb75be6a4121e812f6669e52b4 (diff)
[Database, Models] Add indexes to users.username and User.find_by_lower_username utility methods
-rw-r--r--app/models/user.rb11
-rw-r--r--db/migrate/20200907195410_add_index_to_users_username.rb8
-rw-r--r--db/schema.rb4
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|