diff --git a/packages/schemas/alterations/next-1673001922-support-generic-passcode.ts b/packages/schemas/alterations/next-1673001922-support-generic-passcode.ts new file mode 100644 index 000000000..8c6607b7a --- /dev/null +++ b/packages/schemas/alterations/next-1673001922-support-generic-passcode.ts @@ -0,0 +1,23 @@ +import { sql } from 'slonik'; + +import type { AlterationScript } from '../lib/types/alteration.js'; + +const alteration: AlterationScript = { + up: async (pool) => { + await pool.query(sql` + alter table passcodes alter column interaction_jti drop not null; + create index passcodes__email_type on passcodes (email, type); + create index passcodes__phone_type on passcodes (phone, type); + `); + }, + down: async (pool) => { + await pool.query(sql` + delete from passcodes where interaction_jti is null; + drop index passcodes__email_type; + drop index passcodes__phone_type; + alter table passcodes alter column interaction_jti set not null; + `); + }, +}; + +export default alteration; diff --git a/packages/schemas/tables/passcodes.sql b/packages/schemas/tables/passcodes.sql index 9954ab18d..b5f48c3b3 100644 --- a/packages/schemas/tables/passcodes.sql +++ b/packages/schemas/tables/passcodes.sql @@ -1,6 +1,6 @@ create table passcodes ( id varchar(21) not null, - interaction_jti varchar(128) not null, + interaction_jti varchar(128), phone varchar(32), email varchar(128), type varchar(32) not null, @@ -16,3 +16,15 @@ on passcodes ( interaction_jti, type ); + +create index passcodes__email_type +on passcodes ( + email, + type +); + +create index passcodes__phone_type +on passcodes ( + phone, + type +);