0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

fix(schemas): add user tenantId foreign key constraint

add user tenantId foreign key constraint to the organizatino_user_relations table
This commit is contained in:
simeng-li 2024-09-29 16:41:39 +08:00
parent 3131802c6a
commit ec8eb85d79
No known key found for this signature in database
GPG key ID: 14EA7BB1541E8075
3 changed files with 45 additions and 2 deletions

View file

@ -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;

View file

@ -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
);

View file

@ -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