-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f7c495
commit 89b60ca
Showing
9 changed files
with
178 additions
and
170 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,49 @@ | ||
import type { DomainOptions } from '.'; | ||
import type { MigrationOptions } from '../../types'; | ||
import { escapeValue } from '../../utils'; | ||
import type { Name } from '../generalTypes'; | ||
|
||
export interface DomainOptionsAlter extends DomainOptions { | ||
allowNull?: boolean; | ||
} | ||
|
||
export type AlterDomain = ( | ||
domainName: Name, | ||
domainOptions: DomainOptionsAlter | ||
) => string | string[]; | ||
|
||
export function alterDomain(mOptions: MigrationOptions): AlterDomain { | ||
const _alter: AlterDomain = (domainName, options) => { | ||
const { | ||
default: defaultValue, | ||
notNull, | ||
allowNull, | ||
check, | ||
constraintName, | ||
} = options; | ||
|
||
const actions: string[] = []; | ||
|
||
if (defaultValue === null) { | ||
actions.push('DROP DEFAULT'); | ||
} else if (defaultValue !== undefined) { | ||
actions.push(`SET DEFAULT ${escapeValue(defaultValue)}`); | ||
} | ||
|
||
if (notNull) { | ||
actions.push('SET NOT NULL'); | ||
} else if (notNull === false || allowNull) { | ||
actions.push('DROP NOT NULL'); | ||
} | ||
|
||
if (check) { | ||
actions.push( | ||
`${constraintName ? `CONSTRAINT ${mOptions.literal(constraintName)} ` : ''}CHECK (${check})` | ||
); | ||
} | ||
|
||
return `${actions.map((action) => `ALTER DOMAIN ${mOptions.literal(domainName)} ${action}`).join(';\n')};`; | ||
}; | ||
|
||
return _alter; | ||
} |
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,67 @@ | ||
import type { DomainOptions } from '.'; | ||
import { dropDomain } from '.'; | ||
import type { MigrationOptions } from '../../types'; | ||
import { applyType, escapeValue } from '../../utils'; | ||
import type { DropOptions, Name, Type } from './../generalTypes'; | ||
|
||
export interface DomainOptionsCreate extends DomainOptions { | ||
collation?: string; | ||
} | ||
|
||
type CreateDomainFn = ( | ||
domainName: Name, | ||
type: Type, | ||
domainOptions?: DomainOptionsCreate & DropOptions | ||
) => string | string[]; | ||
|
||
export type CreateDomain = CreateDomainFn & { reverse: CreateDomainFn }; | ||
|
||
export function createDomain(mOptions: MigrationOptions): CreateDomain { | ||
const _create: CreateDomain = (domainName, type, options = {}) => { | ||
const { | ||
default: defaultValue, | ||
collation, | ||
notNull, | ||
check, | ||
constraintName, | ||
} = options; | ||
|
||
const constraints: string[] = []; | ||
|
||
if (collation) { | ||
constraints.push(`COLLATE ${collation}`); | ||
} | ||
|
||
if (defaultValue !== undefined) { | ||
constraints.push(`DEFAULT ${escapeValue(defaultValue)}`); | ||
} | ||
|
||
if (notNull && check) { | ||
throw new Error('"notNull" and "check" can\'t be specified together'); | ||
} else if (notNull || check) { | ||
if (constraintName) { | ||
constraints.push(`CONSTRAINT ${mOptions.literal(constraintName)}`); | ||
} | ||
|
||
if (notNull) { | ||
constraints.push('NOT NULL'); | ||
} else if (check) { | ||
constraints.push(`CHECK (${check})`); | ||
} | ||
} | ||
|
||
const constraintsStr = constraints.length | ||
? ` ${constraints.join(' ')}` | ||
: ''; | ||
|
||
const typeStr = applyType(type, mOptions.typeShorthands).type; | ||
const domainNameStr = mOptions.literal(domainName); | ||
|
||
return `CREATE DOMAIN ${domainNameStr} AS ${typeStr}${constraintsStr};`; | ||
}; | ||
|
||
_create.reverse = (domainName, type, options) => | ||
dropDomain(mOptions)(domainName, options); | ||
|
||
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 DropDomain = ( | ||
domainName: Name, | ||
dropOptions?: DropOptions | ||
) => string | string[]; | ||
|
||
export function dropDomain(mOptions: MigrationOptions): DropDomain { | ||
const _drop: DropDomain = (domainName, options = {}) => { | ||
const { ifExists, cascade } = options; | ||
|
||
const ifExistsStr = ifExists ? ' IF EXISTS' : ''; | ||
const cascadeStr = cascade ? ' CASCADE' : ''; | ||
const domainNameStr = mOptions.literal(domainName); | ||
|
||
return `DROP DOMAIN${ifExistsStr} ${domainNameStr}${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,16 @@ | ||
import type { Value } from './../generalTypes'; | ||
|
||
export interface DomainOptions { | ||
default?: Value; | ||
|
||
notNull?: boolean; | ||
|
||
check?: string; | ||
|
||
constraintName?: string; | ||
} | ||
|
||
export * from './alterDomain'; | ||
export * from './createDomain'; | ||
export * from './dropDomain'; | ||
export * from './renameDomain'; |
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'; | ||
|
||
type RenameDomainFn = ( | ||
oldDomainName: Name, | ||
newDomainName: Name | ||
) => string | string[]; | ||
|
||
export type RenameDomain = RenameDomainFn & { reverse: RenameDomainFn }; | ||
|
||
export function renameDomain(mOptions: MigrationOptions): RenameDomain { | ||
const _rename: RenameDomain = (domainName, newDomainName) => { | ||
const domainNameStr = mOptions.literal(domainName); | ||
const newDomainNameStr = mOptions.literal(newDomainName); | ||
|
||
return `ALTER DOMAIN ${domainNameStr} RENAME TO ${newDomainNameStr};`; | ||
}; | ||
|
||
_rename.reverse = (domainName, newDomainName) => | ||
_rename(newDomainName, domainName); | ||
|
||
return _rename; | ||
} |
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