8000 feat(codegen): Allow plugins to access environment variables by kyleconroy · Pull Request #2669 · sqlc-dev/sqlc · GitHub
[go: up one dir, main page]

Skip to content

feat(codegen): Allow plugins to access environment variables #2669

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 7 commits into from
Aug 29, 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
Prev Previous commit
Next Next commit
Add tests for process-based plugins
  • Loading branch information
kyleconroy committed Aug 29, 2023
commit c1ce837b92b1e712b8c7c67b89358394ae97dfb7
45 changes: 45 additions & 0 deletions cmd/sqlc-gen-env/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"bufio"
"encoding/json"
"fmt"
"os"

"github.com/sqlc-dev/sqlc/internal/plugin"
)

func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error generating env: %s", err)
os.Exit(2)
}
}

func run() error {
env := os.Environ()
blob, err := json.Marshal(env)
if err != nil {
return err
}
resp := &plugin.CodeGenResponse{
Files: []*plugin.File{
{
Name: "env.json",
Contents: append(blob, '\n'),
},
},
}
respBlob, err := resp.MarshalVT()
if err != nil {
return err
}
w := bufio.NewWriter(os.Stdout)
if _, err := w.Write(respBlob); err != nil {
return err
}
if err := w.Flush(); err != nil {
return err
}
return nil
}
4 changes: 4 additions & 0 deletions internal/endtoend/endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func BenchmarkExamples(b *testing.B) {
}

func TestReplay(t *testing.T) {
// Ensure that this environment variable is always set to true when running
// end-to-end tests
os.Setenv("SQLC_DUMMY_VALUE", "true")

t.Parallel()
ctx := context.Background()
var dirs []string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"process": "sqlc-gen-env"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["SQLC_VERSION=v1.20.0","SQLC_DUMMY_VALUE=true"]
19 changes: 19 additions & 0 deletions internal/endtoend/testdata/process_plugin_sqlc_gen_env/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;
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
);
25 changes: 25 additions & 0 deletions internal/endtoend/testdata/process_plugin_sqlc_gen_env/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"version": "2",
"sql": [
{
"schema": "schema.sql",
"queries": "query.sql",
"engine": "postgresql",
"codegen": [
{
"out": "gen",
"plugin": "env"
}
]
}
],
"plugins": [
{
"name": "env",
"env": ["SQLC_DUMMY_VALUE"],
"process": {
"cmd": "sqlc-gen-env"
}
}
]
}
1 change: 1 addition & 0 deletions scripts/regenerate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func regenerate(dir string) error {
}

cmd := exec.Command("sqlc-dev", "generate")
cmd.Env = append(cmd.Env, "SQLC_DUMMY_VALUE=true")
cmd.Dir = cwd
out, failed := cmd.CombinedOutput()
if failed != nil && !expectFailure {
3F8A Expand Down
0