|
| 1 | +import { sql } from 'slonik'; |
| 2 | + |
| 3 | +import type { AlterationScript } from '../lib/types/alteration.js'; |
| 4 | + |
| 5 | +import { applyTableRls, dropTableRls } from './utils/1704934999-tables.js'; |
| 6 | + |
| 7 | +const alteration: AlterationScript = { |
| 8 | + up: async (pool) => { |
| 9 | + await pool.query(sql` |
| 10 | + create table organization_invitations ( |
| 11 | + tenant_id varchar(21) not null |
| 12 | + references tenants (id) on update cascade on delete cascade, |
| 13 | + /** The unique identifier of the invitation. */ |
| 14 | + id varchar(21) not null, |
| 15 | + /** The user ID who sent the invitation. */ |
| 16 | + inviter_id varchar(21) not null, |
| 17 | + /** The email address or other identifier of the invitee. */ |
| 18 | + invitee varchar(256) not null, |
| 19 | + /** The user ID of who accepted the invitation. */ |
| 20 | + accepted_user_id varchar(21) |
| 21 | + references users (id) on update cascade on delete cascade, |
| 22 | + /** The ID of the organization to which the invitee is invited. */ |
| 23 | + organization_id varchar(21) not null, |
| 24 | + /** The status of the invitation. */ |
| 25 | + status varchar(32) /* @use OrganizationInvitationStatus */ not null, |
| 26 | + /** The ID of the magic link that can be used to accept the invitation. */ |
| 27 | + magic_link_id varchar(21) |
| 28 | + references magic_links (id) on update cascade on delete cascade, |
| 29 | + /** The time when the invitation was created. */ |
| 30 | + created_at timestamptz not null default (now()), |
| 31 | + /** The time when the invitation status was last updated. */ |
| 32 | + updated_at timestamptz not null default (now()), |
| 33 | + /** The time when the invitation expires. */ |
| 34 | + expires_at timestamptz not null, |
| 35 | + primary key (id), |
| 36 | + foreign key (tenant_id, inviter_id, organization_id) |
| 37 | + references organization_user_relations (tenant_id, user_id, organization_id) |
| 38 | + on update cascade on delete cascade |
| 39 | + ); |
| 40 | + `); |
| 41 | + await applyTableRls(pool, 'organization_invitations'); |
| 42 | + |
| 43 | + await pool.query(sql` |
| 44 | + create table organization_invitation_roles ( |
| 45 | + tenant_id varchar(21) not null |
| 46 | + references tenants (id) on update cascade on delete cascade, |
| 47 | + /** The ID of the invitation. */ |
| 48 | + invitation_id varchar(21) not null |
| 49 | + references organization_invitations (id) on update cascade on delete cascade, |
| 50 | + /** The ID of the organization role. */ |
| 51 | + organization_role_id varchar(21) not null |
| 52 | + references organization_roles (id) on update cascade on delete cascade, |
| 53 | + primary key (tenant_id, invitation_id, organization_role_id) |
| 54 | + ); |
| 55 | + `); |
| 56 | + await applyTableRls(pool, 'organization_invitation_roles'); |
| 57 | + }, |
| 58 | + down: async (pool) => { |
| 59 | + await dropTableRls(pool, 'organization_invitation_roles'); |
| 60 | + await pool.query(sql` |
| 61 | + drop table organization_invitation_roles; |
| 62 | + `); |
| 63 | + await dropTableRls(pool, 'organization_invitations'); |
| 64 | + await pool.query(sql` |
| 65 | + drop table organization_invitations; |
| 66 | + `); |
| 67 | + }, |
| 68 | +}; |
| 69 | + |
| 70 | +export default alteration; |
0 commit comments