Skip to content

Commit 8c9f0e2

Browse files
authored
fix(redshift): adding distKey to an existing table fails deployment (#26789)
The current implementation executes a `CREATE TABLE` if the table has no `distKey` specified and one is added, or if a `distKey` is present and is removed. The resulting table is created with the same name, causing the update operation to fail. This fixes the problem by using [`ALTER TABLE`](https://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_TABLE.html) when adding/removing `distKey` on a table update. Closes #26733. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent ecb34bb commit 8c9f0e2

26 files changed

+6264
-8
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,14 @@ async function updateTable(
164164

165165
const oldDistKey = getDistKeyColumn(oldTableColumns)?.name;
166166
const newDistKey = getDistKeyColumn(tableColumns)?.name;
167-
if ((!oldDistKey && newDistKey ) || (oldDistKey && !newDistKey)) {
168-
return createTable(tableNamePrefix, tableNameSuffix, tableColumns, tableAndClusterProps);
167+
if (!oldDistKey && newDistKey) {
168+
// Table has no existing distribution key, add a new one
169+
alterationStatements.push(`ALTER TABLE ${tableName} ALTER DISTSTYLE KEY DISTKEY ${newDistKey}`);
170+
} else if (oldDistKey && !newDistKey) {
171+
// Table has a distribution key, remove and set to AUTO
172+
alterationStatements.push(`ALTER TABLE ${tableName} ALTER DISTSTYLE AUTO`);
169173
} else if (oldDistKey !== newDistKey) {
174+
// Table has an existing distribution key, change it
170175
alterationStatements.push(`ALTER TABLE ${tableName} ALTER DISTKEY ${newDistKey}`);
171176
}
172177

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -384,21 +384,21 @@ describe('update', () => {
384384
}));
385385
});
386386

387-
test('replaces if distKey is added', async () => {
387+
test('adds key without creating table if distKey is added', async () => {
388388
const newResourceProperties: ResourcePropertiesType = {
389389
...resourceProperties,
390390
tableColumns: [{ name: 'col1', dataType: 'varchar(1)', distKey: true }],
391391
};
392392

393-
await expect(manageTable(newResourceProperties, event)).resolves.not.toMatchObject({
393+
await expect(manageTable(newResourceProperties, event)).resolves.toMatchObject({
394394
PhysicalResourceId: physicalResourceId,
395395
});
396396
expect(mockExecuteStatement).toHaveBeenCalledWith(expect.objectContaining({
397-
Sql: `CREATE TABLE ${tableNamePrefix}${requestIdTruncated} (col1 varchar(1)) DISTKEY(col1)`,
397+
Sql: `ALTER TABLE ${physicalResourceId} ALTER DISTSTYLE KEY DISTKEY col1`,
398398
}));
399399
});
400400

401-
test('replaces if distKey is removed', async () => {
401+
test('removes key without replacing table if distKey is removed', async () => {
402402
const newEvent: AWSLambda.CloudFormationCustomResourceEvent = {
403403
...event,
404404
OldResourceProperties: {
@@ -410,11 +410,11 @@ describe('update', () => {
410410
...resourceProperties,
411411
};
412412

413-
await expect(manageTable(newResourceProperties, newEvent)).resolves.not.toMatchObject({
413+
await expect(manageTable(newResourceProperties, newEvent)).resolves.toMatchObject({
414414
PhysicalResourceId: physicalResourceId,
415415
});
416416
expect(mockExecuteStatement).toHaveBeenCalledWith(expect.objectContaining({
417-
Sql: `CREATE TABLE ${tableNamePrefix}${requestIdTruncated} (col1 varchar(1))`,
417+
Sql: `ALTER TABLE ${physicalResourceId} ALTER DISTSTYLE AUTO`,
418418
}));
419419
});
420420

packages/@aws-cdk/aws-redshift-alpha/test/integ.cluster-distkey.js.snapshot/asset.73b60c2cf141bf58c33cfaa33858f5c84103a0232ba7192d696536488f7731c4/cfn-response.js

+87
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-redshift-alpha/test/integ.cluster-distkey.js.snapshot/asset.73b60c2cf141bf58c33cfaa33858f5c84103a0232ba7192d696536488f7731c4/consts.js

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)