Skip to content

Commit

Permalink
Unify function checkRoleDBSchemaExists
Browse files Browse the repository at this point in the history
- Pass DBConnection instead of Client (the same as in other functions)
- Check database existence with a simple query outside of a transaction
- Use a single transaction for queries within the function
  • Loading branch information
giner committed Mar 1, 2024
1 parent f2f32ab commit 19a04cd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
2 changes: 1 addition & 1 deletion postgresql/resource_postgresql_default_privileges.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func resourcePostgreSQLDefaultPrivilegesRead(db *DBConnection, d *schema.Resourc
)
}

exists, err := checkRoleDBSchemaExists(db.client, d)
exists, err := checkRoleDBSchemaExists(db, d)
if err != nil {
return err
}
Expand Down
42 changes: 16 additions & 26 deletions postgresql/resource_postgresql_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func resourcePostgreSQLGrantRead(db *DBConnection, d *schema.ResourceData) error
return fmt.Errorf("feature is not supported: %v", err)
}

exists, err := checkRoleDBSchemaExists(db.client, d)
exists, err := checkRoleDBSchemaExists(db, d)
if err != nil {
return err
}
Expand Down Expand Up @@ -699,8 +699,19 @@ func revokeRolePrivileges(txn *sql.Tx, d *schema.ResourceData) error {
return nil
}

func checkRoleDBSchemaExists(client *Client, d *schema.ResourceData) (bool, error) {
txn, err := startTransaction(client, "")
func checkRoleDBSchemaExists(db *DBConnection, d *schema.ResourceData) (bool, error) {
// Check the database exists
database := d.Get("database").(string)
exists, err := dbExists(db, database)
if err != nil {
return false, err
}
if !exists {
log.Printf("[DEBUG] database %s does not exists", database)
return false, nil
}

txn, err := startTransaction(db.client, database)
if err != nil {
return false, err
}
Expand All @@ -719,31 +730,10 @@ func checkRoleDBSchemaExists(client *Client, d *schema.ResourceData) (bool, erro
}
}

// Check the database exists
database := d.Get("database").(string)
exists, err := dbExists(txn, database)
if err != nil {
return false, err
}
if !exists {
log.Printf("[DEBUG] database %s does not exists", database)
return false, nil
}

deferredRollback(txn)

// Check the schema exists (the SQL connection needs to be on the right database)
pgSchema := d.Get("schema").(string)

if !sliceContainsStr([]string{"database", "foreign_data_wrapper", "foreign_server"}, d.Get("object_type").(string)) && pgSchema != "" {
// Connect on this database to check if schema exists
dbTxn, err := startTransaction(client, database)
if err != nil {
return false, err
}
defer deferredRollback(dbTxn)

// Check the schema exists (the SQL connection needs to be on the right database)
exists, err = schemaExists(dbTxn, pgSchema)
exists, err = schemaExists(txn, pgSchema)
if err != nil {
return false, err
}
Expand Down

0 comments on commit 19a04cd

Please sign in to comment.