You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`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
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`.
rule: "!has(postgresql.explain)" # A dummy rule to trigger explain
164
164
```
165
165
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.
169
174
170
175
## Built-in rules
171
176
172
177
### sqlc/db-prepare
173
178
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
175
180
run the built-in `sqlc/db-prepare` rule. This rule will attempt to prepare
176
181
each of your queries against the connected database and report any failures.
177
182
178
183
```yaml
179
184
version: 2
180
185
sql:
181
-
- schema: "query.sql"
186
+
- schema: "schema.sql"
182
187
queries: "query.sql"
183
188
engine: "postgresql"
184
189
gen:
@@ -191,12 +196,20 @@ sql:
191
196
- sqlc/db-prepare
192
197
```
193
198
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.
0 commit comments