Defect #43838 » 0002-Add-an-index-to-users-login-to-speed-up-find-user-by.patch
| app/models/user.rb | ||
|---|---|---|
| 546 | 546 |
def self.find_by_login(login) |
| 547 | 547 |
login = Redmine::CodesetUtil.replace_invalid_utf8(login.to_s) |
| 548 | 548 |
if login.present? |
| 549 |
users = where(:login => login) |
|
| 549 | 550 |
# First look for an exact match |
| 550 |
user = where(:login => login).detect {|u| u.login == login}
|
|
| 551 |
user = users.detect {|u| u.login == login}
|
|
| 551 | 552 |
unless user |
| 552 | 553 |
# Fail over to case-insensitive if none was found |
| 553 |
user = find_by("LOWER(login) = ?", login.downcase)
|
|
| 554 |
if Redmine::Database.mysql? || Redmine::Database.sqlserver? |
|
| 555 |
# MySQL and SQLServer are case-insensitive by default, we can search in the existing results |
|
| 556 |
user = users.detect {|u| u.login.casecmp?(login)}
|
|
| 557 |
else |
|
| 558 |
user = find_by("LOWER(login) = ?", login.downcase)
|
|
| 559 |
end |
|
| 554 | 560 |
end |
| 555 | 561 |
user |
| 556 | 562 |
end |
| db/migrate/20260225170822_add_index_to_users_login.rb | ||
|---|---|---|
| 1 |
class AddIndexToUsersLogin < ActiveRecord::Migration[8.1] |
|
| 2 |
def up |
|
| 3 |
add_index :users, :login |
|
| 4 |
end |
|
| 5 | ||
| 6 |
def down |
|
| 7 |
remove_index :users, :login |
|
| 8 |
end |
|
| 9 |
end |
|
| lib/redmine/database.rb | ||
|---|---|---|
| 65 | 65 |
mysql? ? ActiveRecord::Base.connection.select_value("SELECT VERSION()") : nil
|
| 66 | 66 |
end |
| 67 | 67 | |
| 68 |
def sqlserver? |
|
| 69 |
/sqlserver/i.match?(ActiveRecord::Base.connection.adapter_name) |
|
| 70 |
end |
|
| 71 | ||
| 68 | 72 |
# Returns a SQL statement for case/accent (if possible) insensitive match |
| 69 | 73 |
def like(left, right, options={})
|
| 70 | 74 |
neg = (options[:match] == false ? 'NOT ' : '') |
- « Previous
- 1
- …
- 4
- 5
- 6
- Next »