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/1.13.1-1707360939-grant-is-suspended-read-permission.ts

40 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-03-16 06:04:55 -05:00
import { type CommonQueryMethods, sql } from '@silverhand/slonik';
import type { AlterationScript } from '../lib/types/alteration.js';
const getDatabaseName = async (pool: CommonQueryMethods) => {
const { currentDatabase } = await pool.one<{ currentDatabase: string }>(sql`
select current_database();
`);
return currentDatabase.replaceAll('-', '_');
};
/**
* Grant read permission to the is_suspended column in the tenants table to the logto_tenant_<databaseName> role.
*/
const alteration: AlterationScript = {
up: async (pool) => {
const databaseName = await getDatabaseName(pool);
const baseRoleId = sql.identifier([`logto_tenant_${databaseName}`]);
await pool.query(sql`
grant select (is_suspended)
on table tenants
to ${baseRoleId}
`);
},
down: async (pool) => {
const databaseName = await getDatabaseName(pool);
const baseRoleId = sql.identifier([`logto_tenant_${databaseName}`]);
await pool.query(sql`
revoke select(is_suspended)
on table tenants
from ${baseRoleId}
`);
},
};
export default alteration;