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

postgresql_database: Reassign objects owners if database owner changes #458

Merged

Conversation

lukaalba
Copy link
Contributor

Hi,

this is my first Pull Request, so if something is missing or wrong, let me know.

To resolve 439 I suggest to add an additional parameter to the database resource called alter_object_ownership. If set to true, it will issue a REASSIGN OWNED BY query on the database when the owner changes, transfer the whole ownership in the database from the previous owner to the new one.

As far as I can tell this is only possible if the user in the provider configuration is a superuser or a user which has grants for both the previous owner role and the new one. So I skipped the test in the rds-like test suite.

Copy link
Owner

@cyrilgdn cyrilgdn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lukaalba,

Thanks for your work on that 💪 (and to add the acceptance tests 🙏 )

I've just allowed myself to push a small modification in the tests to drop the role you created after each test so we can execute the same tests multiple times (without recreating the whole database server)

I have also one question/comment on the main function.

But otherwise it seems good to me 👍

@@ -476,6 +488,54 @@ func setDBOwner(db *DBConnection, d *schema.ResourceData) error {
return err
}

func setAlterOwnership(db *DBConnection, d *schema.ResourceData) error {
if d.HasChange(dbOwnerAttr) || d.HasChange(dbAlterObjectOwnership) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if only dbAlterObjectOwnership changes, it seems you will execute the REASSIGN with the same previous and new owner so I'm not sure to see the point on this case 🤔

At least you should check if currentOwner and newOwner are different below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. If updated the code so it checks if currentOwner and newOwner are equal before executing the sql statements

@@ -393,6 +400,10 @@ func resourcePostgreSQLDatabaseUpdate(db *DBConnection, d *schema.ResourceData)
return err
}

if err := setAlterOwnership(db, d); err != nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does more than setting a database "parameter", probably this function could have better name
(something like reassignOwnership or similar)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I update the whole name for the flag then? I don't want to break the naming conventions of the functions called in the resourcePostgreSQLDatabaseUpdate, so I'm not sure if it might be misleading if the function name changes, but the flag itself not

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I get the point, we can keep this function name to be consistent with the rest of the functions then 👍

@@ -393,6 +400,10 @@ func resourcePostgreSQLDatabaseUpdate(db *DBConnection, d *schema.ResourceData)
return err
}

if err := setAlterOwnership(db, d); err != nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I get the point, we can keep this function name to be consistent with the rest of the functions then 👍

postgresql/resource_postgresql_database.go Outdated Show resolved Hide resolved
@cyrilgdn
Copy link
Owner

cyrilgdn commented Sep 8, 2024

I've just fix the used of the transaction (which fixes at the same time the golanci-lint issue), we prefer to defer the rollback and commit, otherwise it will try to commit in any cases .

@cyrilgdn cyrilgdn changed the title Update ownership of existing objects if database owner changes postgresql_database: Reassign objects owners if database owner changes Sep 8, 2024
@cyrilgdn cyrilgdn merged commit 59a36ae into cyrilgdn:main Sep 8, 2024
6 checks passed
@cyrilgdn
Copy link
Owner

cyrilgdn commented Sep 8, 2024

@lukaalba This has just been released in v1.23.0

@lukaalba lukaalba deleted the update-ownership-of-previous-objects branch September 9, 2024 06:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants