-
Notifications
You must be signed in to change notification settings - Fork 180
/
alterView.ts
55 lines (44 loc) · 1.41 KB
/
alterView.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
import type { MigrationOptions } from '../../types';
import type { Name, Nullable } from '../generalTypes';
import type { ViewOptions } from './shared';
import { viewOptionStr } from './shared';
export interface AlterViewOptions {
checkOption?: null | 'CASCADED' | 'LOCAL';
options?: Nullable<ViewOptions>;
}
export type AlterView = (
viewName: Name,
viewOptions: AlterViewOptions
) => string;
export function alterView(mOptions: MigrationOptions): AlterView {
const _alter: AlterView = (viewName, viewOptions) => {
const { checkOption, options = {} } = viewOptions;
if (checkOption !== undefined) {
if (options.check_option === undefined) {
options.check_option = checkOption;
} else {
throw new Error(
'"options.check_option" and "checkOption" can\'t be specified together'
);
}
}
const clauses: string[] = [];
const withOptions = Object.keys(options)
.filter((key) => options[key] !== null)
.map(viewOptionStr(options))
.join(', ');
if (withOptions) {
clauses.push(`SET (${withOptions})`);
}
const resetOptions = Object.keys(options)
.filter((key) => options[key] === null)
.join(', ');
if (resetOptions) {
clauses.push(`RESET (${resetOptions})`);
}
return clauses
.map((clause) => `ALTER VIEW ${mOptions.literal(viewName)} ${clause};`)
.join('\n');
};
return _alter;
}