Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict TS #523

Merged
merged 2 commits into from
Nov 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/migration-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export default class MigrationBuilderImpl implements MigrationBuilder {

private _useTransaction: boolean

constructor(db: DB, typeShorthands: ColumnDefinitions, shouldDecamelize: boolean) {
constructor(db: DB, typeShorthands: ColumnDefinitions | undefined, shouldDecamelize: boolean) {
this._steps = []
this._REVERSE_MODE = false
// by default, all migrations are wrapped in a transaction
Expand All @@ -210,11 +210,15 @@ export default class MigrationBuilderImpl implements MigrationBuilder {
// calls the operation or its reverse, and appends the result (array of sql statements)
// to the steps array
const wrap = <T extends Operation>(operation: T) => (...args: Parameters<T>) => {
if (this._REVERSE_MODE && typeof operation.reverse !== 'function') {
const name = `pgm.${operation.name}()`
throw new Error(`Impossible to automatically infer down migration for "${name}"`)
if (this._REVERSE_MODE) {
if (typeof operation.reverse !== 'function') {
const name = `pgm.${operation.name}()`
throw new Error(`Impossible to automatically infer down migration for "${name}"`)
}
this._steps = this._steps.concat(operation.reverse(...args))
} else {
this._steps = this._steps.concat(operation(...args))
}
this._steps = this._steps.concat(this._REVERSE_MODE ? operation.reverse(...args) : operation(...args))
}

const options: MigrationOptions = {
Expand Down Expand Up @@ -325,7 +329,7 @@ export default class MigrationBuilderImpl implements MigrationBuilder {
this.func = PgLiteral.create

// expose DB so we can access database within transaction
const wrapDB = <T, R>(operation: (...args: T[]) => R) => (...args: T[]) => {
const wrapDB = <T extends any[], R>(operation: (...args: T) => R) => (...args: T) => {
if (this._REVERSE_MODE) {
throw new Error('Impossible to automatically infer down migration')
}
Expand Down
14 changes: 8 additions & 6 deletions src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ export const loadMigrationFiles = async (dir: string, ignorePattern?: string) =>
return stats.isFile() ? file : null
}),
)
).sort()
)
.filter((file): file is string => Boolean(file))
.sort()
const filter = new RegExp(`^(${ignorePattern})$`) // eslint-disable-line security/detect-non-literal-regexp
return ignorePattern === undefined ? files : files.filter(i => i && !filter.test(i))
return ignorePattern === undefined ? files : files.filter(i => !filter.test(i))
}

const getLastSuffix = async (dir: string, ignorePattern?: string) => {
Expand Down Expand Up @@ -87,7 +89,7 @@ export class Migration implements RunMigration {

public readonly options: RunnerOption

public readonly typeShorthands: ColumnDefinitions
public readonly typeShorthands?: ColumnDefinitions

public readonly log: typeof console.log

Expand Down Expand Up @@ -153,7 +155,7 @@ export class Migration implements RunMigration {
this.log(`${sqlSteps.join('\n')}\n\n`)

return sqlSteps.reduce(
(promise, sql) => promise.then((): unknown => this.options.dryRun || this.db.query(sql)),
(promise: Promise<unknown>, sql) => promise.then((): unknown => this.options.dryRun || this.db.query(sql)),
Promise.resolve(),
)
}
Expand All @@ -169,7 +171,7 @@ export class Migration implements RunMigration {
}
}

const action: MigrationAction | false = this[direction]
const action: MigrationAction | false | undefined = this[direction]

if (typeof action !== 'function') {
throw new Error(
Expand All @@ -181,7 +183,7 @@ export class Migration implements RunMigration {
}

apply(direction: MigrationDirection) {
const pgm = new MigrationBuilder(this.db, this.typeShorthands, this.options.decamelize)
const pgm = new MigrationBuilder(this.db, this.typeShorthands, Boolean(this.options.decamelize))
const action = this._getAction(direction)

if (this.down === this.up) {
Expand Down
3 changes: 2 additions & 1 deletion src/operations/domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { CreateDomain, DropDomain, AlterDomain, RenameDomain } from './domainsTy
export { CreateDomain, DropDomain, AlterDomain, RenameDomain }

export function dropDomain(mOptions: MigrationOptions) {
const _drop: DropDomain = (domainName, { ifExists, cascade } = {}) => {
const _drop: DropDomain = (domainName, options = {}) => {
const { ifExists, cascade } = options
const ifExistsStr = ifExists ? ' IF EXISTS' : ''
const cascadeStr = cascade ? ' CASCADE' : ''
const domainNameStr = mOptions.literal(domainName)
Expand Down
6 changes: 4 additions & 2 deletions src/operations/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { CreateExtension, DropExtension } from './extensionsTypes'
export { CreateExtension, DropExtension }

export function dropExtension(mOptions: MigrationOptions) {
const _drop: DropExtension = (extensions, { ifExists, cascade } = {}) => {
const _drop: DropExtension = (extensions, options = {}) => {
const { ifExists, cascade } = options
if (!_.isArray(extensions)) extensions = [extensions] // eslint-disable-line no-param-reassign
const ifExistsStr = ifExists ? ' IF EXISTS' : ''
const cascadeStr = cascade ? ' CASCADE' : ''
Expand All @@ -18,7 +19,8 @@ export function dropExtension(mOptions: MigrationOptions) {
}

export function createExtension(mOptions: MigrationOptions) {
const _create: CreateExtension = (extensions, { ifNotExists, schema } = {}) => {
const _create: CreateExtension = (extensions, options = {}) => {
const { ifNotExists, schema } = options
if (!_.isArray(extensions)) extensions = [extensions] // eslint-disable-line no-param-reassign
const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : ''
const schemaStr = schema ? ` SCHEMA ${mOptions.literal(schema)}` : ''
Expand Down
3 changes: 2 additions & 1 deletion src/operations/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { CreateFunction, DropFunction, RenameFunction } from './functionsTypes'
export { CreateFunction, DropFunction, RenameFunction }

export function dropFunction(mOptions: MigrationOptions) {
const _drop: DropFunction = (functionName, functionParams = [], { ifExists, cascade } = {}) => {
const _drop: DropFunction = (functionName, functionParams = [], options = {}) => {
const { ifExists, cascade } = options
const ifExistsStr = ifExists ? ' IF EXISTS' : ''
const cascadeStr = cascade ? ' CASCADE' : ''
const paramsStr = formatParams(functionParams, mOptions)
Expand Down
8 changes: 5 additions & 3 deletions src/operations/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function dropOperator(mOptions: MigrationOptions) {

export function createOperator(mOptions: MigrationOptions) {
const _create: CreateOperator = (operatorName, options) => {
const { procedure, left, right, commutator, negator, restrict, join, hashes, merges } = options
const { procedure, left, right, commutator, negator, restrict, join, hashes, merges } = options || {}

const defs = []
defs.push(`PROCEDURE = ${mOptions.literal(procedure)}`)
Expand Down Expand Up @@ -81,7 +81,8 @@ export function createOperator(mOptions: MigrationOptions) {
}

export function dropOperatorFamily(mOptions: MigrationOptions) {
const _drop: DropOperatorFamily = (operatorFamilyName, indexMethod, { ifExists, cascade } = {}) => {
const _drop: DropOperatorFamily = (operatorFamilyName, indexMethod, options = {}) => {
const { ifExists, cascade } = options
const operatorFamilyNameStr = mOptions.literal(operatorFamilyName)
const ifExistsStr = ifExists ? ' IF EXISTS' : ''
const cascadeStr = cascade ? ' CASCADE' : ''
Expand Down Expand Up @@ -153,7 +154,8 @@ export function renameOperatorFamily(mOptions: MigrationOptions) {
}

export function dropOperatorClass(mOptions: MigrationOptions) {
const _drop: DropOperatorClass = (operatorClassName, indexMethod, { ifExists, cascade } = {}) => {
const _drop: DropOperatorClass = (operatorClassName, indexMethod, options = {}) => {
const { ifExists, cascade } = options
const operatorClassNameStr = mOptions.literal(operatorClassName)
const ifExistsStr = ifExists ? ' IF EXISTS' : ''
const cascadeStr = cascade ? ' CASCADE' : ''
Expand Down
2 changes: 1 addition & 1 deletion src/operations/operatorsTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface OperatorListDefinition {
params?: FunctionParam[]
}

type CreateOperatorFn = (operatorName: Name, options?: CreateOperatorOptions & DropOperatorOptions) => string | string[]
type CreateOperatorFn = (operatorName: Name, options: CreateOperatorOptions & DropOperatorOptions) => string | string[]
export type CreateOperator = CreateOperatorFn & { reverse: CreateOperatorFn }
export type DropOperator = (operatorName: Name, dropOptions?: DropOperatorOptions) => string | string[]
type CreateOperatorClassFn = (
Expand Down
3 changes: 2 additions & 1 deletion src/operations/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const makeClauses = ({ role, using, check }: PolicyOptions) => {
}

export function dropPolicy(mOptions: MigrationOptions) {
const _drop: DropPolicy = (tableName, policyName, { ifExists } = {}) => {
const _drop: DropPolicy = (tableName, policyName, options = {}) => {
const { ifExists } = options
const ifExistsStr = ifExists ? ' IF EXISTS' : ''
const policyNameStr = mOptions.literal(policyName)
const tableNameStr = mOptions.literal(tableName)
Expand Down
3 changes: 2 additions & 1 deletion src/operations/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ const formatRoleOptions = (roleOptions: RoleOptions = {}) => {
}

export function dropRole(mOptions: MigrationOptions) {
const _drop: DropRole = (roleName, { ifExists } = {}) => {
const _drop: DropRole = (roleName, options = {}) => {
const { ifExists } = options
const ifExistsStr = ifExists ? ' IF EXISTS' : ''
const roleNameStr = mOptions.literal(roleName)
return `DROP ROLE${ifExistsStr} ${roleNameStr};`
Expand Down
6 changes: 4 additions & 2 deletions src/operations/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { CreateSchema, DropSchema, RenameSchema } from './schemasTypes'
export { CreateSchema, DropSchema, RenameSchema }

export function dropSchema(mOptions: MigrationOptions) {
const _drop: DropSchema = (schemaName, { ifExists, cascade } = {}) => {
const _drop: DropSchema = (schemaName, options = {}) => {
const { ifExists, cascade } = options
const ifExistsStr = ifExists ? ' IF EXISTS' : ''
const cascadeStr = cascade ? ' CASCADE' : ''
const schemaNameStr = mOptions.literal(schemaName)
Expand All @@ -14,7 +15,8 @@ export function dropSchema(mOptions: MigrationOptions) {
}

export function createSchema(mOptions: MigrationOptions) {
const _create: CreateSchema = (schemaName: string, { ifNotExists, authorization } = {}) => {
const _create: CreateSchema = (schemaName: string, options = {}) => {
const { ifNotExists, authorization } = options
const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : ''
const schemaNameStr = mOptions.literal(schemaName)
const authorizationStr = authorization ? ` AUTHORIZATION ${authorization}` : ''
Expand Down
5 changes: 3 additions & 2 deletions src/operations/sequences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ColumnDefinitions } from './tablesTypes'

export { CreateSequence, DropSequence, AlterSequence, RenameSequence }

export const parseSequenceOptions = (typeShorthands: ColumnDefinitions, options: SequenceOptions) => {
export const parseSequenceOptions = (typeShorthands: ColumnDefinitions | undefined, options: SequenceOptions) => {
const { type, increment, minvalue, maxvalue, start, cache, cycle, owner } = options
const clauses: string[] = []
if (type) {
Expand Down Expand Up @@ -44,7 +44,8 @@ export const parseSequenceOptions = (typeShorthands: ColumnDefinitions, options:
}

export function dropSequence(mOptions: MigrationOptions) {
const _drop: DropSequence = (sequenceName, { ifExists, cascade } = {}) => {
const _drop: DropSequence = (sequenceName, options = {}) => {
const { ifExists, cascade } = options
const ifExistsStr = ifExists ? ' IF EXISTS' : ''
const cascadeStr = cascade ? ' CASCADE' : ''
const sequenceNameStr = mOptions.literal(sequenceName)
Expand Down
Loading