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
`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).
`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
+
CREATETABLEusers (
19
+
id UUID PRIMARY KEY
20
+
);
21
+
22
+
CREATETABLEuser_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 ONu.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
+
ALTERTABLE 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 ONu.id=ua.user_id
51
+
ORDER BYu.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.
0 commit comments