Skip to content

Commit 28d7f3a

Browse files
committed
#4513 - Support testcontainers_image
1 parent a3b0cfd commit 28d7f3a

13 files changed

Lines changed: 372 additions & 96 deletions

File tree

docs/howto/generate.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,23 @@ sql:
7474

7575
Databases configured with a `uri` must have an up-to-date schema for query analysis to work correctly, and `sqlc` does not apply schema migrations your database. Use your migration tool of choice to create the necessary
7676
tables and objects before running `sqlc generate`.
77+
78+
## Enhanced analysis with a local container
79+
80+
If you have Docker available, `sqlc` can automatically start a local PostgreSQL container, apply your schema, and use it for query analysis — no external database required. Currently supported for PostgreSQL only. Set `testcontainers_image` to the Docker image you want to use:
81+
82+
```yaml
83+
version: "2"
84+
sql:
85+
- engine: "postgresql"
86+
queries: "query.sql"
87+
schema: "schema.sql"
88+
database:
89+
testcontainers_image: "postgres:18-alpine"
90+
gen:
91+
go:
92+
out: "db"
93+
sql_package: "pgx/v5"
94+
```
95+
96+
`testcontainers_image` is mutually exclusive with `uri` and `managed`.

docs/howto/managed-databases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,7 @@ sql:
9999
rules:
100100
- sqlc/db-prepare
101101
```
102+
103+
## Local alternative
104+
105+
If you prefer not to use managed databases, you can use [`testcontainers_image`](generate.md#enhanced-analysis-with-a-local-container) to have `sqlc` spin up a local PostgreSQL container automatically using Docker.

docs/reference/config.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ The `database` mapping supports the following keys:
9797
- If true, connect to a [managed database](../howto/managed-databases.md). Defaults to `false`.
9898
- `uri`:
9999
- Database connection URI
100+
- `testcontainers_image`:
101+
- Docker image to start a local PostgreSQL container automatically (e.g. `postgres:18-alpine`). Currently supported for PostgreSQL only. Mutually exclusive with `uri` and `managed`.
100102

101103
The `uri` string can contain references to environment variables using the `${...}`
102104
syntax. In the following example, the connection string will have the value of

go.mod

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ require (
2222
github.com/spf13/pflag v1.0.10
2323
github.com/sqlc-dev/doubleclick v1.0.0
2424
github.com/sqlc-dev/marino v0.1.0
25+
github.com/testcontainers/testcontainers-go v0.43.0
26+
github.com/testcontainers/testcontainers-go/modules/postgres v0.43.0
2527
github.com/tetratelabs/wazero v1.11.0
2628
github.com/wasilibs/go-pgquery v0.0.0-20250409022910-10ac41983c07
2729
github.com/xeipuuv/gojsonschema v1.2.0
@@ -33,22 +35,67 @@ require (
3335

3436
require (
3537
cel.dev/expr v0.25.1 // indirect
38+
dario.cat/mergo v1.0.2 // indirect
3639
filippo.io/edwards25519 v1.2.0 // indirect
40+
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect
41+
github.com/Microsoft/go-winio v0.6.2 // indirect
42+
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
43+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
44+
github.com/containerd/errdefs v1.0.0 // indirect
45+
github.com/containerd/errdefs/pkg v0.3.0 // indirect
46+
github.com/containerd/log v0.1.0 // indirect
47+
github.com/containerd/platforms v0.2.1 // indirect
48+
github.com/cpuguy83/dockercfg v0.3.2 // indirect
49+
github.com/distribution/reference v0.6.0 // indirect
50+
github.com/docker/go-connections v0.6.0 // indirect
51+
github.com/docker/go-units v0.5.0 // indirect
52+
github.com/ebitengine/purego v0.10.0 // indirect
53+
github.com/felixge/httpsnoop v1.0.4 // indirect
54+
github.com/go-logr/logr v1.4.3 // indirect
55+
github.com/go-logr/stdr v1.2.2 // indirect
56+
github.com/go-ole/go-ole v1.2.6 // indirect
57+
github.com/google/uuid v1.6.0 // indirect
3758
github.com/inconshreveable/mousetrap v1.1.0 // indirect
3859
github.com/jackc/pgpassfile v1.0.0 // indirect
3960
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
4061
github.com/jackc/puddle/v2 v2.2.2 // indirect
41-
github.com/kr/text v0.2.0 // indirect
62+
github.com/klauspost/compress v1.18.5 // indirect
63+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
64+
github.com/magiconair/properties v1.8.10 // indirect
65+
github.com/moby/docker-image-spec v1.3.1 // indirect
66+
github.com/moby/go-archive v0.2.0 // indirect
67+
github.com/moby/moby/api v1.54.2 // indirect
68+
github.com/moby/moby/client v0.4.0 // indirect
69+
github.com/moby/patternmatcher v0.6.1 // indirect
70+
github.com/moby/sys/sequential v0.6.0 // indirect
71+
github.com/moby/sys/user v0.4.0 // indirect
72+
github.com/moby/sys/userns v0.1.0 // indirect
73+
github.com/moby/term v0.5.2 // indirect
4274
github.com/ncruces/go-sqlite3-wasm/v2 v2.2.35301 // indirect
4375
github.com/ncruces/julianday v1.0.0 // indirect
44-
github.com/rogpeppe/go-internal v1.10.0 // indirect
76+
github.com/opencontainers/go-digest v1.0.0 // indirect
77+
github.com/opencontainers/image-spec v1.1.1 // indirect
78+
github.com/pmezard/go-difflib v1.0.0 // indirect
79+
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
80+
github.com/shirou/gopsutil/v4 v4.26.5 // indirect
81+
github.com/sirupsen/logrus v1.9.4 // indirect
82+
github.com/stretchr/testify v1.11.1 // indirect
83+
github.com/tklauser/go-sysconf v0.3.16 // indirect
84+
github.com/tklauser/numcpus v0.11.0 // indirect
4585
github.com/wasilibs/wazero-helpers v0.0.0-20240620070341-3dff1577cd52 // indirect
4686
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
4787
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
88+
github.com/yusufpapurcu/wmi v1.2.4 // indirect
89+
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
90+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect
91+
go.opentelemetry.io/otel v1.43.0 // indirect
92+
go.opentelemetry.io/otel/metric v1.43.0 // indirect
93+
go.opentelemetry.io/otel/trace v1.43.0 // indirect
4894
go.yaml.in/yaml/v3 v3.0.4 // indirect
95+
golang.org/x/crypto v0.51.0 // indirect
4996
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
50-
golang.org/x/net v0.51.0 // indirect
51-
golang.org/x/sys v0.44.0 // indirect
97+
golang.org/x/net v0.53.0 // indirect
98+
golang.org/x/sys v0.45.0 // indirect
5299
golang.org/x/text v0.37.0 // indirect
53100
google.golang.org/genproto/googleapis/api v0.0.0-20260226221140-a57be14db171 // indirect
54101
google.golang.org/genproto/googleapis/rpc v0.0.0-20260226221140-a57be14db171 // indirect

0 commit comments

Comments
 (0)