Skip to content

Commit 94a6c32

Browse files
authored
docs: Add verify and push documentation (#2998)
Add documentation for verify and push
1 parent ca8bfbb commit 94a6c32

File tree

9 files changed

+368
-71
lines changed

9 files changed

+368
-71
lines changed

docs/conf.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
extensions = [
3434
'myst_parser',
3535
'sphinx_rtd_theme',
36-
"sphinx_favicon",
36+
'sphinx_favicon',
37+
'sphinxext.rediraffe',
3738
]
3839

3940
# Add any paths that contain templates here, relative to this directory.
@@ -75,4 +76,8 @@ def setup(app):
7576
myst_enable_extensions = [
7677
"attrs_inline",
7778
"colon_fence",
78-
]
79+
]
80+
81+
rediraffe_redirects = {
82+
"howto/upload.md": "howto/push.md",
83+
}

docs/howto/ci-cd.md

+49-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
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 three subcommands you'll want to run are `diff`, `vet` and `upload`
4+
part of your CI/CD pipeline. The four subcommands you'll want to run are `diff`,
5+
`vet`, `verify` and `push`
56

67
`sqlc diff` ensures that your generated code is up to date. New developers to a
78
project may forget to run `sqlc generate` after adding a query or updating a
@@ -26,15 +27,21 @@ helpful in catching anti-patterns before they make it into production. Please
2627
see the [vet](vet.md) documentation for a complete guide to adding lint rules
2728
for your project.
2829

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)
30+
`sqlc verify` ensures that schema changes do not break production. Existing
31+
queries are checked against new schema changes for correctness. Please see the
32+
[verify](verify.md) documentation for a complete guide.
33+
34+
35+
`sqlc push` pushes your database schema, queries and configuration to sqlc
36+
Cloud. These archives are used by `verify` to catch breaking changes to your
37+
database schema. Learn more about uploading projects [here](push.md)
3238

3339
## General setup
3440

3541
Install `sqlc` using the [suggested instructions](../overview/install).
3642

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.
43+
Create three steps in your pipeline for `sqlc diff`, `sqlc vet`, and `sqlc
44+
verify`. Run `sqlc push` after merge on your `main` branch.
3845

3946
## GitHub Actions
4047

