Skip to content

Commit

Permalink
Handle --output-dir query filename collisions
Browse files Browse the repository at this point in the history
Occurs when the base names are the same like with:

- alpha/query.sql
- bravo/query.sql

As a first attempt to dedupe, we'll add the parent directory as a suffix, like
"alpha_query.sql.go". That won't work for cases like:

- alpha/query.sql
- parent/alpha/query.sql

In those cases, append an incrementing numeric literal:

- alpha_query.sql.0.go
- alpha_query.sql.1.go

Fixes #28.
  • Loading branch information
jschaf committed Apr 19, 2021
1 parent 71a9d1d commit 54c023e
Show file tree
Hide file tree
Showing 13 changed files with 398 additions and 8 deletions.
10 changes: 10 additions & 0 deletions example/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ func TestExamples(t *testing.T) {
"--query-glob", "example/pgcrypto/query.sql",
},
},
{
name: "example/separate_out_dir",
args: []string{
"--schema-glob", "example/separate_out_dir/schema.sql",
"--query-glob", "example/separate_out_dir/alpha/query.sql",
"--query-glob", "example/separate_out_dir/alpha/alpha/query.sql",
"--query-glob", "example/separate_out_dir/bravo/query.sql",
"--output-dir", "example/separate_out_dir/out",
},
},
{
name: "example/void",
args: []string{
Expand Down
4 changes: 4 additions & 0 deletions example/separate_out_dir/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Example using a separate output dir

This example shows how to use the `--output-dir` flag to control where pggen
writes query output files.
2 changes: 2 additions & 0 deletions example/separate_out_dir/alpha/alpha/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: AlphaNested :one
SELECT 'alpha_nested' as output;
2 changes: 2 additions & 0 deletions example/separate_out_dir/alpha/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: Alpha :one
SELECT 'alpha' as output;
2 changes: 2 additions & 0 deletions example/separate_out_dir/bravo/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: Bravo :one
SELECT 'bravo' as output;
129 changes: 129 additions & 0 deletions example/separate_out_dir/out/alpha_query.sql.0.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions example/separate_out_dir/out/alpha_query.sql.1.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions example/separate_out_dir/out/bravo_query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions example/separate_out_dir/out/codegen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package out

import (
"github.com/jschaf/pggen"
"github.com/jschaf/pggen/internal/pgtest"
"github.com/stretchr/testify/assert"
"io/ioutil"
"path/filepath"
"testing"
)

func TestGenerate_Go_Example_SeparateOutDir(t *testing.T) {
conn, cleanupFunc := pgtest.NewPostgresSchema(t, []string{
"../schema.sql",
})
defer cleanupFunc()

tmpDir := t.TempDir()
err := pggen.Generate(
pggen.GenerateOptions{
ConnString: conn.Config().ConnString(),
QueryFiles: []string{
"../alpha/query.sql",
"../alpha/alpha/query.sql",
"../bravo/query.sql",
},
OutputDir: tmpDir,
GoPackage: "out",
Language: pggen.LangGo,
})
if err != nil {
t.Fatalf("Generate(): %s", err)
}

for _, file := range []string{
"alpha_query.sql.0.go",
"alpha_query.sql.1.go",
"bravo_query.sql.go",
} {
wantQueries, err := ioutil.ReadFile(file)
if err != nil {
t.Fatalf("read wanted file %s: %s", file, err)
}

gotFile := filepath.Join(tmpDir, file)
assert.FileExists(t, gotFile, "Generate() should emit "+file)
gotQueries, err := ioutil.ReadFile(gotFile)
if err != nil {
t.Fatalf("read generated %s: %s", file, err)
}
assert.Equalf(t, string(wantQueries), string(gotQueries),
"Got file %s; does not match contents of file %s",
gotFile, file)
}
}
35 changes: 35 additions & 0 deletions example/separate_out_dir/out/query.sql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package out

import (
"context"
"github.com/stretchr/testify/require"
"testing"

"github.com/jschaf/pggen/internal/pgtest"
"github.com/stretchr/testify/assert"
)

func TestNewQuerier_FindAuthorByID(t *testing.T) {
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"../schema.sql"})
defer cleanup()

q := NewQuerier(conn)

t.Run("AlphaNested", func(t *testing.T) {
got, err := q.AlphaNested(context.Background())
require.NoError(t, err)
assert.Equal(t, "alpha_nested", got)
})

t.Run("Alpha", func(t *testing.T) {
got, err := q.Alpha(context.Background())
require.NoError(t, err)
assert.Equal(t, "alpha", got)
})

t.Run("Bravo", func(t *testing.T) {
got, err := q.Bravo(context.Background())
require.NoError(t, err)
assert.Equal(t, "bravo", got)
})
}
7 changes: 7 additions & 0 deletions example/separate_out_dir/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE alpha (
key text NOT NULL PRIMARY KEY
);

CREATE TABLE bravo (
key text NOT NULL PRIMARY KEY
);
Loading

0 comments on commit 54c023e

Please sign in to comment.