0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-10 22:22:45 -05:00
logto/packages/schemas/alterations/next-1740982044-add-one-time-tokens-table.ts
2025-03-03 14:45:59 +08:00

40 lines
1.3 KiB
TypeScript

import { sql } from '@silverhand/slonik';
import type { AlterationScript } from '../lib/types/alteration.js';
import { applyTableRls, dropTableRls } from './utils/1704934999-tables.js';
const alteration: AlterationScript = {
up: async (pool) => {
await pool.query(sql`
create table one_time_tokens (
tenant_id varchar(21) not null
references tenants (id) on update cascade on delete cascade,
id varchar(21) not null,
email varchar(256) not null,
token varchar(256) not null,
context jsonb not null default '{}'::jsonb,
status varchar(64) not null default 'active',
created_at timestamptz not null default(now()),
expires_at timestamptz not null,
primary key (id)
);
create index one_time_token__id on one_time_tokens (tenant_id, id);
create index one_time_token__email on one_time_tokens (tenant_id, email);
create unique index one_time_token__active_unique on one_time_tokens (tenant_id, email, token) where status = 'active';
`);
await applyTableRls(pool, 'one_time_tokens');
},
down: async (pool) => {
await dropTableRls(pool, 'one_time_tokens');
await pool.query(sql`
drop table if exists one_time_tokens;
`);
},
};
export default alteration;