Skip to content

Commit 3a97e74

Browse files
docs: Document database-backed query analyzer (#2904)
* docs: Document database-backed query analyzer --------- Co-authored-by: Andrew Benton <[email protected]>
1 parent dece28f commit 3a97e74

File tree

6 files changed

+137
-20
lines changed

6 files changed

+137
-20
lines changed

docs/howto/generate.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# `generate` - Generating code
2+
3+
`sqlc generate` parses SQL, analyzes the results, and outputs code. Your schema and queries are stored in separate SQL files. The paths to these files live in a `sqlc.yaml` configuration file.
4+
5+
```yaml
6+
version: "2"
7+
sql:
8+
- engine: "postgresql"
9+
queries: "query.sql"
10+
schema: "schema.sql"
11+
gen:
12+
go:
13+
package: "tutorial"
14+
out: "tutorial"
15+
sql_package: "pgx/v5"
16+
```
17+
18+
We've written extensive docs on [retrieving](select.md), [inserting](insert.md),
19+
[updating](update.md), and [deleting](delete.md) rows.
20+
21+
By default, sqlc runs its analysis using a built-in query analysis engine. While fast, this engine can't handle some complex queries and type-inference.
22+
23+
You can configure sqlc to use a database connection for enhanced analysis using metadata from that database.
24+
25+
The database-backed analyzer currently supports PostgreSQL, with [MySQL](https://github.com/sqlc-dev/sqlc/issues/2902) and [SQLite](https://github.com/sqlc-dev/sqlc/issues/2903)
26+
support planned in the future.
27+
28+
## Enhanced analysis with managed databases
29+
30+
```{note}
31+
Managed databases are powered by [sqlc Cloud](https://dashboard.sqlc.dev). Sign up for [free](https://dashboard.sqlc.dev) today.
32+
```
33+
34+
With [managed databases](managed-databases.md) configured, `generate` will automatically create a hosted ephemeral database with your
35+
schema and use that database to improve its query analysis. And sqlc will cache its analysis locally
36+
on a per-query basis to speed up future `generate` runs. This saves you the trouble of running and maintaining a database with
37+
an up-to-date schema. Here's a minimal working configuration:
38+
39+
```yaml
40+
version: "2"
41+
cloud:
42+
project: "<PROJECT_ID>"
43+
sql:
44+
- engine: "postgresql"
45+
queries: "query.sql"
46+
schema: "schema.sql"
47+
database:
48+
managed: true
49+
gen:
50+
go:
51+
out: "db"
52+
sql_package: "pgx/v5"
53+
```
54+
55+
## Enhanced analysis using your own database
56+
57+
You can opt-in to database-backed analysis using your own database, by providing a `uri` in your sqlc
58+
[database](../reference/config.md#database) configuration.
59+
60+
The `uri` string can contain references to environment variables using the `${...}`
61+
syntax. In the following example, the connection string will have the value of
62+
the `PG_PASSWORD` environment variable set as its password.
63+
64+
```yaml
65+
version: "2"
66+
sql:
67+
- engine: "postgresql"
68+
queries: "query.sql"
69+
schema: "schema.sql"
70+
database:
71+
uri: "postgres://postgres:${PG_PASSWORD}@localhost:5432/postgres"
72+
gen:
73+
go:
74+
out: "db"
75+
sql_package: "pgx/v5"
76+
```
77+
78+
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
79+
tables and objects before running `sqlc generate`.

docs/howto/upload.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Uploading projects
1+
# `upload` - Uploading projects
22

33
```{note}
44
Project uploads are powered by [sqlc Cloud](https://dashboard.sqlc.dev). Sign up for [free](https://dashboard.sqlc.dev) today.
@@ -18,7 +18,7 @@ After creating a project, add the project ID to your sqlc configuration file.
1818
```yaml
1919
version: "2"
2020
cloud:
21-
project: "<PROJECT-ID>"
21+
project: "<PROJECT_ID>"
2222
```
2323
2424
You'll also need to create an auth token and make it available via the

docs/howto/vet.md

+24-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Linting queries
1+
# `vet` - Linting queries
22

33
*Added in v1.19.0*
44

@@ -43,7 +43,7 @@ message Parameter
4343
```
4444

4545
In addition to this basic information, when you have a PostgreSQL or MySQL
46-
[database connection configured](../reference/config.html#database)
46+
[database connection configured](../reference/config.md#database)
4747
each CEL expression has access to the output from running `EXPLAIN ...` on your query
4848
via the `postgresql.explain` and `mysql.explain` variables.
4949
This output is quite complex and depends on the structure of your query but sqlc attempts
@@ -95,7 +95,7 @@ rules:
9595
The CEL expression environment has two variables containing `EXPLAIN ...` output,
9696
`postgresql.explain` and `mysql.explain`. `sqlc` only populates the variable associated with
9797
your configured database engine, and only when you have a
98-
[database connection configured](../reference/config.html#database).
98+
[database connection configured](../reference/config.md#database).
9999

100100
For the `postgresql` engine, `sqlc` runs
101101

@@ -163,22 +163,27 @@ rules:
163163
rule: "!has(postgresql.explain)" # A dummy rule to trigger explain
164164
```
165165

166-
Please note that `sqlc` does not manage or migrate your database. Use your
167-
migration tool of choice to create the necessary database tables and objects
168-
before running `sqlc vet` with rules that depend on `EXPLAIN ...` output.
166+
Please note that databases configured with a `uri` must have an up-to-date
167+
schema for `vet` to work correctly, and `sqlc` does not apply schema migrations
168+
to your database. Use your migration tool of choice to create the necessary
169+
tables and objects before running `sqlc vet` with rules that depend on
170+
`EXPLAIN ...` output.
171+
172+
Alternatively, configure [managed databases](managed-databases.md) to have
173+
`sqlc` create hosted ephemeral databases with the correct schema automatically.
169174

170175
## Built-in rules
171176

172177
### sqlc/db-prepare
173178

174-
When a [database](../reference/config.html#database) connection is configured, you can
179+
When a [database](../reference/config.md#database) connection is configured, you can
175180
run the built-in `sqlc/db-prepare` rule. This rule will attempt to prepare
176181
each of your queries against the connected database and report any failures.
177182

178183
```yaml
179184
version: 2
180185
sql:
181-
- schema: "query.sql"
186+
- schema: "schema.sql"
182187
queries: "query.sql"
183188
engine: "postgresql"
184189
gen:
@@ -191,12 +196,20 @@ sql:
191196
- sqlc/db-prepare
192197
```
193198

194-
Databases configured with a `uri` must have an up-to-date schema, and `sqlc` does not apply schema migrations your database. You can configure [managed databases](managed-databases.md) instead to have `sqlc` create and migrate databases automatically.
199+
Please note that databases configured with a `uri` must have an up-to-date
200+
schema for `vet` to work correctly, and `sqlc` does not apply schema migrations
201+
to your database. Use your migration tool of choice to create the necessary
202+
tables and objects before running `sqlc vet` with the `sqlc/db-prepare` rule.
203+
204+
Alternatively, configure [managed databases](managed-databases.md) to have
205+
`sqlc` create hosted ephemeral databases with the correct schema automatically.
195206

196207
```yaml
197208
version: 2
209+
cloud:
210+
project: "<PROJECT_ID>"
198211
sql:
199-
- schema: "query.sql"
212+
- schema: "schema.sql"
200213
queries: "query.sql"
201214
engine: "postgresql"
202215
gen:
@@ -215,7 +228,7 @@ example](https://github.com/sqlc-dev/sqlc/blob/main/examples/authors/sqlc.yaml).
215228
## Running lint rules
216229

217230
When you add the name of a defined rule to the rules list
218-
for a [sql package](https://docs.sqlc.dev/en/stable/reference/config.html#sql),
231+
for a [sql package](../reference/config.md#sql),
219232
`sqlc vet` will evaluate that rule against every query in the package.
220233

221234
In the example below, two rules are defined but only one is enabled.

docs/index.rst

+16-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ code ever again.
3636
tutorials/getting-started-postgresql.md
3737
tutorials/getting-started-sqlite.md
3838

39+
.. toctree::
40+
:maxdepth: 2
41+
:caption: Commands
42+
:hidden:
43+
44+
howto/generate.md
45+
howto/vet.md
46+
howto/upload.md
47+
3948
.. toctree::
4049
:maxdepth: 2
4150
:caption: How-to Guides
@@ -57,10 +66,12 @@ code ever again.
5766
howto/overrides.md
5867
howto/rename.md
5968

60-
howto/vet.md
69+
.. toctree::
70+
:maxdepth: 3
71+
:caption: sqlc Cloud
72+
:hidden:
73+
6174
howto/managed-databases.md
62-
howto/ci-cd.md
63-
howto/upload.md
6475

6576
.. toctree::
6677
:maxdepth: 3
@@ -81,7 +92,8 @@ code ever again.
8192
:caption: Conceptual Guides
8293
:hidden:
8394

95+
howto/ci-cd.md
8496
guides/using-go-and-pgx.rst
85-
guides/development.md
8697
guides/plugins.md
98+
guides/development.md
8799
guides/privacy.md

docs/reference/config.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ file must be in the directory where the `sqlc` command is run.
77

88
```yaml
99
version: "2"
10+
cloud:
11+
project: "<PROJECT_ID>"
1012
sql:
1113
- schema: "postgresql/schema.sql"
1214
queries: "postgresql/query.sql"
@@ -16,7 +18,7 @@ sql:
1618
package: "authors"
1719
out: "postgresql"
1820
database:
19-
uri: "postgresql://postgres:postgres@localhost:5432/postgres"
21+
managed: true
2022
rules:
2123
- sqlc/db-prepare
2224
- schema: "mysql/schema.sql"
@@ -46,6 +48,8 @@ Each mapping in the `sql` collection has the following keys:
4648
- A mapping to configure database connections. See [database](#database) for the supported keys.
4749
- `rules`:
4850
- A collection of rule names to run via `sqlc vet`. See [rules](#rules) for configuration options.
51+
- `analzyer`:
52+
- A mapping to configure query analysis. See [analyzer](#analyzer) for the supported keys.
4953
- `strict_function_checks`
5054
- If true, return an error if a called SQL function does not exist. Defaults to `false`.
5155

@@ -85,6 +89,8 @@ sql:
8589

8690
The `database` mapping supports the following keys:
8791

92+
- `managed`:
93+
- If true, connect to a [managed database](../howto/managed-databases.md). Defaults to `false`.
8894
- `uri`:
8995
- Database connection URI
9096

@@ -105,7 +111,14 @@ sql:
105111
package: authors
106112
out: postgresql
107113
```
108-
114+
115+
### analyzer
116+
117+
The `analyzer` mapping supports the following keys:
118+
119+
- `database`:
120+
- If false, do not use the configured database for query analysis. Defaults to `true`.
121+
109122
### gen
110123

111124
The `gen` mapping supports the following keys:

docs/tutorials/getting-started-postgresql.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ following contents:
3232
```yaml
3333
version: "2"
3434
cloud:
35-
project: "<your project id>"
35+
project: "<PROJECT_ID>"
3636
sql:
3737
- engine: "postgresql"
3838
queries: "query.sql"

0 commit comments

Comments
 (0)