Skip to content

Commit

Permalink
Merge pull request #13 from Riskified/DEV-87880-fix-pg_auth_members
Browse files Browse the repository at this point in the history
fix readGrantRole
  • Loading branch information
TomaBere authored Apr 21, 2024
2 parents 4c5151d + f10ed35 commit a968f52
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ jobs:
uses: actions/setup-go@v4
with:
go-version: 1.20.x
skip-pkg-cache: true

- run: go mod vendor

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.53
skip-pkg-cache: true
41 changes: 24 additions & 17 deletions postgresql/resource_postgresql_grant_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func resourcePostgreSQLGrantRoleDelete(db *DBConnection, d *schema.ResourceData)
return nil
}

func readGrantRole(db QueryAble, d *schema.ResourceData) error {
func readGrantRole(db *DBConnection, d *schema.ResourceData) error {
var roleName, grantRoleName string
var withAdminOption bool

Expand All @@ -135,23 +135,30 @@ func readGrantRole(db QueryAble, d *schema.ResourceData) error {
&grantRoleName,
&withAdminOption,
}

err := db.QueryRow(getGrantRoleQuery, d.Get("role"), d.Get("grant_role")).Scan(values...)
switch {
case err == sql.ErrNoRows:
log.Printf("[WARN] PostgreSQL grant role (%q) not found", grantRoleID)
d.SetId("")
return nil
case err != nil:
return fmt.Errorf("Error reading grant role: %w", err)
var query string
if db.dbType == dbTypeCockroachdb {
query = fmt.Sprintf(` with a as (show grants on role %s for %s) select member as role , role_name as grant_role, is_admin as with_admin_option from a;
`, d.Get("grant_role"), d.Get("role"))
if err := db.QueryRow(query).Scan(values...); err != nil {
return fmt.Errorf("Error to show grants on role %s for %s :%w ", d.Get("grant_role"), d.Get("role"), err)
}
} else {
err := db.QueryRow(getGrantRoleQuery, d.Get("role"), d.Get("grant_role")).Scan(values...)
switch {
case err == sql.ErrNoRows:
log.Printf("[WARN] PostgreSQL grant role (%q) not found", grantRoleID)
d.SetId("")
return nil
case err != nil:
return fmt.Errorf("Error reading grant role: %w", err)
}

d.Set("role", roleName)
d.Set("grant_role", grantRoleName)
d.Set("with_admin_option", withAdminOption)

d.SetId(generateGrantRoleID(d))
}

d.Set("role", roleName)
d.Set("grant_role", grantRoleName)
d.Set("with_admin_option", withAdminOption)

d.SetId(generateGrantRoleID(d))

return nil
}

Expand Down

0 comments on commit a968f52

Please sign in to comment.