-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
table.ts
164 lines (139 loc) · 6.67 KB
/
table.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
/* eslint-disable-next-line import/no-unresolved */
import * as AWSLambda from 'aws-lambda';
import { executeStatement } from './redshift-data';
import { ClusterProps, TableAndClusterProps, TableSortStyle } from './types';
import { areColumnsEqual, getDistKeyColumn, getSortKeyColumns } from './util';
import { Column } from '../../table';
export async function handler(props: TableAndClusterProps, event: AWSLambda.CloudFormationCustomResourceEvent) {
const tableNamePrefix = props.tableName.prefix;
const tableNameSuffix = props.tableName.generateSuffix === 'true' ? `${event.RequestId.substring(0, 8)}` : '';
const tableColumns = props.tableColumns;
const tableAndClusterProps = props;
if (event.RequestType === 'Create') {
const tableName = await createTable(tableNamePrefix, tableNameSuffix, tableColumns, tableAndClusterProps);
return { PhysicalResourceId: tableName };
} else if (event.RequestType === 'Delete') {
await dropTable(event.PhysicalResourceId, tableAndClusterProps);
return;
} else if (event.RequestType === 'Update') {
const tableName = await updateTable(
event.PhysicalResourceId,
tableNamePrefix,
tableNameSuffix,
tableColumns,
tableAndClusterProps,
event.OldResourceProperties as TableAndClusterProps,
);
return { PhysicalResourceId: tableName };
} else {
/* eslint-disable-next-line dot-notation */
throw new Error(`Unrecognized event type: ${event['RequestType']}`);
}
}
async function createTable(
tableNamePrefix: string,
tableNameSuffix: string,
tableColumns: Column[],
tableAndClusterProps: TableAndClusterProps,
): Promise<string> {
const tableName = tableNamePrefix + tableNameSuffix;
const tableColumnsString = tableColumns.map(column => `${column.name} ${column.dataType}`).join();
let statement = `CREATE TABLE ${tableName} (${tableColumnsString})`;
if (tableAndClusterProps.distStyle) {
statement += ` DISTSTYLE ${tableAndClusterProps.distStyle}`;
}
const distKeyColumn = getDistKeyColumn(tableColumns);
if (distKeyColumn) {
statement += ` DISTKEY(${distKeyColumn.name})`;
}
const sortKeyColumns = getSortKeyColumns(tableColumns);
if (sortKeyColumns.length > 0) {
const sortKeyColumnsString = getSortKeyColumnsString(sortKeyColumns);
statement += ` ${tableAndClusterProps.sortStyle} SORTKEY(${sortKeyColumnsString})`;
}
await executeStatement(statement, tableAndClusterProps);
if (tableAndClusterProps.tableComment) {
await executeStatement(`COMMENT ON TABLE ${tableName} IS '${tableAndClusterProps.tableComment}'`, tableAndClusterProps);
}
return tableName;
}
async function dropTable(tableName: string, clusterProps: ClusterProps) {
await executeStatement(`DROP TABLE ${tableName}`, clusterProps);
}
async function updateTable(
tableName: string,
tableNamePrefix: string,
tableNameSuffix: string,
tableColumns: Column[],
tableAndClusterProps: TableAndClusterProps,
oldResourceProperties: TableAndClusterProps,
): Promise<string> {
const alterationStatements: string[] = [];
const oldClusterProps = oldResourceProperties;
if (tableAndClusterProps.clusterName !== oldClusterProps.clusterName || tableAndClusterProps.databaseName !== oldClusterProps.databaseName) {
return createTable(tableNamePrefix, tableNameSuffix, tableColumns, tableAndClusterProps);
}
const oldTableNamePrefix = oldResourceProperties.tableName.prefix;
if (tableNamePrefix !== oldTableNamePrefix) {
return createTable(tableNamePrefix, tableNameSuffix, tableColumns, tableAndClusterProps);
}
const oldTableColumns = oldResourceProperties.tableColumns;
const columnDeletions = oldTableColumns.filter(oldColumn => (
tableColumns.every(column => oldColumn.name !== column.name)
));
if (columnDeletions.length > 0) {
alterationStatements.push(...columnDeletions.map(column => `ALTER TABLE ${tableName} DROP COLUMN ${column.name}`));
}
const columnAdditions = tableColumns.filter(column => {
return !oldTableColumns.some(oldColumn => column.name === oldColumn.name && column.dataType === oldColumn.dataType);
}).map(column => `ADD ${column.name} ${column.dataType}`);
if (columnAdditions.length > 0) {
alterationStatements.push(...columnAdditions.map(addition => `ALTER TABLE ${tableName} ${addition}`));
}
const oldDistStyle = oldResourceProperties.distStyle;
if ((!oldDistStyle && tableAndClusterProps.distStyle) ||
(oldDistStyle && !tableAndClusterProps.distStyle)) {
return createTable(tableNamePrefix, tableNameSuffix, tableColumns, tableAndClusterProps);
} else if (oldDistStyle !== tableAndClusterProps.distStyle) {
alterationStatements.push(`ALTER TABLE ${tableName} ALTER DISTSTYLE ${tableAndClusterProps.distStyle}`);
}
const oldDistKey = getDistKeyColumn(oldTableColumns)?.name;
const newDistKey = getDistKeyColumn(tableColumns)?.name;
if ((!oldDistKey && newDistKey ) || (oldDistKey && !newDistKey)) {
return createTable(tableNamePrefix, tableNameSuffix, tableColumns, tableAndClusterProps);
} else if (oldDistKey !== newDistKey) {
alterationStatements.push(`ALTER TABLE ${tableName} ALTER DISTKEY ${newDistKey}`);
}
const oldSortKeyColumns = getSortKeyColumns(oldTableColumns);
const newSortKeyColumns = getSortKeyColumns(tableColumns);
const oldSortStyle = oldResourceProperties.sortStyle;
const newSortStyle = tableAndClusterProps.sortStyle;
if ((oldSortStyle === newSortStyle && !areColumnsEqual(oldSortKeyColumns, newSortKeyColumns))
|| (oldSortStyle !== newSortStyle)) {
switch (newSortStyle) {
case TableSortStyle.INTERLEAVED:
// INTERLEAVED sort key addition requires replacement.
// https://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_TABLE.html
return createTable(tableNamePrefix, tableNameSuffix, tableColumns, tableAndClusterProps);
case TableSortStyle.COMPOUND: {
const sortKeyColumnsString = getSortKeyColumnsString(newSortKeyColumns);
alterationStatements.push(`ALTER TABLE ${tableName} ALTER ${newSortStyle} SORTKEY(${sortKeyColumnsString})`);
break;
}
case TableSortStyle.AUTO: {
alterationStatements.push(`ALTER TABLE ${tableName} ALTER SORTKEY ${newSortStyle}`);
break;
}
}
}
const oldComment = oldResourceProperties.tableComment;
const newComment = tableAndClusterProps.tableComment;
if (oldComment !== newComment) {
alterationStatements.push(`COMMENT ON TABLE ${tableName} IS ${newComment ? `'${newComment}'` : 'NULL'}`);
}
await Promise.all(alterationStatements.map(statement => executeStatement(statement, tableAndClusterProps)));
return tableName;
}
function getSortKeyColumnsString(sortKeyColumns: Column[]) {
return sortKeyColumns.map(column => column.name).join();
}