Skip to content
This repository was archived by the owner on Dec 21, 2023. It is now read-only.

Commit c6904c0

Browse files
Fix unique username constraint for local users not being enforced in database (mastodon#14099)
This should not be an issue in practice because of the Rails-level uniqueness check, but local accounts having a NULL domain means the uniqueness constraint did not apply to them (since no two NULL values are considered equal).
1 parent 75a2b8f commit c6904c0

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class AddFixedLowercaseIndexToAccounts < ActiveRecord::Migration[5.2]
2+
disable_ddl_transaction!
3+
4+
def up
5+
rename_index :accounts, 'index_accounts_on_username_and_domain_lower', 'old_index_accounts_on_username_and_domain_lower' unless index_name_exists?(:accounts, 'old_index_accounts_on_username_and_domain_lower')
6+
add_index :accounts, "lower (username), COALESCE(lower(domain), '')", name: 'index_accounts_on_username_and_domain_lower', unique: true, algorithm: :concurrently
7+
remove_index :accounts, name: 'old_index_accounts_on_username_and_domain_lower'
8+
end
9+
10+
def down
11+
add_index :accounts, 'lower (username), lower(domain)', name: 'old_index_accounts_on_username_and_domain_lower', unique: true, algorithm: :concurrently
12+
remove_index :accounts, name: 'index_accounts_on_username_and_domain_lower'
13+
rename_index :accounts, 'old_index_accounts_on_username_and_domain_lower', 'index_accounts_on_username_and_domain_lower'
14+
end
15+
end

db/schema.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2020_06_08_113046) do
13+
ActiveRecord::Schema.define(version: 2020_06_20_164023) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -176,7 +176,7 @@
176176
t.integer "header_storage_schema_version"
177177
t.string "devices_url"
178178
t.index "(((setweight(to_tsvector('simple'::regconfig, (display_name)::text), 'A'::\"char\") || setweight(to_tsvector('simple'::regconfig, (username)::text), 'B'::\"char\")) || setweight(to_tsvector('simple'::regconfig, (COALESCE(domain, ''::character varying))::text), 'C'::\"char\")))", name: "search_index", using: :gin
179-
t.index "lower((username)::text), lower((domain)::text)", name: "index_accounts_on_username_and_domain_lower", unique: true
179+
t.index "lower((username)::text), COALESCE(lower((domain)::text), ''::text)", name: "index_accounts_on_username_and_domain_lower", unique: true
180180
t.index ["moved_to_account_id"], name: "index_accounts_on_moved_to_account_id"
181181
t.index ["uri"], name: "index_accounts_on_uri"
182182
t.index ["url"], name: "index_accounts_on_url"

0 commit comments

Comments
 (0)