Skip to content

Commit 2c25c42

Browse files
authored
fix(copyfrom): Fix imports when non-copyfrom queries needed imports that copyfrom queries didn't (#1386)
The attached test case is broken without the fix and passes with the fix.
1 parent 3555469 commit 2c25c42

File tree

7 files changed

+154
-1
lines changed

7 files changed

+154
-1
lines changed

internal/codegen/golang/imports.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (i *importer) Imports(filename string) [][]ImportSpec {
9999
case querierFileName:
100100
return mergeImports(i.interfaceImports())
101101
case copyfromFileName:
102-
return mergeImports(i.interfaceImports())
102+
return mergeImports(i.copyfromImports())
103103
default:
104104
return mergeImports(i.queryImports(filename))
105105
}
@@ -367,3 +367,28 @@ func (i *importer) queryImports(filename string) fileImports {
367367

368368
return sortedImports(std, pkg)
369369
}
370+
371+
func (i *importer) copyfromImports() fileImports {
372+
std, pkg := buildImports(i.Settings, i.Queries, func(name string) bool {
373+
for _, q := range i.Queries {
374+
if q.Cmd != metadata.CmdCopyFrom {
375+
continue
376+
}
377+
if q.hasRetType() {
378+
if strings.HasPrefix(q.Ret.Type(), name) {
379+
return true
380+
}
381+
}
382+
if !q.Arg.isEmpty() {
383+
if strings.HasPrefix(q.Arg.Type(), name) {
384+
return true
385+
}
386+
}
387+
}
388+
return false
389+
})
390+
391+
std["context"] = struct{}{}
392+
393+
return sortedImports(std, pkg)
394+
}

internal/endtoend/testdata/copyfrom_imports/postgresql/pgx/go/copyfrom.go

Lines changed: 41 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/go/db.go

Lines changed: 31 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/go/models.go

Lines changed: 12 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/go/query.sql.go

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE SCHEMA myschema;
2+
CREATE TABLE myschema.foo (a text, b integer);
3+
4+
-- name: InsertValues :copyfrom
5+
INSERT INTO myschema.foo (a, b) VALUES ($1, $2);
6+
7+
-- name: InsertSingleValue :exec
8+
INSERT INTO myschema.foo (a) VALUES ($1);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "postgresql",
7+
"sql_package": "pgx/v4",
8+
"name": "querytest",
9+
"schema": "query.sql",
10+
"queries": "query.sql"
11+
}
12+
]
13+
}

0 commit comments

Comments
 (0)