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

aws-redshift: table is dropped on table name change #24246

Closed
Rizxcviii opened this issue Feb 21, 2023 · 3 comments · Fixed by #24308
Closed

aws-redshift: table is dropped on table name change #24246

Rizxcviii opened this issue Feb 21, 2023 · 3 comments · Fixed by #24308
Labels
@aws-cdk/aws-redshift Related to Amazon Redshift bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@Rizxcviii
Copy link
Contributor

Rizxcviii commented Feb 21, 2023

Describe the bug

When a table name changes, the entire table is dropped (if the removal policy is DESTROY). Amazon Redshift allows the table name to be altered, but the table is being dropped, even though the original table construct is still intact (the resource ID does not change).

Expected Behavior

Table names should change

Current Behavior

A new redshift table is created, with the old table being dropped.
Provided a DESTROY removal policy, the table, and it's respective data will be deleted.

Reproduction Steps

redshift.Table(
    self,
    id="ConsistentTableId",
    table_name="old_table_name",
    removal_policy=cdk.RemovalPolicy.DESTROY,
    # other attributes needed for redshift table
)

We then change the redshift table name, this will drop the redshift table, and create a new table.

redshift.Table(
    self,
    id="ConsistentTableId",
    table_name="new_table_name",
    removal_policy=cdk.RemovalPolicy.DESTROY,
    # other attributes needed for redshift table
)

Possible Solution

Persist the redshift table when table names change, given the use of the redshift data API, we could use ALTER statements to change the name of the table. Redundant tables that are no longer used in the Stack can still be "removed" by specifying a new id/removing the existing table construct in the code.

Additional Information/Context

Obviously not all developers would add a DESTROY removal policy, but this was not the point of this issue. The point of the issue is the table not persisting through a table name change, but a new table being created. I am simply using a DESTROY removal policy for the worst case scenario, as an example.

Not quite sure if this is intended behaviour. However from my personal development experience, I would say I was surprised that tables were being dropped/being replaced in the stack.

The cdk diff command shows the following changes

[~] Custom::RedshiftDatabaseQuery RedshiftClusterStack/ConsistentTableId/Resource/Resource ConsistentTableId808D9241 
 └─ [~] tableName
     └─ [~] .prefix:
         ├─ [-] old_table_name
         └─ [+] new_table_name

The above shows to myself that we're just changing the table name, not creating a new construct

CDK CLI Version

2.65.0

Framework Version

2.65.0

Node.js Version

v16.16.0

OS

Amazon Linux 2

Language

Python

Language Version

3.7.16

Other information

This might be a feature request... If the original behaviour is intended behaviour.

@Rizxcviii Rizxcviii added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 21, 2023
@github-actions github-actions bot added the @aws-cdk/aws-redshift Related to Amazon Redshift label Feb 21, 2023
@pahud pahud added the needs-reproduction This issue needs reproduction. label Feb 21, 2023
@pahud pahud self-assigned this Feb 21, 2023
@pahud
Copy link
Contributor

pahud commented Feb 21, 2023

I think you are right.

When the provided tableName is changed and stack re-deployed, the following logic will be executed by creating a new table, which should be avoid and execute an ALTER statement instead.

if (tableNamePrefix !== oldTableNamePrefix) {
return createTable(tableNamePrefix, tableNameSuffix, tableColumns, tableAndClusterProps);
}

I am making this as a p2 and any PR submission will be highly appreciated.

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-reproduction This issue needs reproduction. labels Feb 21, 2023
@pahud pahud removed their assignment Feb 21, 2023
@pahud pahud removed the needs-triage This issue or PR still needs to be triaged. label Feb 21, 2023
@Rizxcviii
Copy link
Contributor Author

Currently blocked by #24874

mergify bot pushed a commit that referenced this issue Oct 6, 2023
…26955)

To support solving #24246, there needs to first be a refactor of the UserTablePriviliges to instead record the table id. The reason being that new privileges would be granted/revoked on old tables that now have new names, since a change in table name caused an UPDATE action to be triggered. However the issue remains where, since the table has a new name, the grant/revoke action will be called on an invalid table name (i.e. the old table name).
We now use the table id to track tables, therefore preventing UPDATE events to be triggered.

This blocks #24308

This was originally PR #24874, however that had closed. @kaizencc requested changes, that I had added in here.

This was originally PR #26558, however that had closed. @kaizencc requested changes, that I have already implemented previously. However, he did not review them.

BREAKING CHANGE: the behavior of redshift tables has changed. UPDATE action will not be triggered on new table names and instead be triggered on table id changes.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@mergify mergify bot closed this as completed in #24308 Dec 7, 2023
mergify bot pushed a commit that referenced this issue Dec 7, 2023
Upon a refactor of the `UserTablePrivilieges` now using `tableId` to record changes, this fix will essentially allow table names to be changed.

Closes #24246.

BREAKING CHANGE: Further updates of the Redshift table will fail for existing tables, if the table name is changed. Therefore, changing the table name for existing Redshift tables have been disabled.



----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Copy link

github-actions bot commented Dec 7, 2023

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-redshift Related to Amazon Redshift bug This issue is a bug. effort/medium Medium work item – several days of effort p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants