Skip to content

cmd/sqlc: Add option to disable process-based plugins #2180

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

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
Expand All @@ -19,6 +20,7 @@ import (
"github.com/kyleconroy/sqlc/internal/config"
"github.com/kyleconroy/sqlc/internal/debug"
"github.com/kyleconroy/sqlc/internal/info"
"github.com/kyleconroy/sqlc/internal/opts"
"github.com/kyleconroy/sqlc/internal/tracer"
)

Expand Down Expand Up @@ -106,16 +108,25 @@ var initCmd = &cobra.Command{

type Env struct {
DryRun bool
Debug opts.Debug
}

func ParseEnv(c *cobra.Command) Env {
dr := c.Flag("dry-run")
return Env{
DryRun: dr != nil && dr.Changed,
Debug: opts.DebugFromEnv(),
}
}

var ErrPluginProcessDisabled = errors.New("plugin: process-based plugins disabled via SQLCDEBUG=processplugins=0")

func (e *Env) Validate(cfg *config.Config) error {
for _, plugin := range cfg.Plugins {
if plugin.Process != nil && !e.Debug.ProcessPlugins {
return ErrPluginProcessDisabled
}
}
return nil
}

Expand Down
13 changes: 9 additions & 4 deletions internal/endtoend/endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"

"github.com/kyleconroy/sqlc/internal/cmd"
"github.com/kyleconroy/sqlc/internal/opts"
)

func TestExamples(t *testing.T) {
Expand Down Expand Up @@ -110,11 +111,14 @@ func TestReplay(t *testing.T) {
}
}

env := cmd.Env{
Debug: opts.DebugFromString(args.Env["SQLCDEBUG"]),
}
switch args.Command {
case "diff":
err = cmd.Diff(ctx, cmd.Env{}, path, "", &stderr)
err = cmd.Diff(ctx, env, path, "", &stderr)
case "generate":
output, err = cmd.Generate(ctx, cmd.Env{}, path, "", &stderr)
output, err = cmd.Generate(ctx, env, path, "", &stderr)
if err == nil {
cmpDirectory(t, path, output)
}
Expand Down Expand Up @@ -209,8 +213,9 @@ func expectedStderr(t *testing.T, dir string) string {
}

type exec struct {
Command string `json:"command"`
Process string `json:"process"`
Command string `json:"command"`
Process string `json:"process"`
Env map[string]string `json:"env"`
}

func parseExec(t *testing.T, dir string) exec {
Expand Down
6 changes: 6 additions & 0 deletions internal/endtoend/testdata/process_plugin_disabled/exec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"process": "sqlc-gen-json",
"env": {
"SQLCDEBUG": "processplugins=0"
}
}
19 changes: 19 additions & 0 deletions internal/endtoend/testdata/process_plugin_disabled/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1 LIMIT 1;

-- name: ListAuthors :many
SELECT * FROM authors
ORDER BY name;

-- name: CreateAuthor :one
INSERT INTO authors (
name, bio
) VALUES (
$1, $2
)
RETURNING *;

-- name: DeleteAuthor :exec
DELETE FROM authors
WHERE id = $1;
5 changes: 5 additions & 0 deletions internal/endtoend/testdata/process_plugin_disabled/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text
);
28 changes: 28 additions & 0 deletions internal/endtoend/testdata/process_plugin_disabled/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"version": "2",
"sql": [
{
"schema": "schema.sql",
"queries": "query.sql",
"engine": "postgresql",
"codegen": [
{
"out": "gen",
"plugin": "jsonb",
"options": {
"indent": " ",
"filename": "codegen.json"
}
}
]
}
],
"plugins": [
{
"name": "jsonb",
"process": {
"cmd": "sqlc-gen-json"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error validating sqlc.json: plugin: process-based plugins disabled via SQLCDEBUG=processplugins=0
18 changes: 13 additions & 5 deletions internal/opts/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ import (
// trace: setting trace=<path> will output a trace

type Debug struct {
DumpAST bool
DumpCatalog bool
Trace string
DumpAST bool
DumpCatalog bool
Trace string
ProcessPlugins bool
}

func DebugFromEnv() Debug {
d := Debug{}
val := os.Getenv("SQLCDEBUG")
return DebugFromString(os.Getenv("SQLCDEBUG"))
}

func DebugFromString(val string) Debug {
d := Debug{
ProcessPlugins: true,
}
if val == "" {
return d
}
Expand All @@ -38,6 +44,8 @@ func DebugFromEnv() Debug {
} else {
d.Trace = traceName
}
case pair == "processplugins=0":
d.ProcessPlugins = false
}
}
return d
Expand Down