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

fix: use union type in addConstraint #1155

Merged
merged 4 commits into from
May 6, 2024
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
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