Skip to content

Commit

Permalink
Implement grantRoles
Browse files Browse the repository at this point in the history
  • Loading branch information
littlewhywhat committed Jan 5, 2022
1 parent 6b32310 commit 1384c43
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
4 changes: 1 addition & 3 deletions src/migration-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand Down
20 changes: 20 additions & 0 deletions src/operations/grants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
import { MigrationOptions } from '../types'
import { GrantRoles, GrantOnTables, GrantOnSchemas } from './grantsTypes'

export { GrantRoles, GrantOnTables, GrantOnSchemas }

const isArray = <T>(item: T | T[]): item is T[] => {
return Boolean((item as Array<T>).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
}
4 changes: 2 additions & 2 deletions src/operations/grantsTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down

0 comments on commit 1384c43

Please sign in to comment.