0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-24 22:05:56 -05:00
logto/packages/schemas/alterations/next-1727597588-add-organization-user-relations-foreign-key.ts
simeng-li ec8eb85d79
fix(schemas): add user tenantId foreign key constraint
add user tenantId foreign key constraint to the organizatino_user_relations table
2024-09-29 16:41:39 +08:00

39 lines
1,019 B
TypeScript

import { sql } from '@silverhand/slonik';
import type { AlterationScript } from '../lib/types/alteration.js';
const alteration: AlterationScript = {
up: async (pool) => {
await pool.query(sql`
alter table users
add constraint users__tenant_id__id unique (tenant_id, id);
`);
await pool.query(sql`
alter table organization_user_relations
add constraint organization_user_relations__user_id__fk
foreign key (tenant_id, user_id) references users (tenant_id, id) on update cascade on delete cascade;
`);
await pool.query(sql`
drop index users__id;
`);
},
down: async (pool) => {
await pool.query(sql`
create index users__id on users (id);
`);
await pool.query(sql`
alter table organization_user_relations
drop constraint organization_user_relations__user_id__fk;
`);
await pool.query(sql`
alter table users
drop constraint users__tenant_id__id;
`);
},
};
export default alteration;