-
Notifications
You must be signed in to change notification settings - Fork 180
/
alterDomain.ts
49 lines (40 loc) · 1.26 KB
/
alterDomain.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import type { MigrationOptions } from '../../types';
import { escapeValue } from '../../utils';
import type { Name } from '../generalTypes';
import type { DomainOptions } from './shared';
export interface AlterDomainOptions extends DomainOptions {
allowNull?: boolean;
}
export type AlterDomain = (
domainName: Name,
domainOptions: AlterDomainOptions
) => string;
export function alterDomain(mOptions: MigrationOptions): AlterDomain {
const _alter: AlterDomain = (domainName, options) => {
const {
default: defaultValue,
notNull,
allowNull = false,
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;
}