0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00
logto/packages/schemas/alterations/1.18.0-1718785576-organization-application-relations.ts
2024-07-06 09:07:56 +08:00

37 lines
1.4 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 function check_application_type(application_id varchar(21), target_type application_type) returns boolean as
$$ begin
return (select type from applications where id = application_id) = target_type;
end; $$ language plpgsql;
create table organization_application_relations (
tenant_id varchar(21) not null
references tenants (id) on update cascade on delete cascade,
organization_id varchar(21) not null
references organizations (id) on update cascade on delete cascade,
application_id varchar(21) not null
references applications (id) on update cascade on delete cascade,
primary key (tenant_id, organization_id, application_id),
constraint application_type
check (check_application_type(application_id, 'MachineToMachine'))
);
`);
await applyTableRls(pool, 'organization_application_relations');
},
down: async (pool) => {
await dropTableRls(pool, 'organization_application_relations');
await pool.query(sql`
drop table organization_application_relations;
drop function check_application_type;
`);
},
};
export default alteration;