Skip to content

Commit 5b0af70

Browse files
committed
chore: rename schema fields for consistency
- Adds unique user_id to mfa_recovery_code_sets as a workaround to avoid creating a unique index on mfa_factors (user_id) which would impact large tables. - Renames fields for consistency - Adds unique index on mfa_recovery_codes (mfa_recovery_code_set_id, code_hash) to prevent duplicate codes per set.
1 parent f612a02 commit 5b0af70

2 files changed

Lines changed: 6 additions & 10 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* auth_migration: 20260824000000 */
22
do $$ begin
3-
alter type {{ index .Options "Namespace" }}.factor_type add value 'recovery_codes';
3+
alter type {{ index .Options "Namespace" }}.factor_type add value 'recovery_code';
44
exception
55
when duplicate_object then null;
66
end $$;
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
-- Enforce exactly one recovery-codes factor per user.
2-
/* auth_migration: 20260824000001 */
3-
create unique index if not exists mfa_factors_user_recovery_codes_unique
4-
on {{ index .Options "Namespace" }}.mfa_factors (user_id) where factor_type = 'recovery_codes';
5-
61
/* auth_migration: 20260824000001 */
72
create table if not exists {{ index .Options "Namespace" }}.mfa_recovery_code_sets (
83
id uuid primary key,
9-
factor_id uuid not null unique references {{ index .Options "Namespace" }}.mfa_factors (id) on delete cascade,
4+
user_id uuid not null unique references {{ index .Options "Namespace" }}.users (id) on delete cascade,
5+
mfa_factor_id uuid not null unique references {{ index .Options "Namespace" }}.mfa_factors (id) on delete cascade,
106
failed_verification_count integer not null default 0 check (failed_verification_count >= 0),
117
verification_locked_until timestamptz,
128
created_at timestamptz not null default now(),
@@ -18,10 +14,10 @@ create table if not exists {{ index .Options "Namespace" }}.mfa_recovery_codes (
1814
id uuid primary key,
1915
mfa_recovery_code_set_id uuid not null references {{ index .Options "Namespace" }}.mfa_recovery_code_sets (id) on delete cascade,
2016
code_hash text not null,
21-
used_at timestamptz,
17+
consumed_at timestamptz,
2218
created_at timestamptz not null default now()
2319
);
2420

2521
/* auth_migration: 20260824000001 */
26-
create index if not exists mfa_recovery_codes_set_id_idx
27-
on {{ index .Options "Namespace" }}.mfa_recovery_codes (mfa_recovery_code_set_id);
22+
create unique index if not exists mfa_recovery_codes_set_id_code_hash_idx
23+
on {{ index .Options "Namespace" }}.mfa_recovery_codes (mfa_recovery_code_set_id, code_hash);

0 commit comments

Comments
 (0)