Skip to content

Commit 1b6ba1e

Browse files
authored
fix(pgx): copyfrom imports (#1626)
Depending on the commands used in your `query.sql` the `copyfrom.go` may end up with unused packge imports. There is a patch for this problem that was merged in the #1386 PR. However, it looks like some edge cases were left out of the fix. This commit ensures that `buildImports` only has `copyfrom` queries when called from `copyfromImports`.
1 parent d458fbe commit 1b6ba1e

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

internal/codegen/golang/imports.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,14 @@ func (i *importer) queryImports(filename string) fileImports {
375375
}
376376

377377
func (i *importer) copyfromImports() fileImports {
378-
std, pkg := buildImports(i.Settings, i.Queries, func(name string) bool {
379-
for _, q := range i.Queries {
380-
if q.Cmd != metadata.CmdCopyFrom {
381-
continue
382-
}
378+
copyFromQueries := make([]Query, 0, len(i.Queries))
379+
for _, q := range i.Queries {
380+
if q.Cmd == metadata.CmdCopyFrom {
381+
copyFromQueries = append(copyFromQueries, q)
382+
}
383+
}
384+
std, pkg := buildImports(i.Settings, copyFromQueries, func(name string) bool {
385+
for _, q := range copyFromQueries {
383386
if q.hasRetType() {
384387
if strings.HasPrefix(q.Ret.Type(), name) {
385388
return true

internal/endtoend/testdata/copyfrom_imports/postgresql/pgx/go/query.sql.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/copyfrom_imports/postgresql/pgx/query.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ INSERT INTO myschema.foo (a, b) VALUES ($1, $2);
66

77
-- name: InsertSingleValue :exec
88
INSERT INTO myschema.foo (a) VALUES ($1);
9+
10+
-- name: DeleteValues :execresult
11+
DELETE
12+
FROM myschema.foo;

0 commit comments

Comments
 (0)