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
Note that this may make committing take some time as it'll cause your project to be recompiled, and
199
199
as an ergonomic choice it does _not_ block committing if `cargo sqlx prepare` fails.
200
200
201
-
We're working on a way for the macros to save their data to the filesystem automatically which should be part of SQLx 0.6,
202
-
so your pre-commit hook would then just need to stage the changed files.
201
+
We're working on a way for the macros to save their data to the filesystem automatically which should be part of SQLx 0.7,
202
+
so your pre-commit hook would then just need to stage the changed files. This can be enabled by creating a directory
203
+
and setting the `SQLX_OFFLINE_DIR` environment variable to it before compiling, e.g.
204
+
205
+
```shell
206
+
$ mkdir .sqlx
207
+
$ export SQLX_OFFLINE_DIR="./.sqlx"`
208
+
$ cargo check
209
+
```
210
+
211
+
However, this behaviour is not considered stable and it is still recommended to use `cargo sqlx prepare`.
203
212
204
213
----
205
214
@@ -253,9 +262,9 @@ Even Sisyphus would pity us.
253
262
254
263
### Why does my project using sqlx query macros not build on docs.rs?
255
264
256
-
Docs.rs doesn't have access to your database, so it needs to be provided a `sqlx-data.json` file and be instructed to set the `SQLX_OFFLINE` environment variable to true while compiling your project. Luckily for us, docs.rs creates a `DOCS_RS` environment variable that we can access in a custom build script to achieve this functionality.
265
+
Docs.rs doesn't have access to your database, so it needs to be provided prepared queries ina `.sqlx` directory and be instructed to set the `SQLX_OFFLINE` environment variable to truewhile compiling your project. Luckily forus, docs.rs creates a `DOCS_RS` environment variable that we can accessin a custom build script to achieve this functionality.
257
266
258
-
To do so, first, make sure that you have run `cargo sqlx prepare` to generate a `sqlx-data.json` file in your project.
267
+
To do so, first, make sure that you have run `cargo sqlx prepare` to generate a `.sqlx` directoryin your project.
259
268
260
269
Next, create a file called `build.rs`in the root of your project directory (at the same level as `Cargo.toml`). Add the following code to it:
Copy file name to clipboardExpand all lines: sqlx-cli/README.md
+18-19Lines changed: 18 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -106,44 +106,43 @@ error: cannot mix reversible migrations with simple migrations. All migrations s
106
106
107
107
### Enable building in "offline mode" with `query!()`
108
108
109
-
There are 3 steps to building with "offline mode":
109
+
There are 2 steps to building with "offline mode":
110
110
111
-
1. Enable the SQLx's Cargo feature `offline`
112
-
- E.g. in your `Cargo.toml`, `sqlx = { features = [ "offline", ... ] }`
113
-
2. Save query metadata for offline usage
111
+
1. Save query metadata for offline usage
114
112
-`cargo sqlx prepare`
115
-
3. Build
113
+
2. Build
116
114
117
115
Note: Saving query metadata must be run as `cargo sqlx`.
118
116
119
117
```bash
120
118
cargo sqlx prepare
121
119
```
122
120
123
-
Invoking `prepare` saves query metadata to `sqlx-data.json` in the current directory; check this file into version
124
-
control and an active database connection will no longer be needed to build your project.
121
+
Invoking `prepare` saves query metadata to `.sqlx` in the current directory.
122
+
For workspaces where several crates are using query macros, pass the `--workspace` flag
123
+
to generate a single `.sqlx` directory at the root of the workspace.
125
124
126
-
Has no effect unless the `offline` Cargo feature of `sqlx` is enabled in your project. Omitting that
127
-
feature is the most likely cause if you get a `sqlx-data.json` file that looks like this:
128
-
129
-
```json
130
-
{
131
-
"database": "PostgreSQL"
132
-
}
125
+
```bash
126
+
cargo sqlx prepare --workspace
133
127
```
134
128
129
+
Check this directory into version control and an active database connection will
130
+
no longer be needed to build your project.
131
+
135
132
---
136
133
137
134
```bash
138
135
cargo sqlx prepare --check
136
+
# OR
137
+
cargo sqlx prepare --check --workspace
139
138
```
140
139
141
-
Exits with a nonzero exit status if the data in `sqlx-data.json` is out of date with the current
142
-
database schema and queries in the project. Intended for use in Continuous Integration.
140
+
Exits with a nonzero exit status if the data in `.sqlx` is out of date with the current
141
+
database schema or queries in the project. Intended for use in Continuous Integration.
143
142
144
143
### Force building in offline mode
145
144
146
-
The presence of a `DATABASE_URL` environment variable will take precedence over the presence of `sqlx-data.json`, meaning SQLx will default to building against a database if it can. To make sure an accidentally-present `DATABASE_URL` environment variable or `.env` file does not
145
+
The presence of a `DATABASE_URL` environment variable will take precedence over the presence of `.sqlx`, meaning SQLx will default to building against a database if it can. To make sure an accidentally-present `DATABASE_URL` environment variable or `.env` file does not
147
146
result in `cargo build` (trying to) access the database, you can set the `SQLX_OFFLINE` environment
148
147
variable to `true`.
149
148
@@ -152,8 +151,8 @@ still do the right thing and connect to the database.
152
151
153
152
### Include queries behind feature flags (such as queries inside of tests)
154
153
155
-
In order for sqlx to be able to find queries behind certain feature flags you need to turn them
156
-
on by passing arguments to rustc.
154
+
In order for sqlx to be able to find queries behind certain feature flags or in tests, you need to turn them
155
+
on by passing arguments to `cargo`.
157
156
158
157
This is how you would turn all targets and features on.
0 commit comments