Skip to content

Commit

Permalink
fix(aws-redshift): Columns are not dropped on removal from array (aws…
Browse files Browse the repository at this point in the history
…#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 aws#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*
  • Loading branch information
Rizxcviii authored and Brennan Ho committed Jan 20, 2023
1 parent f2fb430 commit 9350e93
Show file tree
Hide file tree
Showing 29 changed files with 672 additions and 413 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ async function updateTable(
}

const oldTableColumns = oldResourceProperties.tableColumns;
if (!oldTableColumns.every(oldColumn => tableColumns.some(column => column.name === oldColumn.name && column.dataType === oldColumn.dataType))) {
return createTable(tableNamePrefix, tableNameSuffix, tableColumns, tableAndClusterProps);
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 => {
Expand Down
4 changes: 3 additions & 1 deletion packages/@aws-cdk/aws-redshift/lib/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export enum TableAction {
*/
export interface Column {
/**
* The name of the column.
* The unique name/identifier of the column.
*
* **NOTE**. After deploying this column, you cannot change its name. Doing so will cause the column to be dropped and recreated.
*/
readonly name: string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,17 @@ describe('update', () => {
}));
});

test('replaces if table columns change', async () => {
const newTableColumnName = 'col2';
const newTableColumnDataType = 'varchar(1)';
const newTableColumns = [{ name: newTableColumnName, dataType: newTableColumnDataType }];
test('does not replace if table columns removed', async () => {
const newResourceProperties = {
...resourceProperties,
tableColumns: newTableColumns,
tableColumns: [],
};

await expect(manageTable(newResourceProperties, event)).resolves.not.toMatchObject({
await expect(manageTable(newResourceProperties, event)).resolves.toMatchObject({
PhysicalResourceId: physicalResourceId,
});
expect(mockExecuteStatement).toHaveBeenCalledWith(expect.objectContaining({
Sql: `CREATE TABLE ${tableNamePrefix}${requestIdTruncated} (${newTableColumnName} ${newTableColumnDataType})`,
Sql: `ALTER TABLE ${physicalResourceId} DROP COLUMN col1`,
}));
});

Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 9350e93

Please sign in to comment.