Skip to content

Commit 9ee704f

Browse files
committed
refactor: Use context on all SQL calls
1 parent d4cf035 commit 9ee704f

File tree

10 files changed

+259
-234
lines changed

10 files changed

+259
-234
lines changed

internal/controller/postgresql/postgres/aws.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package postgres
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/lib/pq"
@@ -21,32 +22,32 @@ func newAWSPG(postgres *pg) PG {
2122
}
2223
}
2324

24-
func (c *awspg) AlterDefaultLoginRole(role, setRole string) error {
25+
func (c *awspg) AlterDefaultLoginRole(ctx context.Context, role, setRole string) error {
2526
// On AWS RDS the postgres user isn't really superuser so he doesn't have permissions
2627
// to ALTER USER unless he belongs to both roles
27-
err := c.GrantRole(role, c.user, false)
28+
err := c.GrantRole(ctx, role, c.user, false)
2829
if err != nil {
2930
return err
3031
}
3132

3233
defer func() {
33-
err := c.RevokeRole(role, c.user)
34+
err := c.RevokeRole(ctx, role, c.user)
3435
// Check error
3536
if err != nil {
3637
c.log.Error(err, "error in revoke role")
3738
}
3839
}()
3940

40-
return c.pg.AlterDefaultLoginRole(role, setRole)
41+
return c.pg.AlterDefaultLoginRole(ctx, role, setRole)
4142
}
4243

43-
func (c *awspg) CreateDB(dbname, role string) error {
44+
func (c *awspg) CreateDB(ctx context.Context, dbname, role string) error {
4445
err := c.connect(c.defaultDatabase)
4546
if err != nil {
4647
return err
4748
}
4849

49-
_, err = c.db.Exec(fmt.Sprintf(CreateDBWithoutOwnerSQLTemplate, dbname))
50+
_, err = c.db.ExecContext(ctx, fmt.Sprintf(CreateDBWithoutOwnerSQLTemplate, dbname))
5051
if err != nil {
5152
// eat DUPLICATE DATABASE ERROR
5253
// Try to cast error
@@ -56,18 +57,18 @@ func (c *awspg) CreateDB(dbname, role string) error {
5657
}
5758
}
5859

59-
_, err = c.db.Exec(fmt.Sprintf(AlterDBOwnerSQLTemplate, dbname, role))
60+
_, err = c.db.ExecContext(ctx, fmt.Sprintf(AlterDBOwnerSQLTemplate, dbname, role))
6061
if err != nil {
6162
return err
6263
}
6364

6465
return nil
6566
}
6667

67-
func (c *awspg) DropRoleAndDropAndChangeOwnedBy(role, newOwner, database string) error {
68+
func (c *awspg) DropRoleAndDropAndChangeOwnedBy(ctx context.Context, role, newOwner, database string) error {
6869
// On AWS RDS the postgres user isn't really superuser so he doesn't have permissions
6970
// to REASSIGN OWNED BY unless he belongs to both roles
70-
err := c.GrantRole(role, c.user, false)
71+
err := c.GrantRole(ctx, role, c.user, false)
7172
// Check error
7273
if err != nil {
7374
// Try to cast error
@@ -85,7 +86,7 @@ func (c *awspg) DropRoleAndDropAndChangeOwnedBy(role, newOwner, database string)
8586
}
8687
}
8788

88-
err = c.GrantRole(newOwner, c.user, false)
89+
err = c.GrantRole(ctx, newOwner, c.user, false)
8990
// Check error
9091
if err != nil {
9192
// Try to cast error
@@ -107,19 +108,19 @@ func (c *awspg) DropRoleAndDropAndChangeOwnedBy(role, newOwner, database string)
107108
}
108109

109110
defer func() {
110-
err := c.RevokeRole(newOwner, c.user)
111+
err := c.RevokeRole(ctx, newOwner, c.user)
111112
if err != nil {
112113
c.log.Error(err, "error in revoke role")
113114
}
114115
}()
115116

116-
return c.pg.DropRoleAndDropAndChangeOwnedBy(role, newOwner, database)
117+
return c.pg.DropRoleAndDropAndChangeOwnedBy(ctx, role, newOwner, database)
117118
}
118119

119-
func (c *awspg) ChangeAndDropOwnedBy(role, newOwner, database string) error {
120+
func (c *awspg) ChangeAndDropOwnedBy(ctx context.Context, role, newOwner, database string) error {
120121
// On AWS RDS the postgres user isn't really superuser so he doesn't have permissions
121122
// to REASSIGN OWNED BY unless he belongs to both roles
122-
err := c.GrantRole(role, c.user, false)
123+
err := c.GrantRole(ctx, role, c.user, false)
123124
// Check error
124125
if err != nil {
125126
// Try to cast error
@@ -137,7 +138,7 @@ func (c *awspg) ChangeAndDropOwnedBy(role, newOwner, database string) error {
137138
}
138139
}
139140

140-
err = c.GrantRole(newOwner, c.user, false)
141+
err = c.GrantRole(ctx, newOwner, c.user, false)
141142
// Check error
142143
if err != nil {
143144
// Try to cast error
@@ -159,11 +160,11 @@ func (c *awspg) ChangeAndDropOwnedBy(role, newOwner, database string) error {
159160
}
160161

161162
defer func() {
162-
err := c.RevokeRole(newOwner, c.user)
163+
err := c.RevokeRole(ctx, newOwner, c.user)
163164
if err != nil {
164165
c.log.Error(err, "error in revoke role")
165166
}
166167
}()
167168

168-
return c.pg.ChangeAndDropOwnedBy(role, newOwner, database)
169+
return c.pg.ChangeAndDropOwnedBy(ctx, role, newOwner, database)
169170
}

internal/controller/postgresql/postgres/azure.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package postgres
22

33
import (
4+
"context"
45
"fmt"
56
"strings"
67
)
@@ -26,16 +27,16 @@ func newAzurePG(postgres *pg) PG {
2627
}
2728
}
2829

29-
func (azpg *azurepg) CreateUserRole(role, password string, attributes *RoleAttributes) (string, error) {
30-
returnedRole, err := azpg.pg.CreateUserRole(role, password, attributes)
30+
func (azpg *azurepg) CreateUserRole(ctx context.Context, role, password string, attributes *RoleAttributes) (string, error) {
31+
returnedRole, err := azpg.pg.CreateUserRole(ctx, role, password, attributes)
3132
if err != nil {
3233
return "", err
3334
}
3435

3536
return fmt.Sprintf("%s@%s", returnedRole, azpg.serverName), nil
3637
}
3738

38-
func (azpg *azurepg) GetRoleForLogin(login string) string {
39+
func (azpg *azurepg) GetRoleForLogin(ctx context.Context, login string) string {
3940
splitUser := strings.Split(azpg.user, "@")
4041
if len(splitUser) > MinUserSplit {
4142
return splitUser[0]
@@ -44,12 +45,12 @@ func (azpg *azurepg) GetRoleForLogin(login string) string {
4445
return login
4546
}
4647

47-
func (azpg *azurepg) CreateDB(dbname, role string) error {
48+
func (azpg *azurepg) CreateDB(ctx context.Context, dbname, role string) error {
4849
// Have to add the master role to the group role before we can transfer the database owner
49-
err := azpg.GrantRole(role, azpg.GetRoleForLogin(azpg.user), false)
50+
err := azpg.GrantRole(ctx, role, azpg.GetRoleForLogin(ctx, azpg.user), false)
5051
if err != nil {
5152
return err
5253
}
5354

54-
return azpg.pg.CreateDB(dbname, role)
55+
return azpg.pg.CreateDB(ctx, dbname, role)
5556
}

0 commit comments

Comments
 (0)