-
Notifications
You must be signed in to change notification settings - Fork 180
/
alterViewColumn.ts
39 lines (31 loc) · 1.03 KB
/
alterViewColumn.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
import type { MigrationOptions } from '../../types';
import { escapeValue } from '../../utils';
import type { Name, Value } from '../generalTypes';
export interface AlterViewColumnOptions {
default?: Value;
}
export type AlterViewColumn = (
viewName: Name,
columnName: string,
viewColumnOptions: AlterViewColumnOptions
) => string;
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;
}