2023-01-16 05:21:39 -05:00
|
|
|
import { sql } from 'slonik';
|
|
|
|
|
|
|
|
import type { AlterationScript } from '../lib/types/alteration.js';
|
|
|
|
|
|
|
|
const adminConsoleAdminRoleId = 'ac-admin-id';
|
2023-01-16 23:54:14 -05:00
|
|
|
const adminRoleName = 'admin';
|
|
|
|
const managementResourceScopeId = 'management-api-scope';
|
2023-01-16 05:21:39 -05:00
|
|
|
|
|
|
|
const alteration: AlterationScript = {
|
|
|
|
up: async (pool) => {
|
2023-01-16 23:54:14 -05:00
|
|
|
// Fix role id
|
|
|
|
const newRole = await pool.maybeOne(sql`
|
|
|
|
select * from roles
|
2023-02-02 05:36:18 -05:00
|
|
|
where id = ${adminConsoleAdminRoleId}
|
2023-01-16 23:54:14 -05:00
|
|
|
`);
|
|
|
|
|
|
|
|
if (!newRole) {
|
|
|
|
await pool.query(sql`
|
|
|
|
update roles
|
2023-02-02 05:36:18 -05:00
|
|
|
set id = ${adminConsoleAdminRoleId}
|
|
|
|
where name = ${adminRoleName}
|
2023-01-16 23:54:14 -05:00
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix scope role
|
2023-01-16 05:21:39 -05:00
|
|
|
const relation = await pool.maybeOne(sql`
|
|
|
|
select * from roles_scopes
|
2023-02-02 05:36:18 -05:00
|
|
|
where scope_id = ${managementResourceScopeId}
|
|
|
|
and role_id = ${adminConsoleAdminRoleId}
|
2023-01-16 05:21:39 -05:00
|
|
|
`);
|
|
|
|
|
|
|
|
if (!relation) {
|
|
|
|
await pool.query(sql`
|
|
|
|
insert into roles_scopes
|
2023-02-02 05:36:18 -05:00
|
|
|
(role_id, scope_id)
|
|
|
|
values (${adminConsoleAdminRoleId}, ${managementResourceScopeId})
|
2023-01-16 05:21:39 -05:00
|
|
|
`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
down: async () => {
|
|
|
|
// This is a hotfix for seed, down script is not needed
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default alteration;
|