Skip to content

Commit

Permalink
fix(pgx): copyfrom imports (#1626)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
positiveblue authored May 22, 2022
1 parent d458fbe commit 1b6ba1e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
13 changes: 8 additions & 5 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,14 @@ func (i *importer) queryImports(filename string) fileImports {
}

func (i *importer) copyfromImports() fileImports {
std, pkg := buildImports(i.Settings, i.Queries, func(name string) bool {
for _, q := range i.Queries {
if q.Cmd != metadata.CmdCopyFrom {
continue
}
copyFromQueries := make([]Query, 0, len(i.Queries))
for _, q := range i.Queries {
if q.Cmd == metadata.CmdCopyFrom {
copyFromQueries = append(copyFromQueries, q)
}
}
std, pkg := buildImports(i.Settings, copyFromQueries, func(name string) bool {
for _, q := range copyFromQueries {
if q.hasRetType() {
if strings.HasPrefix(q.Ret.Type(), name) {
return true
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ INSERT INTO myschema.foo (a, b) VALUES ($1, $2);

-- name: InsertSingleValue :exec
INSERT INTO myschema.foo (a) VALUES ($1);

-- name: DeleteValues :execresult
DELETE
FROM myschema.foo;

0 comments on commit 1b6ba1e

Please sign in to comment.