Skip to content

Commit 5f87be3

Browse files
docs: Add managed dbs to CI/CD and vet guides (#2896)
Co-authored-by: Andrew Benton <andrew@sqlc.dev>
1 parent 0d32dbe commit 5f87be3

File tree

2 files changed

+68
-25
lines changed

2 files changed

+68
-25
lines changed

docs/howto/ci-cd.md

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Using sqlc in CI/CD
22

33
If your project has more than a single developer, we suggest running `sqlc` as
4-
part of your CI/CD pipeline. The two subcommands you'll want to run are `diff` and `vet`.
4+
part of your CI/CD pipeline. The three subcommands you'll want to run are `diff`, `vet` and `upload`
55

6-
`sqlc diff` ensures that your generated code is up to date. New developers to a project may
7-
forget to run `sqlc generate` after adding a query or updating a schema. They also might
8-
edit generated code. `sqlc diff` will catch both errors by comparing the expected output
9-
from `sqlc generate` to what's on disk.
6+
`sqlc diff` ensures that your generated code is up to date. New developers to a
7+
project may forget to run `sqlc generate` after adding a query or updating a
8+
schema. They also might edit generated code. `sqlc diff` will catch both errors
9+
by comparing the expected output from `sqlc generate` to what's on disk.
1010

1111
```diff
1212
% sqlc diff
@@ -26,11 +26,15 @@ helpful in catching anti-patterns before they make it into production. Please
2626
see the [vet](vet.md) documentation for a complete guide to adding lint rules
2727
for your project.
2828

29+
`sqlc upload` pushes your database schema and queries to sqlc Cloud. Once
30+
uploaded, we ensure that future releases of sqlc do not break your code. Learn
31+
more about uploading projects [here](upload.md)
32+
2933
## General setup
3034

3135
Install `sqlc` using the [suggested instructions](../overview/install).
3236

33-
Create two steps in your pipeline, one for `sqlc diff`and one for `sqlc vet`.
37+
Create two steps in your pipeline, one for `sqlc diff` and one for `sqlc vet`. Run `sqlc upload` after merge on your `main` branch.
3438

3539
## GitHub Actions
3640

@@ -39,10 +43,6 @@ GitHub Action to install `sqlc`. The action uses the built-in
3943
[tool-cache](https://github.com/actions/toolkit/blob/main/packages/tool-cache/README.md)
4044
to speed up the installation process.
4145

42-
## GitHub Workflows
43-
44-
We suggest running `diff`, `vet` and `upload` as part of your workflow.
45-
4646
### diff
4747

4848
The following GitHub Workflow configuration runs `sqlc diff` on every push.
@@ -63,7 +63,7 @@ jobs:
6363
6464
### vet
6565
66-
The following GitHub Workflow configuration runs [`sqlc vet`](vet.md) on every push.
66+
The following GitHub Workflow configuration runs [sqlc vet](vet.md) on every push.
6767
You can use `sqlc vet` without a database connection, but you'll need one if your
6868
`sqlc` configuration references the built-in `sqlc/db-prepare` lint rule.
6969

@@ -100,10 +100,38 @@ jobs:
100100
- run: sqlc vet
101101
```
102102
103+
#### Managed databases
104+
105+
```{note}
106+
Managed databases are powered by [sqlc Cloud](https://dashboard.sqlc.dev). Sign up for [free](https://dashboard.sqlc.dev) today.
107+
```
108+
109+
If you're using [managed databases](managed-databases.md), the `services` block
110+
in the previous workflow isn't required.
111+
112+
```yaml
113+
name: sqlc
114+
on: [push]
115+
jobs:
116+
vet:
117+
runs-on: ubuntu-latest
118+
steps:
119+
- uses: actions/checkout@v3
120+
- uses: sqlc-dev/setup-sqlc@v3
121+
with:
122+
sqlc-version: '1.22.0'
123+
- run: sqlc vet
124+
```
125+
103126
### upload
104127
105-
The following GitHub Workflow configuration runs [`sqlc upload`](upload.md) on
106-
every push to `main`.
128+
```{note}
129+
Project uploads are powered by [sqlc Cloud](https://dashboard.sqlc.dev). Sign up for [free](https://dashboard.sqlc.dev) today.
130+
```
131+
132+
The following GitHub Workflow configuration runs [sqlc upload](upload.md) on
133+
every push to `main`. Create an auth token via the
134+
[dashboard](https://dashboard.sqlc.dev).
107135

108136
```yaml
109137
name: sqlc
@@ -120,4 +148,4 @@ jobs:
120148
- run: sqlc upload
121149
env:
122150
SQLC_AUTH_TOKEN: ${{ secrets.SQLC_AUTH_TOKEN }}
123-
```
151+
```

docs/howto/vet.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44

55
`sqlc vet` runs queries through a set of lint rules.
66

7-
Rules are defined in the `sqlc` [configuration](../reference/config) file. They consist
8-
of a name, message, and a [Common Expression Language (CEL)](https://github.com/google/cel-spec)
9-
expression. Expressions are evaluated using [cel-go](https://github.com/google/cel-go).
10-
If an expression evaluates to `true`, `sqlc vet` will report an error using the given message.
7+
Rules are defined in the `sqlc` [configuration](../reference/config) file. They
8+
consist of a name, message, and a [Common Expression Language
9+
(CEL)](https://github.com/google/cel-spec) expression. Expressions are evaluated
10+
using [cel-go](https://github.com/google/cel-go). If an expression evaluates to
11+
`true`, `sqlc vet` will report an error using the given message.
1112

1213
## Defining lint rules
1314

14-
Each lint rule's CEL expression has access to information from your sqlc configuration and queries
15-
via variables defined in the following proto messages.
15+
Each lint rule's CEL expression has access to information from your sqlc
16+
configuration and queries via variables defined in the following proto messages.
1617

1718
```proto
1819
message Config
@@ -190,13 +191,27 @@ sql:
190191
- sqlc/db-prepare
191192
```
192193

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.
195+
196+
```yaml
197+
version: 2
198+
sql:
199+
- schema: "query.sql"
200+
queries: "query.sql"
201+
engine: "postgresql"
202+
gen:
203+
go:
204+
package: "authors"
205+
out: "db"
206+
database:
207+
managed: true
208+
rules:
209+
- sqlc/db-prepare
210+
```
211+
193212
To see this in action, check out the [authors
194213
example](https://github.com/sqlc-dev/sqlc/blob/main/examples/authors/sqlc.yaml).
195214

196-
Please note that `sqlc` does not manage or migrate your database. Use your
197-
migration tool of choice to create the necessary database tables and objects
198-
before running `sqlc vet` with the `sqlc/db-prepare` rule.
199-
200215
## Running lint rules
201216

202217
When you add the name of a defined rule to the rules list
@@ -238,4 +253,4 @@ For any query, you can tell `sqlc vet` not to evaluate lint rules using the
238253
/* @sqlc-vet-disable */
239254
SELECT * FROM authors
240255
WHERE id = ? LIMIT 1;
241-
```
256+
```

0 commit comments

Comments
 (0)