Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(aws-redshift): Columns would not be deleted upon removal from the array #22974

Closed
wants to merge 11 commits into from
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
Original file line number Diff line number Diff line change
Expand Up @@ -225,37 +225,34 @@ describe('update', () => {
}));
});

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

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} ADD ${newTableColumnName} ${newTableColumnDataType}`,
}));
});

test('does not replace if table columns added', async () => {
const newTableColumnName = 'col2';
const newTableColumnDataType = 'varchar(1)';
const newTableColumns = [{ name: 'col1', dataType: 'varchar(1)' }, { 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.toMatchObject({
PhysicalResourceId: physicalResourceId,
});
expect(mockExecuteStatement).toHaveBeenCalledWith(expect.objectContaining({
Sql: `ALTER TABLE ${physicalResourceId} ADD ${newTableColumnName} ${newTableColumnDataType}`,
Sql: `ALTER TABLE ${physicalResourceId} DROP COLUMN col1`,
}));
});

Expand Down

This file was deleted.

This file was deleted.

Loading