-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathviews.ts
160 lines (129 loc) · 4.42 KB
/
views.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import type { MigrationOptions } from '../types';
import { escapeValue } from '../utils';
import type { Nullable } from './generalTypes';
import type {
AlterView,
AlterViewColumn,
CreateView,
DropView,
RenameView,
ViewOptions,
} from './viewsTypes';
export type {
CreateView,
DropView,
AlterView,
AlterViewColumn,
RenameView,
ViewOptions,
};
const viewOptionStr =
<TViewOptions extends Nullable<ViewOptions>, TKey extends keyof TViewOptions>(
options: TViewOptions
) =>
(key: TKey) => {
const value = options[key] === true ? '' : ` = ${options[key]}`;
// @ts-expect-error: Implicit conversion of a 'symbol' to a 'string' will fail at runtime. Consider wrapping this expression in 'String(...)'. ts(2731)
return `${key}${value}`;
};
export function dropView(mOptions: MigrationOptions): DropView {
const _drop: DropView = (viewName, options = {}) => {
const { ifExists, cascade } = options;
const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const cascadeStr = cascade ? ' CASCADE' : '';
const viewNameStr = mOptions.literal(viewName);
return `DROP VIEW${ifExistsStr} ${viewNameStr}${cascadeStr};`;
};
return _drop;
}
export function createView(mOptions: MigrationOptions): CreateView {
const _create: CreateView = (viewName, viewOptions, definition) => {
const {
temporary,
replace,
recursive,
columns = [],
options = {},
checkOption,
} = viewOptions;
const columnNames = (Array.isArray(columns) ? columns : [columns])
.map(mOptions.literal)
.join(', ');
const withOptions = Object.keys(options)
.map(viewOptionStr(options))
.join(', ');
const replaceStr = replace ? ' OR REPLACE' : '';
const temporaryStr = temporary ? ' TEMPORARY' : '';
const recursiveStr = recursive ? ' RECURSIVE' : '';
const columnStr = columnNames ? `(${columnNames})` : '';
const withOptionsStr = withOptions ? ` WITH (${withOptions})` : '';
const checkOptionStr = checkOption
? ` WITH ${checkOption} CHECK OPTION`
: '';
const viewNameStr = mOptions.literal(viewName);
return `CREATE${replaceStr}${temporaryStr}${recursiveStr} VIEW ${viewNameStr}${columnStr}${withOptionsStr} AS ${definition}${checkOptionStr};`;
};
_create.reverse = dropView(mOptions);
return _create;
}
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;
}
export function alterViewColumn(mOptions: MigrationOptions): AlterViewColumn {
const _alter: AlterViewColumn = (viewName, columnName, options) => {
const { default: defaultValue } = options;
const actions: string[] = [];
if (defaultValue === null) {
actions.push('DROP DEFAULT');
} else if (defaultValue !== undefined) {
actions.push(`SET DEFAULT ${escapeValue(defaultValue)}`);
}
const viewNameStr = mOptions.literal(viewName);
const columnNameStr = mOptions.literal(columnName);
return actions
.map(
(action) =>
`ALTER VIEW ${viewNameStr} ALTER COLUMN ${columnNameStr} ${action};`
)
.join('\n');
};
return _alter;
}
export function renameView(mOptions: MigrationOptions): RenameView {
const _rename: RenameView = (viewName, newViewName) => {
const viewNameStr = mOptions.literal(viewName);
const newViewNameStr = mOptions.literal(newViewName);
return `ALTER VIEW ${viewNameStr} RENAME TO ${newViewNameStr};`;
};
_rename.reverse = (viewName, newViewName) => _rename(newViewName, viewName);
return _rename;
}