Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: enable linter defer (#2227)
Yilong Li406868a4aac1
diff --git a/.golangci.yaml b/.golangci.yamlindex e0c5c2953d..093c0d17f3 100644--- a/.golangci.yaml+++ b/.golangci.yaml@@ -68,9 +68,6 @@ linters-settings:disabled: true- name: early-returndisabled: true- # TODO(dragonly): Remove the rules below in the following PRs.- - name: defer- disabled: truegocritic:disabled-checks:- ifElseChaindiff --git a/bin/bb/cmd/migrate.go b/bin/bb/cmd/migrate.goindex 99dac73289..99f662a879 100644--- a/bin/bb/cmd/migrate.go+++ b/bin/bb/cmd/migrate.go@@ -40,6 +40,8 @@ func newMigrateCmd() *cobra.Command {if err != nil {return err}+ // nolint:revive+ // f.Close() is intended to be deferred to the end of the function.defer f.Close()sqlReaders = append(sqlReaders, f)}diff --git a/bin/bb/cmd/root_test.go b/bin/bb/cmd/root_test.goindex fc9c67c358..be2e8350d1 100644--- a/bin/bb/cmd/root_test.go+++ b/bin/bb/cmd/root_test.go@@ -57,15 +57,17 @@ got unexpected output:%s`, strings.Join(tc.args, " "), cmp.Diff(actual, tc.expected))- f, err := os.CreateTemp("", "*")- if err != nil {- t.Log(err)- }- defer f.Close()- if _, err := io.WriteString(f, actual); err != nil {- t.Log(err)- }- t.Logf("Actual output written to %s", f.Name())+ func() {+ f, err := os.CreateTemp("", "*")+ if err != nil {+ t.Log(err)+ }+ defer f.Close()+ if _, err := io.WriteString(f, actual); err != nil {+ t.Log(err)+ }+ t.Logf("Actual output written to %s", f.Name())+ }()}}}diff --git a/plugin/db/clickhouse/sync.go b/plugin/db/clickhouse/sync.goindex ba7f2ee719..dfef55bfcd 100644--- a/plugin/db/clickhouse/sync.go+++ b/plugin/db/clickhouse/sync.go@@ -230,34 +230,39 @@ func (driver *Driver) getUserList(ctx context.Context) ([]db.User, error) {return nil, err}- // Uses single quote instead of backtick to escape because this is a string- // instead of table (which should use backtick instead). MySQL actually works- // in both ways. On the other hand, some other MySQL compatible engines might not (OceanBase in this case).- grantQuery := fmt.Sprintf("SHOW GRANTS FOR %s", user)- grantRows, err := driver.db.QueryContext(ctx,- grantQuery,- )- if err != nil {- return nil, util.FormatErrorWithQuery(err, grantQuery)- }- defer grantRows.Close()+ if err := func() error {+ // Uses single quote instead of backtick to escape because this is a string+ // instead of table (which should use backtick instead). MySQL actually works+ // in both ways. On the other hand, some other MySQL compatible engines might not (OceanBase in this case).+ grantQuery := fmt.Sprintf("SHOW GRANTS FOR %s", user)+ grantRows, err := driver.db.QueryContext(ctx,+ grantQuery,+ )+ if err != nil {+ return util.FormatErrorWithQuery(err, grantQuery)+ }+ defer grantRows.Close()- grantList := []string{}- for grantRows.Next() {- var grant string- if err := grantRows.Scan(&grant); err != nil {- return nil, err+ grantList := []string{}+ for grantRows.Next() {+ var grant string+ if err := grantRows.Scan(&grant); err != nil {+ return err+ }+ grantList = append(grantList, grant)+ }+ if err := grantRows.Err(); err != nil {+ return util.FormatErrorWithQuery(err, grantQuery)}- grantList = append(grantList, grant)- }- if err := grantRows.Err(); err != nil {- return nil, util.FormatErrorWithQuery(err, grantQuery)- }- userList = append(userList, db.User{- Name: user,- Grant: strings.Join(grantList, "\n"),- })+ userList = append(userList, db.User{+ Name: user,+ Grant: strings.Join(grantList, "\n"),+ })+ return nil+ }(); err != nil {+ return nil, err+ }}if err := userRows.Err(); err != nil {return nil, util.FormatErrorWithQuery(err, userQuery)diff --git a/plugin/db/mysql/dump.go b/plugin/db/mysql/dump.goindex 85cb97af9b..907c0c066b 100644--- a/plugin/db/mysql/dump.go+++ b/plugin/db/mysql/dump.go@@ -482,32 +482,34 @@ func isNumeric(t string) bool {func getRoutines(txn *sql.Tx, dbName string) ([]*routineSchema, error) {var routines []*routineSchemafor _, routineType := range []string{"FUNCTION", "PROCEDURE"} {- query := fmt.Sprintf("SHOW %s STATUS WHERE Db = ?;", routineType)- rows, err := txn.Query(query, dbName)- if err != nil {- return nil, err- }- defer rows.Close()+ if err := func() error {+ query := fmt.Sprintf("SHOW %s STATUS WHERE Db = ?;", routineType)+ rows, err := txn.Query(query, dbName)+ if err != nil {+ return err+ }+ defer rows.Close()- cols, err := rows.Columns()- if err != nil {- return nil, err- }- var values []interface{}- for i := 0; i < len(cols); i++ {- values = append(values, new(interface{}))- }- for rows.Next() {- var r routineSchema- if err := rows.Scan(values...); err != nil {- return nil, err+ cols, err := rows.Columns()+ if err != nil {+ return err+ }+ var values []interface{}+ for i := 0; i < len(cols); i++ {+ values = append(values, new(interface{}))}- r.name = fmt.Sprintf("%s", *values[1].(*interface{}))- r.routineType = fmt.Sprintf("%s", *values[2].(*interface{}))+ for rows.Next() {+ var r routineSchema+ if err := rows.Scan(values...); err != nil {+ return err+ }+ r.name = fmt.Sprintf("%s", *values[1].(*interface{}))+ r.routineType = fmt.Sprintf("%s", *values[2].(*interface{}))- routines = append(routines, &r)- }- if err := rows.Err(); err != nil {+ routines = append(routines, &r)+ }+ return rows.Err()+ }(); err != nil {return nil, err}}diff --git a/plugin/db/mysql/sync.go b/plugin/db/mysql/sync.goindex 7d4efd95ad..fae18a5efe 100644--- a/plugin/db/mysql/sync.go+++ b/plugin/db/mysql/sync.go@@ -412,35 +412,40 @@ func (driver *Driver) getUserList(ctx context.Context) ([]db.User, error) {return nil, err}- // Uses single quote instead of backtick to escape because this is a string- // instead of table (which should use backtick instead). MySQL actually works- // in both ways. On the other hand, some other MySQL compatible engines might not (OceanBase in this case).- name := fmt.Sprintf("'%s'@'%s'", user, host)- grantQuery := fmt.Sprintf("SHOW GRANTS FOR %s", name)- grantRows, err := driver.db.QueryContext(ctx,- grantQuery,- )- if err != nil {- return nil, util.FormatErrorWithQuery(err, grantQuery)- }- defer grantRows.Close()-- grantList := []string{}- for grantRows.Next() {- var grant string- if err := grantRows.Scan(&grant); err != nil {- return nil, err+ if err := func() error {+ // Uses single quote instead of backtick to escape because this is a string+ // instead of table (which should use backtick instead). MySQL actually works+ // in both ways. On the other hand, some other MySQL compatible engines might not (OceanBase in this case).+ name := fmt.Sprintf("'%s'@'%s'", user, host)+ grantQuery := fmt.Sprintf("SHOW GRANTS FOR %s", name)+ grantRows, err := driver.db.QueryContext(ctx,+ grantQuery,+ )+ if err != nil {+ return util.FormatErrorWithQuery(err, grantQuery)+ }+ defer grantRows.Close()++ grantList := []string{}+ for grantRows.Next() {+ var grant string+ if err := grantRows.Scan(&grant); err != nil {+ return err+ }+ grantList = append(grantList, grant)+ }+ if err := grantRows.Err(); err != nil {+ return util.FormatErrorWithQuery(err, grantQuery)}- grantList = append(grantList, grant)- }- if err := grantRows.Err(); err != nil {- return nil, util.FormatErrorWithQuery(err, grantQuery)- }- userList = append(userList, db.User{- Name: name,- Grant: strings.Join(grantList, "\n"),- })+ userList = append(userList, db.User{+ Name: name,+ Grant: strings.Join(grantList, "\n"),+ })+ return nil+ }(); err != nil {+ return nil, err+ }}if err := userRows.Err(); err != nil {return nil, util.FormatErrorWithQuery(err, userQuery)diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.goindex 7708118027..7528100327 100644--- a/plugin/db/pg/pg.go+++ b/plugin/db/pg/pg.go@@ -139,13 +139,14 @@ func guessDSN(username, password, hostname, port, database, sslCA, sslCert, sslKif guess != "" {guessDSN = fmt.Sprintf("%s dbname=%s", dsn, guess)}- db, err := sql.Open(driverName, guessDSN)- if err != nil {- continue- }- defer db.Close()-- if err = db.Ping(); err != nil {+ if err := func() error {+ db, err := sql.Open(driverName, guessDSN)+ if err != nil {+ return err+ }+ defer db.Close()+ return db.Ping()+ }(); err != nil {continue}return guess, guessDSN, nildiff --git a/plugin/db/sqlite/sync.go b/plugin/db/sqlite/sync.goindex 6df3999d58..df7b5f7685 100644--- a/plugin/db/sqlite/sync.go+++ b/plugin/db/sqlite/sync.go@@ -144,56 +144,64 @@ func getTables(txn *sql.Tx, indicesMap map[string][]indexSchema) ([]db.Table, ertbl.Name = nametbl.Type = "BASE TABLE"- // Get columns: cid, name, type, notnull, dflt_value, pk.- query := fmt.Sprintf("pragma table_info(%s);", name)- rows, err := txn.Query(query)- if err != nil {- return nil, err- }- defer rows.Close()-- for rows.Next() {- var col db.Column-- var cid int- var notnull, pk bool- var name, ctype string- var dfltValue sql.NullString- if err := rows.Scan(&cid, &name, &ctype, ¬null, &dfltValue, &pk); err != nil {- return nil, err- }- col.Position = cid- col.Name = name- col.Nullable = !notnull- col.Type = ctype- if dfltValue.Valid {- col.Default = &dfltValue.String- }-- tbl.ColumnList = append(tbl.ColumnList, col)- }- if err := rows.Err(); err != nil {- return nil, err- }- for _, idx := range indicesMap[tbl.Name] {- query := fmt.Sprintf("pragma index_info(%s);", idx.name)+ if err := func() error {+ // Get columns: cid, name, type, notnull, dflt_value, pk.+ query := fmt.Sprintf("pragma table_info(%s);", name)rows, err := txn.Query(query)if err != nil {- return nil, err+ return err}defer rows.Close()+for rows.Next() {- var dbIdx db.Index- dbIdx.Name = idx.name- dbIdx.Unique = idx.unique- var cid string- if err := rows.Scan(&dbIdx.Position, &cid, &dbIdx.Expression); err != nil {- return nil, err+ var col db.Column++ var cid int+ var notnull, pk bool+ var name, ctype string+ var dfltValue sql.NullString+ if err := rows.Scan(&cid, &name, &ctype, ¬null, &dfltValue, &pk); err != nil {+ return err+ }+ col.Position = cid+ col.Name = name+ col.Nullable = !notnull+ col.Type = ctype+ if dfltValue.Valid {+ col.Default = &dfltValue.String}- tbl.IndexList = append(tbl.IndexList, dbIdx)++ tbl.ColumnList = append(tbl.ColumnList, col)}- if err := rows.Err(); err != nil {- return nil, util.FormatErrorWithQuery(err, query)+ return rows.Err()+ }(); err != nil {+ return nil, err+ }++ for _, idx := range indicesMap[tbl.Name] {+ if err := func() error {+ query := fmt.Sprintf("pragma index_info(%s);", idx.name)+ rows, err := txn.Query(query)+ if err != nil {+ return err+ }+ defer rows.Close()+ for rows.Next() {+ var dbIdx db.Index+ dbIdx.Name = idx.name+ dbIdx.Unique = idx.unique+ var cid string+ if err := rows.Scan(&dbIdx.Position, &cid, &dbIdx.Expression); err != nil {+ return err+ }+ tbl.IndexList = append(tbl.IndexList, dbIdx)+ }+ if err := rows.Err(); err != nil {+ return util.FormatErrorWithQuery(err, query)+ }+ return nil+ }(); err != nil {+ return nil, err}}