-
Notifications
You must be signed in to change notification settings - Fork 168
/
viewsMaterialized.ts
191 lines (153 loc) · 5.49 KB
/
viewsMaterialized.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import type { MigrationOptions } from '../types';
import { formatLines } from '../utils';
import type { Nullable } from './generalTypes';
import type {
AlterMaterializedView,
CreateMaterializedView,
DropMaterializedView,
RefreshMaterializedView,
RenameMaterializedView,
RenameMaterializedViewColumn,
StorageParameters,
} from './viewsMaterializedTypes';
export type {
CreateMaterializedView,
DropMaterializedView,
AlterMaterializedView,
RenameMaterializedView,
RenameMaterializedViewColumn,
RefreshMaterializedView,
};
const dataClause = (data?: boolean) =>
data !== undefined ? ` WITH${data ? '' : ' NO'} DATA` : '';
const storageParameterStr =
<
TStorageParameters extends Nullable<StorageParameters>,
TKey extends keyof TStorageParameters,
>(
storageParameters: TStorageParameters
) =>
(key: TKey) => {
const value =
storageParameters[key] === true ? '' : ` = ${storageParameters[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 dropMaterializedView(
mOptions: MigrationOptions
): DropMaterializedView {
const _drop: DropMaterializedView = (viewName, options = {}) => {
const { ifExists, cascade } = options;
const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const cascadeStr = cascade ? ' CASCADE' : '';
const viewNameStr = mOptions.literal(viewName);
return `DROP MATERIALIZED VIEW${ifExistsStr} ${viewNameStr}${cascadeStr};`;
};
return _drop;
}
export function createMaterializedView(
mOptions: MigrationOptions
): CreateMaterializedView {
const _create: CreateMaterializedView = (viewName, options, definition) => {
const {
ifNotExists,
columns = [],
tablespace,
storageParameters = {},
data,
} = options;
const columnNames = (Array.isArray(columns) ? columns : [columns])
.map(mOptions.literal)
.join(', ');
const withOptions = Object.keys(storageParameters)
.map(storageParameterStr(storageParameters))
.join(', ');
const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : '';
const columnsStr = columnNames ? `(${columnNames})` : '';
const withOptionsStr = withOptions ? ` WITH (${withOptions})` : '';
const tablespaceStr = tablespace
? ` TABLESPACE ${mOptions.literal(tablespace)}`
: '';
const dataStr = dataClause(data);
const viewNameStr = mOptions.literal(viewName);
return `CREATE MATERIALIZED VIEW${ifNotExistsStr} ${viewNameStr}${columnsStr}${withOptionsStr}${tablespaceStr} AS ${definition}${dataStr};`;
};
_create.reverse = dropMaterializedView(mOptions);
return _create;
}
export function alterMaterializedView(
mOptions: MigrationOptions
): AlterMaterializedView {
const _alter: AlterMaterializedView = (viewName, options) => {
const { cluster, extension, storageParameters = {} } = options;
const clauses: string[] = [];
if (cluster !== undefined) {
if (cluster) {
clauses.push(`CLUSTER ON ${mOptions.literal(cluster)}`);
} else {
clauses.push('SET WITHOUT CLUSTER');
}
}
if (extension) {
clauses.push(`DEPENDS ON EXTENSION ${mOptions.literal(extension)}`);
}
const withOptions = Object.keys(storageParameters)
.filter((key) => storageParameters[key] !== null)
.map(storageParameterStr(storageParameters))
.join(', ');
if (withOptions) {
clauses.push(`SET (${withOptions})`);
}
const resetOptions = Object.keys(storageParameters)
.filter((key) => storageParameters[key] === null)
.join(', ');
if (resetOptions) {
clauses.push(`RESET (${resetOptions})`);
}
const clausesStr = formatLines(clauses);
const viewNameStr = mOptions.literal(viewName);
return `ALTER MATERIALIZED VIEW ${viewNameStr}\n${clausesStr};`;
};
return _alter;
}
export function renameMaterializedView(
mOptions: MigrationOptions
): RenameMaterializedView {
const _rename: RenameMaterializedView = (viewName, newViewName) => {
const viewNameStr = mOptions.literal(viewName);
const newViewNameStr = mOptions.literal(newViewName);
return `ALTER MATERIALIZED VIEW ${viewNameStr} RENAME TO ${newViewNameStr};`;
};
_rename.reverse = (viewName, newViewName) => _rename(newViewName, viewName);
return _rename;
}
export function renameMaterializedViewColumn(
mOptions: MigrationOptions
): RenameMaterializedViewColumn {
const _rename: RenameMaterializedViewColumn = (
viewName,
columnName,
newColumnName
) => {
const viewNameStr = mOptions.literal(viewName);
const columnNameStr = mOptions.literal(columnName);
const newColumnNameStr = mOptions.literal(newColumnName);
return `ALTER MATERIALIZED VIEW ${viewNameStr} RENAME COLUMN ${columnNameStr} TO ${newColumnNameStr};`;
};
_rename.reverse = (viewName, columnName, newColumnName) =>
_rename(viewName, newColumnName, columnName);
return _rename;
}
export function refreshMaterializedView(
mOptions: MigrationOptions
): RefreshMaterializedView {
const _refresh: RefreshMaterializedView = (viewName, options = {}) => {
const { concurrently, data } = options;
const concurrentlyStr = concurrently ? ' CONCURRENTLY' : '';
const dataStr = dataClause(data);
const viewNameStr = mOptions.literal(viewName);
return `REFRESH MATERIALIZED VIEW${concurrentlyStr} ${viewNameStr}${dataStr};`;
};
_refresh.reverse = _refresh;
return _refresh;
}