Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: use "store.db" build flag to separate the db query logging (#2444)
tianzhouf95360c13682
diff --git a/scripts/.air.toml b/scripts/.air.tomlindex 88dd0f9b4f..4610f9414f 100644--- a/scripts/.air.toml+++ b/scripts/.air.toml@@ -2,9 +2,12 @@ root = "."tmp_dir = ".air"[build]- bin = "./.air/bytebase --port 8080 --frontend-port 3000 --data ."+ bin = "./.air/bytebase --port 8080 --frontend-port 3000 --data . --debug"+ ## Use --tags "store.db" to enable SQL query logging against our metadata db.cmd = """- go build -gcflags=all=\"-N -l\" \+ go build \+ --tags "" \+ -gcflags=all=\"-N -l\" \-ldflags " \-X 'github.com/bytebase/bytebase/bin/server/cmd.version=development' \-X 'github.com/bytebase/bytebase/bin/server/cmd.goversion=$(go version)' \diff --git a/store/pg_engine.go b/store/pg_engine.goindex d6a0620f50..d6dca5a078 100644--- a/store/pg_engine.go+++ b/store/pg_engine.go@@ -7,7 +7,6 @@ import ("fmt""io/fs""path/filepath"- "regexp""sort""strconv""strings"@@ -15,7 +14,6 @@ import ("github.com/blang/semver/v4""github.com/pkg/errors"- "go.uber.org/zap""github.com/bytebase/bytebase/common/log"dbdriver "github.com/bytebase/bytebase/plugin/db"@@ -662,37 +660,6 @@ type Tx struct {now time.Time}-// Replace mutiple whitespace characters including /t/n with a single space.-var pattern = regexp.MustCompile(`\s+`)--func cleanQuery(query string) string {- return pattern.ReplaceAllString(query, " ")-}--// PrepareContext overrides sql.Tx PrepareContext.-func (tx *Tx) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {- log.Debug("PrepareContext", zap.String("query", cleanQuery(query)))- return tx.Tx.PrepareContext(ctx, query)-}--// ExecContext overrides sql.Tx ExecContext.-func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {- log.Debug("ExecContext", zap.String("query", cleanQuery(query)))- return tx.Tx.ExecContext(ctx, query, args...)-}--// QueryContext overrides sql.Tx QueryContext.-func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {- log.Debug("QueryContext", zap.String("query", cleanQuery(query)))- return tx.Tx.QueryContext(ctx, query, args...)-}--// QueryRowContext overrides sql.Tx QueryRowContext.-func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {- log.Debug("QueryRowContext", zap.String("query", cleanQuery(query)))- return tx.Tx.QueryRowContext(ctx, query, args...)-}-// FormatError returns err as a Bytebase error, if possible.// Otherwise returns the original error.func FormatError(err error) error {diff --git a/store/pg_engine_log.go b/store/pg_engine_log.gonew file mode 100644index 0000000000..e6a3256731--- /dev/null+++ b/store/pg_engine_log.go@@ -0,0 +1,46 @@+//go:build store.db+// +build store.db++// Log SQL query hitting our metadata db. Useful to spot unnecessary SQLs.+package store++import (+ "context"+ "database/sql"+ "regexp"++ "go.uber.org/zap"++ "github.com/bytebase/bytebase/common/log"+)++// Replace mutiple whitespace characters including /t/n with a single space.+var pattern = regexp.MustCompile(`\s+`)++func cleanQuery(query string) string {+ return pattern.ReplaceAllString(query, " ")+}++// PrepareContext overrides sql.Tx PrepareContext.+func (tx *Tx) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {+ log.Debug("PrepareContext", zap.String("query", cleanQuery(query)))+ return tx.Tx.PrepareContext(ctx, query)+}++// ExecContext overrides sql.Tx ExecContext.+func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {+ log.Debug("ExecContext", zap.String("query", cleanQuery(query)))+ return tx.Tx.ExecContext(ctx, query, args...)+}++// QueryContext overrides sql.Tx QueryContext.+func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {+ log.Debug("QueryContext", zap.String("query", cleanQuery(query)))+ return tx.Tx.QueryContext(ctx, query, args...)+}++// QueryRowContext overrides sql.Tx QueryRowContext.+func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {+ log.Debug("QueryRowContext", zap.String("query", cleanQuery(query)))+ return tx.Tx.QueryRowContext(ctx, query, args...)+}