Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(debug): add debug flag and docs for dumping vet rule variables #2521

Merged
merged 2 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ return an error.

`SQLCDEBUG=processplugins=0`

### dumpvetenv

The `dumpvetenv` command prints the variables available to a `sqlc vet` rule
during evaluation.

`SQLCDEBUG=dumpvetenv=1`

### dumpexplain

The `dumpexplain` command prints the JSON-formatted result from running
Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,10 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error {
evalMap["mysql"] = engineOutput.MySQL
}

if debug.Debug.DumpVetEnv {
fmt.Printf("vars for rule '%s' evaluating against query '%s':\n%v\n", name, query.Name, evalMap)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we dump this as JSON?

}

out, _, err := (*rule.Program).Eval(evalMap)
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions internal/opts/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
// dumpcatalog: setting dumpcatalog=1 will print the parsed database schema
// trace: setting trace=<path> will output a trace
// processplugins: setting processplugins=0 will disable process-based plugins
// dumpvetenv: setting dumpvetenv=1 will print the variables available to
// a vet rule during evaluation
// dumpexplain: setting dumpexplain=1 will print the JSON-formatted output
// from executing EXPLAIN ... on a query during vet rule evaluation

Expand All @@ -20,6 +22,7 @@ type Debug struct {
DumpCatalog bool
Trace string
ProcessPlugins bool
DumpVetEnv bool
DumpExplain bool
}

Expand Down Expand Up @@ -50,6 +53,8 @@ func DebugFromString(val string) Debug {
}
case pair == "processplugins=0":
d.ProcessPlugins = false
case pair == "dumpvetenv=1":
d.DumpVetEnv = true
case pair == "dumpexplain=1":
d.DumpExplain = true
}
Expand Down
Loading