Skip to content

Commit

Permalink
fix: Upgrade linter and patch all code
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyno-zeta committed Jan 25, 2024
1 parent c841f0d commit 9dc3205
Show file tree
Hide file tree
Showing 15 changed files with 633 additions and 130 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
- uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.51.1
version: v1.55.2

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
514 changes: 514 additions & 0 deletions .golangci.yaml

Large diffs are not rendered by default.

61 changes: 0 additions & 61 deletions .golangci.yml

This file was deleted.

4 changes: 4 additions & 0 deletions controllers/postgresql/postgres/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ func (c *awspg) DropRoleAndDropAndChangeOwnedBy(role, newOwner, database string)
if !ok {
return err
}

if pqErr.Code == RoleNotFoundErrorCode {
return nil
}

if pqErr.Code != InvalidGrantOperationErrorCode {
return err
}
Expand Down Expand Up @@ -125,9 +127,11 @@ func (c *awspg) ChangeAndDropOwnedBy(role, newOwner, database string) error {
if !ok {
return err
}

if pqErr.Code == RoleNotFoundErrorCode {
return nil
}

if pqErr.Code != InvalidGrantOperationErrorCode {
return err
}
Expand Down
1 change: 1 addition & 0 deletions controllers/postgresql/postgres/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const MinUserSplit = 1
func newAzurePG(postgres *pg) PG {
splitUser := strings.Split(postgres.user, "@")
serverName := ""

if len(splitUser) > MinUserSplit {
serverName = splitUser[1]
}
Expand Down
2 changes: 2 additions & 0 deletions controllers/postgresql/postgres/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func (c *pg) DropExtension(database, extension string, cascade bool) error {
if cascade {
param = CascadeKeyword
}

_, err = c.db.Exec(fmt.Sprintf(DropExtensionSQLTemplate, extension, param))
if err != nil {
return err
Expand All @@ -140,6 +141,7 @@ func (c *pg) DropSchema(database, schema string, cascade bool) error {
if cascade {
param = CascadeKeyword
}

_, err = c.db.Exec(fmt.Sprintf(DropSchemaSQLTemplate, schema, param))
if err != nil {
return err
Expand Down
16 changes: 8 additions & 8 deletions controllers/postgresql/postgres/pool_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const (

// Pool saved structure per postgres engine configuration.
type poolSaved struct {
// This map will save all pools per database
pools *sync.Map
// Username and password are saved because this comes from secret
username string
password string
// This map will save all pools per database
pools *sync.Map
}

// Pool manager map per pgec.
Expand Down Expand Up @@ -53,7 +53,7 @@ func getOrOpenPool(p *pg, database string) (*sql.DB, error) {
db = sqlDB
} else {
// Cast saved pool object
sav := savInt.(*poolSaved)
sav, _ := savInt.(*poolSaved)
// Check if username and password haven't changed, if yes, close pools and recreate current
if sav.username != p.GetUser() || sav.password != p.GetPassword() {
// Close all pools
Expand Down Expand Up @@ -81,7 +81,7 @@ func getOrOpenPool(p *pg, database string) (*sql.DB, error) {
db = sqlDB
} else {
// Result
db = sqlDBInt.(*sql.DB)
db, _ = sqlDBInt.(*sql.DB)
}
}

Expand Down Expand Up @@ -125,7 +125,7 @@ func CloseDatabaseSavedPoolsForName(name, database string) error {
}

// Cast pool saved
ps := psInt.(*poolSaved)
ps, _ := psInt.(*poolSaved)

// Get entry
enInt, ok := ps.pools.Load(database)
Expand All @@ -135,7 +135,7 @@ func CloseDatabaseSavedPoolsForName(name, database string) error {
}

// Cast db
en := enInt.(*sql.DB)
en, _ := enInt.(*sql.DB)

// Close pool
err := en.Close()
Expand All @@ -159,7 +159,7 @@ func CloseAllSavedPoolsForName(name string) error {
}

// Cast pool saved
ps := psInt.(*poolSaved)
ps, _ := psInt.(*poolSaved)

// Save all keys to be removed
keysToBeRemoved := make([]interface{}, 0)
Expand All @@ -168,7 +168,7 @@ func CloseAllSavedPoolsForName(name string) error {
// Loop over pools
ps.pools.Range(func(k, val interface{}) bool {
// Cast sql db
v := val.(*sql.DB)
v, _ := val.(*sql.DB)
// Close pool
err = v.Close()
// Check error
Expand Down
7 changes: 4 additions & 3 deletions controllers/postgresql/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ type pg struct {
db *sql.DB
log logr.Logger
host string
port int
user string
pass string
args string
defaultDatabase string
name string
port int
}

func NewPG(
Expand Down Expand Up @@ -139,6 +139,7 @@ func (c *pg) Ping() error {
if err != nil {
return err
}

err = c.db.Ping()
if err != nil {
return err
Expand All @@ -147,8 +148,8 @@ func (c *pg) Ping() error {
return nil
}

func TemplatePostgresqlURLWithArgs(host, user, password, URIArgs, database string, port int) string {
return fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?%s", user, password, host, port, database, URIArgs)
func TemplatePostgresqlURLWithArgs(host, user, password, uriArgs, database string, port int) string {
return fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?%s", user, password, host, port, database, uriArgs)
}

func TemplatePostgresqlURL(host, user, password, database string, port int) string {
Expand Down
3 changes: 2 additions & 1 deletion controllers/postgresql/postgres/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
GetRoleMembershipSQLTemplate = `SELECT r1.rolname as "role" FROM pg_catalog.pg_roles r JOIN pg_catalog.pg_auth_members m ON (m.member = r.oid) JOIN pg_roles r1 ON (m.roleid=r1.oid) WHERE r.rolcanlogin AND r.rolname='%s'`
// DO NOT TOUCH THIS
// Cannot filter on compute value so... cf line before.
GetRoleSettingsSQLTemplate = `SELECT pg_catalog.split_part(pg_catalog.unnest(setconfig), '=', 1) as parameter_type, pg_catalog.split_part(pg_catalog.unnest(setconfig), '=', 2) as parameter_value, d.datname as database FROM pg_catalog.pg_roles r JOIN pg_catalog.pg_db_role_setting c ON (c.setrole = r.oid) JOIN pg_catalog.pg_database d ON (d.oid = c.setdatabase) WHERE r.rolcanlogin AND r.rolname='%s'`
GetRoleSettingsSQLTemplate = `SELECT pg_catalog.split_part(pg_catalog.unnest(setconfig), '=', 1) as parameter_type, pg_catalog.split_part(pg_catalog.unnest(setconfig), '=', 2) as parameter_value, d.datname as database FROM pg_catalog.pg_roles r JOIN pg_catalog.pg_db_role_setting c ON (c.setrole = r.oid) JOIN pg_catalog.pg_database d ON (d.oid = c.setdatabase) WHERE r.rolcanlogin AND r.rolname='%s'` //nolint:lll//Because
DoesRoleHaveActiveSessionSQLTemplate = `SELECT 1 from pg_stat_activity WHERE usename = '%s' group by usename`
DuplicateRoleErrorCode = "42710"
RoleNotFoundErrorCode = "42704"
Expand Down Expand Up @@ -266,6 +266,7 @@ func (c *pg) DropRole(role string) error {
if err != nil {
return err
}

_, err = c.db.Exec(fmt.Sprintf(DropRoleSQLTemplate, role))
// Check if error exists and if different from "ROLE NOT FOUND" => 42704
if err != nil {
Expand Down
Loading

0 comments on commit 9dc3205

Please sign in to comment.