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

Fix getting stuck on refreshing postgresql grant #351

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading