Skip to content

Commit

Permalink
refactor(functions): reorganize operations functions (#1074)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Apr 8, 2024
1 parent 464513a commit c58dcdb
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 167 deletions.
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ export { Migration } from './migration';
export type {
AlterDomain,
CreateDomain,
CreateDomainFn,
DomainOptions,
DomainOptionsAlter,
DomainOptionsCreate,
DropDomain,
RenameDomain,
RenameDomainFn,
} from './operations/domains';
export type {
CreateExtension,
Expand All @@ -18,11 +20,14 @@ export type {
} from './operations/extensions';
export type {
CreateFunction,
CreateFunctionFn,
DropFunction,
FunctionOptions,
FunctionParam,
FunctionParamType,
RenameFunction,
} from './operations/functionsTypes';
RenameFunctionFn,
} from './operations/functions';
export type {
CascadeOption,
DropOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/operations/domains/createDomain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface DomainOptionsCreate extends DomainOptions {
collation?: string;
}

type CreateDomainFn = (
export type CreateDomainFn = (
domainName: Name,
type: Type,
domainOptions?: DomainOptionsCreate & DropOptions
Expand Down
2 changes: 1 addition & 1 deletion src/operations/domains/renameDomain.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MigrationOptions } from '../../types';
import type { Name } from '../generalTypes';

type RenameDomainFn = (
export type RenameDomainFn = (
oldDomainName: Name,
newDomainName: Name
) => string | string[];
Expand Down
105 changes: 0 additions & 105 deletions src/operations/functions.ts

This file was deleted.

72 changes: 72 additions & 0 deletions src/operations/functions/createFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type { MigrationOptions } from '../../types';
import { escapeValue, formatParams } from '../../utils';
import type { DropOptions, Name, Value } from '../generalTypes';
import { dropFunction } from './dropFunction';
import type { FunctionOptions, FunctionParam } from './shared';

export type CreateFunctionFn = (
functionName: Name,
functionParams: FunctionParam[],
functionOptions: FunctionOptions & DropOptions,
definition: Value
) => string | string[];

export type CreateFunction = CreateFunctionFn & { reverse: CreateFunctionFn };

export function createFunction(mOptions: MigrationOptions): CreateFunction {
const _create: CreateFunction = (
functionName,
functionParams = [],
functionOptions,
definition
) => {
const {
replace,
returns = 'void',
language,
window,
behavior = 'VOLATILE',
onNull,
parallel,
} = functionOptions;

const options: string[] = [];

if (behavior) {
options.push(behavior);
}

if (language) {
options.push(`LANGUAGE ${language}`);
} else {
throw new Error(
`Language for function ${functionName} have to be specified`
);
}

if (window) {
options.push('WINDOW');
}

if (onNull) {
options.push('RETURNS NULL ON NULL INPUT');
}

if (parallel) {
options.push(`PARALLEL ${parallel}`);
}

const replaceStr = replace ? ' OR REPLACE' : '';
const paramsStr = formatParams(functionParams, mOptions);
const functionNameStr = mOptions.literal(functionName);

return `CREATE${replaceStr} FUNCTION ${functionNameStr}${paramsStr}
RETURNS ${returns}
AS ${escapeValue(definition)}
${options.join('\n ')};`;
};

_create.reverse = dropFunction(mOptions);

return _create;
}
29 changes: 29 additions & 0 deletions src/operations/functions/dropFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { MigrationOptions } from '../../types';
import { formatParams } from '../../utils';
import type { DropOptions, Name } from '../generalTypes';
import type { FunctionParam } from './shared';

export type DropFunction = (
functionName: Name,
functionParams: FunctionParam[],
dropOptions?: DropOptions
) => string | string[];

export function dropFunction(mOptions: MigrationOptions): DropFunction {
const _drop: DropFunction = (
functionName,
functionParams = [],
options = {}
) => {
const { ifExists, cascade } = options;

const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const cascadeStr = cascade ? ' CASCADE' : '';
const paramsStr = formatParams(functionParams, mOptions);
const functionNameStr = mOptions.literal(functionName);

return `DROP FUNCTION${ifExistsStr} ${functionNameStr}${paramsStr}${cascadeStr};`;
};

return _drop;
}
4 changes: 4 additions & 0 deletions src/operations/functions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './createFunction';
export * from './dropFunction';
export * from './renameFunction';
export * from './shared';
31 changes: 31 additions & 0 deletions src/operations/functions/renameFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { MigrationOptions } from '../../types';
import { formatParams } from '../../utils';
import type { Name } from '../generalTypes';
import type { FunctionParam } from './shared';

export type RenameFunctionFn = (
oldFunctionName: Name,
functionParams: FunctionParam[],
newFunctionName: Name
) => string | string[];

export type RenameFunction = RenameFunctionFn & { reverse: RenameFunctionFn };

export function renameFunction(mOptions: MigrationOptions): RenameFunction {
const _rename: RenameFunction = (
oldFunctionName,
functionParams = [],
newFunctionName
) => {
const paramsStr = formatParams(functionParams, mOptions);
const oldFunctionNameStr = mOptions.literal(oldFunctionName);
const newFunctionNameStr = mOptions.literal(newFunctionName);

return `ALTER FUNCTION ${oldFunctionNameStr}${paramsStr} RENAME TO ${newFunctionNameStr};`;
};

_rename.reverse = (oldFunctionName, functionParams, newFunctionName) =>
_rename(newFunctionName, functionParams, oldFunctionName);

return _rename;
}
29 changes: 29 additions & 0 deletions src/operations/functions/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { Value } from '../generalTypes';

export interface FunctionParamType {
mode?: 'IN' | 'OUT' | 'INOUT' | 'VARIADIC';

name?: string;

type: string;

default?: Value;
}

export type FunctionParam = string | FunctionParamType;

export interface FunctionOptions {
returns?: string;

language: string;

replace?: boolean;

window?: boolean;

behavior?: 'IMMUTABLE' | 'STABLE' | 'VOLATILE';

onNull?: boolean;

parallel?: 'UNSAFE' | 'RESTRICTED' | 'SAFE';
}
52 changes: 0 additions & 52 deletions src/operations/functionsTypes.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/operations/operatorsTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FunctionParam } from './functionsTypes';
import type { FunctionParam } from './functions';
import type { DropOptions, Name, Type } from './generalTypes';

export interface CreateOperatorOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/operations/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
intersection,
makeComment,
} from '../utils';
import type { FunctionParamType } from './functionsTypes';
import type { FunctionParamType } from './functions';
import type { Name } from './generalTypes';
import { parseSequenceOptions } from './sequences';
import type {
Expand Down

0 comments on commit c58dcdb

Please sign in to comment.