Skip to content

Commit

Permalink
fix: use union type in addConstraint (#1155)
Browse files Browse the repository at this point in the history
Co-authored-by: Shinigami92 <chrissi92@hotmail.de>
  • Loading branch information
pigulla and Shinigami92 committed May 6, 2024
1 parent 775b245 commit 57ca2a3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,6 @@ export type {
ConstraintOptions,
CreateConstraint,
CreateConstraintFn,
CreateConstraintFn1,
CreateConstraintFn2,
CreateTable,
CreateTableFn,
DropColumns,
Expand Down
36 changes: 19 additions & 17 deletions src/operations/tables/addConstraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,33 @@ import { dropConstraint } from './dropConstraint';
import type { ConstraintOptions } from './shared';
import { parseConstraints } from './shared';

export type CreateConstraintFn1 = (
export type CreateConstraintFn = (
tableName: Name,
constraintName: string | null,
constraintOptions: ConstraintOptions & DropConstraintOptions
constraintExpressionOrOptions:
| (ConstraintOptions & DropConstraintOptions)
| string
) => string;

export type CreateConstraintFn2 = (
tableName: Name,
constraintName: string | null,
expression: string
) => string;

export type CreateConstraintFn = CreateConstraintFn1 & CreateConstraintFn2;

export type CreateConstraint = Reversible<CreateConstraintFn>;

export function addConstraint(mOptions: MigrationOptions): CreateConstraint {
const _add: CreateConstraint = (tableName, constraintName, expression) => {
const _add: CreateConstraint = (
tableName,
constraintName,
expressionOrOptions
) => {
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
);
Expand All @@ -47,20 +45,24 @@ export function addConstraint(mOptions: MigrationOptions): CreateConstraint {
].join('\n');
};

_add.reverse = (tableName, constraintName, options) => {
_add.reverse = (tableName, constraintName, expressionOrOptions) => {
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
);
};

return _add;
Expand Down
9 changes: 5 additions & 4 deletions test/operations/constraints/addConstraint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ COMMENT ON CONSTRAINT "my_constraint_name" ON "my_table_name" IS $pga$this is an
expected
) => {
const addConstraintFn = addConstraint(optionPreset);
const statement =
typeof expression === 'string'
? addConstraintFn(tableName, constraintName, expression)
: addConstraintFn(tableName, constraintName, expression);
const statement = addConstraintFn(
tableName,
constraintName,
expression
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(expected);
Expand Down

0 comments on commit 57ca2a3

Please sign in to comment.