-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(sequences): reorganize operations sequences (#1081)
- Loading branch information
1 parent
58b6010
commit 4c81bd5
Showing
11 changed files
with
199 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ')};`; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.