0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-23 20:33:16 -05:00
logto/packages/schemas/alterations/next-1669091623-roles-and-scopes.ts

44 lines
1.5 KiB
TypeScript
Raw Normal View History

import { sql } from 'slonik';
2022-12-20 00:33:53 -05:00
import type { AlterationScript } from '../lib/types/alteration.js';
const alteration: AlterationScript = {
up: async (pool) => {
await pool.query(sql`
-- scopes
create table scopes (
id varchar(21) not null,
resource_id varchar(21) references resources (id) on update cascade on delete cascade,
name varchar(256) not null,
description text,
created_at timestamptz not null default(now()),
primary key (id)
);
-- update table roles, add id and replace pkey
alter table roles add column id varchar(21);
update roles set id = name;
alter table roles alter column id set not null;
alter table roles drop constraint roles_pkey;
create unique index roles_pkey on roles using btree(id);
create unique index roles__name on roles (name);
-- roles_scopes
create table roles_scopes (
role_id varchar(21) references roles (id) on update cascade on delete cascade,
scope_id varchar(21) references scopes (id) on update cascade on delete cascade,
constraint roles_permissison_pkey primary key (role_id, scope_id)
);
`);
},
down: async (pool) => {
await pool.query(sql`
drop table permissions;
alter index roles_pkey rename to roles_pkey_1;
create unique index roles_pkey on roles using btree(name)
drop index roles_pkey_1;
alter table roles drop column id;
`);
},
};
export default alteration;