-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle --output-dir query filename collisions
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
Showing
13 changed files
with
398 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- name: AlphaNested :one | ||
SELECT 'alpha_nested' as output; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- name: Alpha :one | ||
SELECT 'alpha' as output; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- name: Bravo :one | ||
SELECT 'bravo' as output; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
Oops, something went wrong.