Skip to content

Commit

Permalink
Merge pull request #66 from lukasbudisky/feature/1
Browse files Browse the repository at this point in the history
Before alter group make sure user still exists
  • Loading branch information
winglot authored Apr 22, 2022
2 parents 71656ee + 2cf1bc5 commit 05dc1bf
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions redshift/resource_redshift_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,21 @@ func setGroupName(tx *sql.Tx, d *schema.ResourceData) error {
return nil
}

func checkIfUserExists(tx *sql.Tx, name string) (bool, error) {

var result int
err := tx.QueryRow("SELECT 1 from pg_user_info WHERE usename=$1", name).Scan(&result)

switch err {
case sql.ErrNoRows:
return false, nil
case nil:
return false, fmt.Errorf("error reading info about user: %s", err)
}

return true, nil
}

func setUsersNames(tx *sql.Tx, db *DBConnection, d *schema.ResourceData) error {
if !d.HasChange(groupUsersAttr) {
return nil
Expand All @@ -226,13 +241,22 @@ func setUsersNames(tx *sql.Tx, db *DBConnection, d *schema.ResourceData) error {
if removedUsers.Len() > 0 {
removedUsersNamesSafe := []string{}
for _, name := range removedUsers.List() {
removedUsersNamesSafe = append(removedUsersNamesSafe, pq.QuoteIdentifier(name.(string)))
userExists, err := checkIfUserExists(tx, name.(string))
if err != nil {
return err
}

if userExists {
removedUsersNamesSafe = append(removedUsersNamesSafe, pq.QuoteIdentifier(name.(string)))
}
}

sql := fmt.Sprintf("ALTER GROUP %s DROP USER %s", pq.QuoteIdentifier(groupName), strings.Join(removedUsersNamesSafe, ", "))
if len(removedUsersNamesSafe) > 0 {
sql := fmt.Sprintf("ALTER GROUP %s DROP USER %s", pq.QuoteIdentifier(groupName), strings.Join(removedUsersNamesSafe, ", "))

if _, err := tx.Exec(sql); err != nil {
return err
if _, err := tx.Exec(sql); err != nil {
return err
}
}
}

Expand Down

0 comments on commit 05dc1bf

Please sign in to comment.