2022-11-23 22:09:03 -05:00
|
|
|
import { sql } from 'slonik';
|
|
|
|
|
2022-12-20 00:33:53 -05:00
|
|
|
import type { AlterationScript } from '../lib/types/alteration.js';
|
2022-11-23 22:09:03 -05:00
|
|
|
|
|
|
|
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;
|