@@ -123,29 +130,61 @@ jobs:
123130
- run: sqlc vet
124131
```
125132
126-
### upload
133+
### push
127134
128135
```{note}
129-
Project uploads are powered by [sqlc Cloud](https://dashboard.sqlc.dev). Sign up for [free](https://dashboard.sqlc.dev) today.
136+
Pushing a project is powered by [sqlc Cloud](https://dashboard.sqlc.dev). Sign up for [free](https://dashboard.sqlc.dev) today.
130137
```
131138

132-
The following GitHub Workflow configuration runs [sqlc upload](upload.md) on
139+
The following GitHub Workflow configuration runs [sqlc push](push.md) on
133140
every push to `main`. Create an auth token via the
134141
[dashboard](https://dashboard.sqlc.dev).
135142

136143
```yaml
137144
name: sqlc
138145
on: [push]
139146
jobs:
140-
upload:
147+
push:
141148
runs-on: ubuntu-latest
142149
if: ${{ github.ref == 'refs/heads/main' }}
143150
steps:
144151
- uses: actions/checkout@v3
145152
- uses: sqlc-dev/setup-sqlc@v3
146153
with:
147154
sqlc-version: '1.23.0'
148-
- run: sqlc upload
155+
- run: sqlc push
149156
env:
150157
SQLC_AUTH_TOKEN: ${{ secrets.SQLC_AUTH_TOKEN }}
151158
```
159+
160+
### verify
161+
162+
```{note}
163+
Verify database migrations is powered by [sqlc Cloud](https://dashboard.sqlc.dev). Sign up for [free](https://dashboard.sqlc.dev) today.
164+
```
165+
166+
```yaml
167+
name: sqlc
168+
on: [push]
169+
jobs:
170+
verify:
171+
runs-on: ubuntu-latest
172+
steps:
173+
- uses: actions/checkout@v3
174+
- uses: sqlc-dev/setup-sqlc@v3
175+
with:
176+
sqlc-version: '1.23.0'
177+
- run: sqlc verify
178+
env:
179+
SQLC_AUTH_TOKEN: ${{ secrets.SQLC_AUTH_TOKEN }}
180+
push:
181+
runs-on: ubuntu-latest
182+
if: ${{ github.ref == 'refs/heads/main' }}
183+
steps:
184+
- uses: sqlc-dev/setup-sqlc@v3
185+
with:
186+
sqlc-version: '1.23.0'
187+
- run: sqlc push
188+
env:
189+
SQLC_AUTH_TOKEN: ${{ secrets.SQLC_AUTH_TOKEN }}
190+
``````

docs/howto/push.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# `push` - Uploading projects
2+
3+
```{note}
4+
`push` is powered by [sqlc Cloud](https://dashboard.sqlc.dev). Sign up for [free](https://dashboard.sqlc.dev) today.
5+
```
6+
7+
*Added in v1.24.0*
8+
9+
We've renamed the `upload` sub-command to `push`. We've also changed the data sent along in a push request. Upload used to include the configuration file, migrations, queries, and all generated code. Push drops the generated code in favor of including the [plugin.GenerateRequest](https://buf.build/sqlc/sqlc/docs/main:plugin#plugin.GenerateRequest), which is the protocol buffer message we pass to codegen plugins.
10+
11+
## Add configuration
12+
13+
After creating a project, add the project ID to your sqlc configuration file.
14+
15+
```yaml
16+
version: "2"
17+
cloud:
18+
project: "<PROJECT_ID>"
19+
```
20+
21+
You'll also need to create an auth token and make it available via the
22+
`SQLC_AUTH_TOKEN` environment variable.
23+
24+
```shell
25+
export SQLC_AUTH_TOKEN=sqlc_xxxxxxxx
26+
```
27+
28+
## Dry run
29+
30+
You can see what's included when uploading your project by using using the
31+
`--dry-run` flag:
32+
33+
```shell
34+
$ sqlc push --dry-run
35+
2023/11/21 10:39:51 INFO config file=sqlc.yaml bytes=912
36+
2023/11/21 10:39:51 INFO codegen_request queryset=app file=codegen_request.pb
37+
2023/11/21 10:39:51 INFO schema queryset=app file=migrations/00001_initial.sql bytes=3033
38+
2023/11/21 10:39:51 INFO query queryset=app file=queries/app.sql bytes=1150
39+
```
40+
41+
The output is the files `sqlc` would have sent without the `--dry-run` flag.
42+
43+
## Push
44+
45+
Once you're ready to push, remove the `--dry-run` flag.
46+
47+
```shell
48+
sqlc push
49+
```
50+
51+
### Annotations
52+
53+
Annotations are added to each push request. By default, we include these environment variables (if they are present).
54+
55+
```
56+
GITHUB_REPOSITORY
57+
GITHUB_REF
58+
GITHUB_REF_NAME
59+
GITHUB_REF_TYPE
60+
GITHUB_SHA
61+
```

docs/howto/upload.md

-50
This file was deleted.

docs/howto/verify.md

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# `verify` - Verifying database schema changes
2+
3+
```{note}
4+
`verify` is powered by [sqlc Cloud](https://dashboard.sqlc.dev). Sign up for [free](https://dashboard.sqlc.dev) today.
5+
```
6+
7+
*Added in v1.24.0*
8+
9+
Schema updates and poorly-written queries often bring down production databases. That’s bad.
10+
11+
Out of the box, `sqlc generate` catches some of these issues. Running `sqlc vet` with the `sqlc/db-prepare` rule catches more subtle problems. But there is a large class of issues that sqlc can’t prevent by looking at current schema and queries alone.
12+
13+
For instance, when a schema change is proposed, existing queries and code running in production might fail when the schema change is applied. Enter `sqlc verify`, which analyzes existing queries against new schema changes and errors if there are any issues.
14+
15+
Let's look at an example. Assume you have these two tables in production.
16+
17+
```sql
18+
CREATE TABLE users (
19+
id UUID PRIMARY KEY
20+
);
21+
22+
CREATE TABLE user_actions (
23+
id UUID PRIMARY KEY,
24+
user_id UUID NOT NULL,
25+
action TEXT,
26+
created_at TIMESTAMP
27+
);
28+
```
29+
30+
Your application contains the following query to join user actions against the users table.
31+
32+
```sql
33+
-- name: GetUserActions :many
34+
SELECT * FROM users u
35+
JOIN user_actions ua ON u.id = ua.user_id
36+
ORDER BY created_at;
37+
```
38+
39+
So far, so good. Then assume you propose this schema change:
40+
41+
```sql
42+
ALTER TABLE users ADD COLUMN created_at TIMESTAMP;
43+
```
44+
45+
Running `sqlc generate` fails with this change, returning a `column reference "created_at" is ambiguous` error. You update your query to fix the issue.
46+
47+
```sql
48+
-- name: GetUserActions :many
49+
SELECT * FROM users u
50+
JOIN user_actions ua ON u.id = ua.user_id
51+
ORDER BY u.created_at;
52+
```
53+
54+
While that change fixes the issue, there's a production outage waiting to happen. When the schema change is applied, the existing `GetUserActions` query will begin to fail. The correct way to fix this is to deploy the updated query before applying the schema migration.
55+
56+
It ensures migrations are safe to deploy by sending your current schema and queries to sqlc cloud. There, we run the queries for your latest push against your new schema changes. This check catches backwards incompatible schema changes for existing queries.
57+
58+
Here `sqlc verify` alerts you to the fact that ORDER BY "created_at" is ambiguous.
59+
60+
```sh
61+
$ sqlc verify
62+
FAIL: app query.sql
63+
64+
=== Failed
65+
=== FAIL: app query.sql GetUserActions
66+
ERROR: column reference "created_at" is ambiguous (SQLSTATE 42702)
67+
```
68+
69+
By the way, this scenario isn't made up! It happened to us a few weeks ago. We've been happily testing early versions of `verify` for the last two weeks and haven't had any issues since.
70+
71+
This type of verification is only the start. If your application is deployed on-prem by your customers, `verify` could tell you if it's safe for your customers to rollback to an older version of your app, even after schema migrations have been run.
72+
73+
Using `verify` requires that you push your queries and schema when you tag a release of your application. We run it on every push to main, as we continuously deploy those commits.
74+
75+
## Authentication
76+
77+
`sqlc` expects to find a valid auth token in the value of the `SQLC_AUTH_TOKEN`
78+
environment variable. You can create an auth token via the [dashboard](https://dashboard.sqlc.dev).
79+
80+
```shell
81+
export SQLC_AUTH_TOKEN=sqlc_xxxxxxxx
82+
```
83+
84+
85+
## Expected workflow
86+
87+
Using `sqlc verify` requires pushing your queries and schema to sqlc Cloud. When
88+
you release a new version of your application, you should push your schema and
89+
queries as well. For example, we run `sqlc push` after any change has been
90+
merged into our `main` branch on Github, as we deploy every commit to
91+
production.
92+
93+
Locally or in pull requests, run `sqlc verify` to check that existing queries
94+
continue to work with your current database schema.

docs/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ code ever again.
4242
:hidden:
4343

4444
howto/generate.md
45+
howto/verify.md
4546
howto/vet.md
46-
howto/upload.md
4747

4848
.. toctree::
4949
:maxdepth: 2

0 commit comments

Comments
 (0)