-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathdomains.ts
124 lines (97 loc) · 3.3 KB
/
domains.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import type { MigrationOptions } from '../types';
import { applyType, escapeValue } from '../utils';
import type {
AlterDomain,
CreateDomain,
DropDomain,
RenameDomain,
} from './domainsTypes';
export type { CreateDomain, DropDomain, AlterDomain, RenameDomain };
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;
}
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;
}
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;
}
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;
}