From ec8eb85d79d3e828c60364214c91559db198f23a Mon Sep 17 00:00:00 2001 From: simeng-li Date: Sun, 29 Sep 2024 16:41:39 +0800 Subject: [PATCH] fix(schemas): add user tenantId foreign key constraint add user tenantId foreign key constraint to the organizatino_user_relations table --- ...organization-user-relations-foreign-key.ts | 39 +++++++++++++++++++ .../tables/organization_user_relations.sql | 4 +- packages/schemas/tables/users.sql | 4 +- 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 packages/schemas/alterations/next-1727597588-add-organization-user-relations-foreign-key.ts diff --git a/packages/schemas/alterations/next-1727597588-add-organization-user-relations-foreign-key.ts b/packages/schemas/alterations/next-1727597588-add-organization-user-relations-foreign-key.ts new file mode 100644 index 000000000..5fcfabe76 --- /dev/null +++ b/packages/schemas/alterations/next-1727597588-add-organization-user-relations-foreign-key.ts @@ -0,0 +1,39 @@ +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; diff --git a/packages/schemas/tables/organization_user_relations.sql b/packages/schemas/tables/organization_user_relations.sql index 76f9b7004..b510957df 100644 --- a/packages/schemas/tables/organization_user_relations.sql +++ b/packages/schemas/tables/organization_user_relations.sql @@ -8,5 +8,7 @@ create table organization_user_relations ( references organizations (id) on update cascade on delete cascade, user_id varchar(21) not null references users (id) on update cascade on delete cascade, - primary key (tenant_id, organization_id, user_id) + primary key (tenant_id, organization_id, user_id), + /** Ensures that the user is a member of the organization. */ + foreign key (tenant_id, user_id) references users (tenant_id, id) on update cascade on delete cascade ); diff --git a/packages/schemas/tables/users.sql b/packages/schemas/tables/users.sql index e0304208f..b86bb0aa6 100644 --- a/packages/schemas/tables/users.sql +++ b/packages/schemas/tables/users.sql @@ -31,7 +31,9 @@ create table users ( constraint users__primary_email unique (tenant_id, primary_email), constraint users__primary_phone - unique (tenant_id, primary_phone) + unique (tenant_id, primary_phone), + constraint users__tenant_id__id + unique (tenant_id, id) ); create index users__id