diff --git a/src/operations/tables/addConstraint.ts b/src/operations/tables/addConstraint.ts index 3fe7f134a..211574cd3 100644 --- a/src/operations/tables/addConstraint.ts +++ b/src/operations/tables/addConstraint.ts @@ -18,23 +18,27 @@ export type CreateConstraintFn2 = ( expression: string ) => string; -export type CreateConstraintFn = CreateConstraintFn1 & CreateConstraintFn2; +export type CreateConstraintFn = CreateConstraintFn1 | CreateConstraintFn2; export type CreateConstraint = Reversible; export function addConstraint(mOptions: MigrationOptions): CreateConstraint { - const _add: CreateConstraint = (tableName, constraintName, expression) => { + const _add: CreateConstraint = ( + tableName: Name, + constraintName: string | null, + expressionOrOptions: string | (ConstraintOptions & DropConstraintOptions) + ) => { const { constraints, comments } = - typeof expression === 'string' + typeof expressionOrOptions === 'string' ? { constraints: [ - `${constraintName ? `CONSTRAINT ${mOptions.literal(constraintName)} ` : ''}${expression}`, + `${constraintName ? `CONSTRAINT ${mOptions.literal(constraintName)} ` : ''}${expressionOrOptions}`, ], comments: [], } : parseConstraints( tableName, - expression, + expressionOrOptions, constraintName, mOptions.literal ); @@ -47,21 +51,31 @@ export function addConstraint(mOptions: MigrationOptions): CreateConstraint { ].join('\n'); }; - _add.reverse = (tableName, constraintName, options) => { + const reverse: CreateConstraintFn = ( + tableName: Name, + constraintName: string | null, + expressionOrOptions: string | (ConstraintOptions & DropConstraintOptions) + ) => { if (constraintName === null) { throw new Error( 'Impossible to automatically infer down migration for addConstraint without naming constraint' ); } - if (typeof options === 'string') { + if (typeof expressionOrOptions === 'string') { throw new Error( 'Impossible to automatically infer down migration for addConstraint with raw SQL expression' ); } - return dropConstraint(mOptions)(tableName, constraintName, options); + return dropConstraint(mOptions)( + tableName, + constraintName, + expressionOrOptions + ); }; + _add.reverse = reverse; + return _add; }