Skip to content

Commit

Permalink
refactor(sequences): reorganize operations sequences (#1081)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Apr 10, 2024
1 parent 58b6010 commit 4c81bd5
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 193 deletions.
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,14 @@ export type {
export type {
AlterSequence,
CreateSequence,
CreateSequenceFn,
DropSequence,
RenameSequence,
RenameSequenceFn,
SequenceOptions,
SequenceOptionsAlter,
SequenceOptionsCreate,
} from './operations/sequencesTypes';
} from './operations/sequences';
export type { Sql } from './operations/sql';
export type {
AddColumns,
Expand Down
132 changes: 0 additions & 132 deletions src/operations/sequences.ts

This file was deleted.

32 changes: 32 additions & 0 deletions src/operations/sequences/alterSequence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { MigrationOptions } from '../../types';
import type { Name } from '../generalTypes';
import type { SequenceOptions } from './shared';
import { parseSequenceOptions } from './shared';

export interface SequenceOptionsAlter extends SequenceOptions {
restart?: number | true;
}

export type AlterSequence = (
sequenceName: Name,
sequenceOptions: SequenceOptionsAlter
) => string | string[];

export function alterSequence(mOptions: MigrationOptions): AlterSequence {
return (sequenceName, options) => {
const { restart } = options;

const clauses = parseSequenceOptions(mOptions.typeShorthands, options);

if (restart) {
if (restart === true) {
clauses.push('RESTART');
} else {
clauses.push(`RESTART WITH ${restart}`);
}
}

return `ALTER SEQUENCE ${mOptions.literal(sequenceName)}
${clauses.join('\n ')};`;
};
}
39 changes: 39 additions & 0 deletions src/operations/sequences/createSequence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { MigrationOptions } from '../../types';
import type { DropOptions, IfNotExistsOption, Name } from '../generalTypes';
import { dropSequence } from './dropSequence';
import type { SequenceOptions } from './shared';
import { parseSequenceOptions } from './shared';

export interface SequenceOptionsCreate
extends SequenceOptions,
IfNotExistsOption {
temporary?: boolean;
}

export type CreateSequenceFn = (
sequenceName: Name,
sequenceOptions?: SequenceOptionsCreate & DropOptions
) => string | string[];

export type CreateSequence = CreateSequenceFn & { reverse: CreateSequenceFn };

export function createSequence(mOptions: MigrationOptions): CreateSequence {
const _create: CreateSequence = (sequenceName, options = {}) => {
const { temporary, ifNotExists } = options;

const temporaryStr = temporary ? ' TEMPORARY' : '';
const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : '';
const sequenceNameStr = mOptions.literal(sequenceName);
const clausesStr = parseSequenceOptions(
mOptions.typeShorthands,
options
).join('\n ');

return `CREATE${temporaryStr} SEQUENCE${ifNotExistsStr} ${sequenceNameStr}
${clausesStr};`;
};

_create.reverse = dropSequence(mOptions);

return _create;
}
21 changes: 21 additions & 0 deletions src/operations/sequences/dropSequence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { MigrationOptions } from '../../types';
import type { DropOptions, Name } from '../generalTypes';

export type DropSequence = (
sequenceName: Name,
dropOptions?: DropOptions
) => string | string[];

export function dropSequence(mOptions: MigrationOptions): DropSequence {
const _drop: DropSequence = (sequenceName, options = {}) => {
const { ifExists, cascade } = options;

const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const cascadeStr = cascade ? ' CASCADE' : '';
const sequenceNameStr = mOptions.literal(sequenceName);

return `DROP SEQUENCE${ifExistsStr} ${sequenceNameStr}${cascadeStr};`;
};

return _drop;
}
5 changes: 5 additions & 0 deletions src/operations/sequences/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './alterSequence';
export * from './createSequence';
export * from './dropSequence';
export * from './renameSequence';
export * from './shared';
23 changes: 23 additions & 0 deletions src/operations/sequences/renameSequence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { MigrationOptions } from '../../types';
import type { Name } from '../generalTypes';

export type RenameSequenceFn = (
oldSequenceName: Name,
newSequenceName: Name
) => string | string[];

export type RenameSequence = RenameSequenceFn & { reverse: RenameSequenceFn };

export function renameSequence(mOptions: MigrationOptions): RenameSequence {
const _rename: RenameSequence = (sequenceName, newSequenceName) => {
const sequenceNameStr = mOptions.literal(sequenceName);
const newSequenceNameStr = mOptions.literal(newSequenceName);

return `ALTER SEQUENCE ${sequenceNameStr} RENAME TO ${newSequenceNameStr};`;
};

_rename.reverse = (sequenceName, newSequenceName) =>
_rename(newSequenceName, sequenceName);

return _rename;
}
73 changes: 73 additions & 0 deletions src/operations/sequences/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { applyType } from '../../utils';
import type { Type } from '../generalTypes';
import type { ColumnDefinitions } from '../tablesTypes';

export interface SequenceOptions {
type?: Type;

increment?: number;

minvalue?: number | null | false;

maxvalue?: number | null | false;

start?: number;

cache?: number;

cycle?: boolean;

owner?: string | null | false;
}

export function parseSequenceOptions(
typeShorthands: ColumnDefinitions | undefined,
options: SequenceOptions
): string[] {
const { type, increment, minvalue, maxvalue, start, cache, cycle, owner } =
options;

const clauses: string[] = [];

if (type) {
clauses.push(`AS ${applyType(type, typeShorthands).type}`);
}

if (increment) {
clauses.push(`INCREMENT BY ${increment}`);
}

if (minvalue) {
clauses.push(`MINVALUE ${minvalue}`);
} else if (minvalue === null || minvalue === false) {
clauses.push('NO MINVALUE');
}

if (maxvalue) {
clauses.push(`MAXVALUE ${maxvalue}`);
} else if (maxvalue === null || maxvalue === false) {
clauses.push('NO MAXVALUE');
}

if (start) {
clauses.push(`START WITH ${start}`);
}

if (cache) {
clauses.push(`CACHE ${cache}`);
}

if (cycle) {
clauses.push('CYCLE');
} else if (cycle === false) {
clauses.push('NO CYCLE');
}

if (owner) {
clauses.push(`OWNED BY ${owner}`);
} else if (owner === null || owner === false) {
clauses.push('OWNED BY NONE');
}

return clauses;
}

0 comments on commit 4c81bd5

Please sign in to comment.