Skip to content

Commit 2981313

Browse files
authored
fix(aws-redshift): Columns are not dropped on removal from array (#23011)
When attempting to a drop a column from the array, this would cause an error similar to that of the following ```Received response status [FAILED] from custom resource. Message returned: Statement status was FAILED: ERROR: column "column" of relation "table” already exists``` fixes #22208 ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6bae8c9 commit 2981313

File tree

29 files changed

+672
-413
lines changed

29 files changed

+672
-413
lines changed

packages/@aws-cdk/aws-redshift/lib/private/database-query-provider/table.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,11 @@ async function updateTable(
8888
}
8989

9090
const oldTableColumns = oldResourceProperties.tableColumns;
91-
if (!oldTableColumns.every(oldColumn => tableColumns.some(column => column.name === oldColumn.name && column.dataType === oldColumn.dataType))) {
92-
return createTable(tableNamePrefix, tableNameSuffix, tableColumns, tableAndClusterProps);
91+
const columnDeletions = oldTableColumns.filter(oldColumn => (
92+
tableColumns.every(column => oldColumn.name !== column.name)
93+
));
94+
if (columnDeletions.length > 0) {
95+
alterationStatements.push(...columnDeletions.map(column => `ALTER TABLE ${tableName} DROP COLUMN ${column.name}`));
9396
}
9497

9598
const columnAdditions = tableColumns.filter(column => {

packages/@aws-cdk/aws-redshift/lib/table.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ export enum TableAction {
5555
*/
5656
export interface Column {
5757
/**
58-
* The name of the column.
58+
* The unique name/identifier of the column.
59+
*
60+
* **NOTE**. After deploying this column, you cannot change its name. Doing so will cause the column to be dropped and recreated.
5961
*/
6062
readonly name: string;
6163

packages/@aws-cdk/aws-redshift/test/database-query-provider/table.test.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,20 +225,17 @@ describe('update', () => {
225225
}));
226226
});
227227

228-
test('replaces if table columns change', async () => {
229-
const newTableColumnName = 'col2';
230-
const newTableColumnDataType = 'varchar(1)';
231-
const newTableColumns = [{ name: newTableColumnName, dataType: newTableColumnDataType }];
228+
test('does not replace if table columns removed', async () => {
232229
const newResourceProperties = {
233230
...resourceProperties,
234-
tableColumns: newTableColumns,
231+
tableColumns: [],
235232
};
236233

237-
await expect(manageTable(newResourceProperties, event)).resolves.not.toMatchObject({
234+
await expect(manageTable(newResourceProperties, event)).resolves.toMatchObject({
238235
PhysicalResourceId: physicalResourceId,
239236
});
240237
expect(mockExecuteStatement).toHaveBeenCalledWith(expect.objectContaining({
241-
Sql: `CREATE TABLE ${tableNamePrefix}${requestIdTruncated} (${newTableColumnName} ${newTableColumnDataType})`,
238+
Sql: `ALTER TABLE ${physicalResourceId} DROP COLUMN col1`,
242239
}));
243240
});
244241

packages/@aws-cdk/aws-redshift/test/integ.database.js.snapshot/asset.3b263c2ad043fd069ef446753788c36e595c82b51a70478e58258c8ef7471671/cfn-response.js

Lines changed: 0 additions & 83 deletions
This file was deleted.

packages/@aws-cdk/aws-redshift/test/integ.database.js.snapshot/asset.3b263c2ad043fd069ef446753788c36e595c82b51a70478e58258c8ef7471671/outbound.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)