From 1384c43b4a73f33563e35e2a1a63de62b2171614 Mon Sep 17 00:00:00 2001 From: Roman Vaivod Date: Wed, 5 Jan 2022 15:54:00 +0100 Subject: [PATCH] Implement grantRoles --- src/migration-builder.ts | 4 +--- src/operations/grants.ts | 20 ++++++++++++++++++++ src/operations/grantsTypes.ts | 4 ++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/migration-builder.ts b/src/migration-builder.ts index 8057faf6..0d00c811 100644 --- a/src/migration-builder.ts +++ b/src/migration-builder.ts @@ -330,9 +330,7 @@ export default class MigrationBuilderImpl implements MigrationBuilder { this.renameMaterializedViewColumn = wrap(mViews.renameMaterializedViewColumn(options)) this.refreshMaterializedView = wrap(mViews.refreshMaterializedView(options)) - this.grantRoles = () => { - console.log('grantRoles') - } + this.grantRoles = wrap(grants.grantRoles(options)) this.grantOnTables = () => { console.log('grantOnTables') } diff --git a/src/operations/grants.ts b/src/operations/grants.ts index aa2ada29..1907ae92 100644 --- a/src/operations/grants.ts +++ b/src/operations/grants.ts @@ -1,3 +1,23 @@ +import { MigrationOptions } from '../types' import { GrantRoles, GrantOnTables, GrantOnSchemas } from './grantsTypes' export { GrantRoles, GrantOnTables, GrantOnSchemas } + +const isArray = (item: T | T[]): item is T[] => { + return Boolean((item as Array).length) +} + +export function grantRoles(mOptions: MigrationOptions) { + const _grantRoles: GrantRoles = (rolesFrom, rolesTo) => { + const _rolesFrom = isArray(rolesFrom) ? rolesFrom : [rolesFrom] + const _rolesTo = isArray(rolesTo) ? rolesTo : [rolesTo] + const rolesFromStr = _rolesFrom.map(mOptions.literal).join(',') + const rolesToStr = _rolesTo.map(mOptions.literal).join(',') + return `GRANT ${rolesFromStr} TO ${rolesToStr};` + } + _grantRoles.reverse = () => { + console.log('reverse') + return '' + } + return _grantRoles +} diff --git a/src/operations/grantsTypes.ts b/src/operations/grantsTypes.ts index 7e3d3b44..f4ee8962 100644 --- a/src/operations/grantsTypes.ts +++ b/src/operations/grantsTypes.ts @@ -13,7 +13,7 @@ type GrantRolesFn = ( rolesTo: Name | Name[], roleOptions?: WithAdminOption, ) => string | string[] -export type GrantRoles = GrantRolesFn & { reverse: GrantRoles } +export type GrantRoles = GrantRolesFn & { reverse: GrantRolesFn } type TablePrivilege = 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE' | 'TRUNCATE' | 'REFERENCES' | 'TRIGGER' type SchemaPrivilege = 'CREATE' | 'USAGE' @@ -35,7 +35,7 @@ type GrantOnTablesProps = (GrantOnSomeTablesProps | GrantOnAllTablesProps) & Wit type GrantOnTablesFn = (props: GrantOnTablesProps) => string | string[] -export type GrantOnTables = GrantOnTablesFn & { reverse: GrantOnTables } +export type GrantOnTables = GrantOnTablesFn & { reverse: GrantOnTablesFn } interface GrantOnSchemasProps { privileges: SchemaPrivilege | SchemaPrivilege[] | 'ALL'