-
Notifications
You must be signed in to change notification settings - Fork 212
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
Changes from 7 commits
3298f52
2d4540e
11038f6
5ad83f8
43d570a
6bae9c6
cf1c0ce
0d380aa
7936df2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,16 +14,17 @@ import ( | |
) | ||
|
||
const ( | ||
dbAllowConnsAttr = "allow_connections" | ||
dbCTypeAttr = "lc_ctype" | ||
dbCollationAttr = "lc_collate" | ||
dbConnLimitAttr = "connection_limit" | ||
dbEncodingAttr = "encoding" | ||
dbIsTemplateAttr = "is_template" | ||
dbNameAttr = "name" | ||
dbOwnerAttr = "owner" | ||
dbTablespaceAttr = "tablespace_name" | ||
dbTemplateAttr = "template" | ||
dbAllowConnsAttr = "allow_connections" | ||
dbCTypeAttr = "lc_ctype" | ||
dbCollationAttr = "lc_collate" | ||
dbConnLimitAttr = "connection_limit" | ||
dbEncodingAttr = "encoding" | ||
dbIsTemplateAttr = "is_template" | ||
dbNameAttr = "name" | ||
dbOwnerAttr = "owner" | ||
dbTablespaceAttr = "tablespace_name" | ||
dbTemplateAttr = "template" | ||
dbAlterObjectOwnership = "alter_object_ownership" | ||
) | ||
|
||
func resourcePostgreSQLDatabase() *schema.Resource { | ||
|
@@ -102,6 +103,12 @@ func resourcePostgreSQLDatabase() *schema.Resource { | |
Computed: true, | ||
Description: "If true, then this database can be cloned by any user with CREATEDB privileges", | ||
}, | ||
dbAlterObjectOwnership: { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
Description: "If true, the owner of already existing objects will change if the owner changes", | ||
}, | ||
}, | ||
} | ||
} | ||
|
@@ -393,6 +400,10 @@ func resourcePostgreSQLDatabaseUpdate(db *DBConnection, d *schema.ResourceData) | |
return err | ||
} | ||
|
||
if err := setAlterOwnership(db, d); err != nil { | ||
return err | ||
} | ||
|
||
if err := setDBOwner(db, d); err != nil { | ||
return err | ||
} | ||
|
@@ -468,6 +479,7 @@ func setDBOwner(db *DBConnection, d *schema.ResourceData) error { | |
} | ||
|
||
dbName := d.Get(dbNameAttr).(string) | ||
|
||
sql := fmt.Sprintf("ALTER DATABASE %s OWNER TO %s", pq.QuoteIdentifier(dbName), pq.QuoteIdentifier(owner)) | ||
if _, err := db.Exec(sql); err != nil { | ||
return fmt.Errorf("Error updating database OWNER: %w", err) | ||
|
@@ -476,6 +488,57 @@ 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if only At least you should check if There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
cyrilgdn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
owner := d.Get(dbOwnerAttr).(string) | ||
if owner == "" { | ||
return nil | ||
} | ||
|
||
alterOwnership := d.Get(dbAlterObjectOwnership).(bool) | ||
if !alterOwnership { | ||
return nil | ||
} | ||
currentUser := db.client.config.getDatabaseUsername() | ||
|
||
dbName := d.Get(dbNameAttr).(string) | ||
|
||
lockTxn, err := startTransaction(db.client, dbName) | ||
if err := pgLockRole(lockTxn, currentUser); err != nil { | ||
return err | ||
} | ||
defer lockTxn.Commit() | ||
|
||
currentOwner, err := getDatabaseOwner(db, dbName) | ||
if err != nil { | ||
return fmt.Errorf("Error getting current database OWNER: %w", err) | ||
} | ||
|
||
newOwner := d.Get(dbOwnerAttr).(string) | ||
|
||
if currentOwner == newOwner { | ||
return nil | ||
} | ||
|
||
currentOwnerGranted, err := grantRoleMembership(db, currentOwner, currentUser) | ||
if err != nil { | ||
return err | ||
} | ||
if currentOwnerGranted { | ||
defer func() { | ||
_, err = revokeRoleMembership(db, currentOwner, currentUser) | ||
}() | ||
} | ||
sql := fmt.Sprintf("REASSIGN OWNED BY %s TO %s", pq.QuoteIdentifier(currentOwner), pq.QuoteIdentifier(newOwner)) | ||
if _, err := lockTxn.Exec(sql); err != nil { | ||
return fmt.Errorf("Error reassigning objects owned by '%s': %w", currentOwner, err) | ||
} | ||
return nil | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func setDBTablespace(db QueryAble, d *schema.ResourceData) error { | ||
if !d.HasChange(dbTablespaceAttr) { | ||
return nil | ||
|
There was a problem hiding this comment.
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)There was a problem hiding this comment.
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 notThere was a problem hiding this comment.
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 👍