Skip to content

Commit

Permalink
fix(postgresql_grant): getting stuck on refreshing (#351)
Browse files Browse the repository at this point in the history
This change fixes terraform refresh getting stuck forever on
postgresql_grant schema when provider connection database is the same as
the one we are trying to read schema information from. Until now the
issue could be reproduced only on RDS (PostgreSQL 14.7). 

Fixes #335
  • Loading branch information
giner authored Mar 2, 2024
1 parent c7b49a3 commit 1338df7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
run:
timeout: 5m

issues:
exclude-rules:
- linters:
Expand Down
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
40 changes: 16 additions & 24 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,29 +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
}

// 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 1338df7

Please sign in to comment.