Skip to content

Commit ce20889

Browse files
committed
chore: update documentation to replace sqlx-data.json with .sqlx
1 parent afb67b2 commit ce20889

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

FAQ.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,26 +180,35 @@ See Also:
180180
----
181181
### How do I compile with the macros without needing a database, e.g. in CI?
182182

183-
The macros support an offline mode which saves data for existing queries to a JSON file,
184-
so the macros can just read that file instead of talking to a database.
183+
The macros support an offline mode which saves data for existing queries to a `.sqlx` directory,
184+
so the macros can just read those instead of talking to a database.
185185

186186
See the following:
187187

188188
* [the docs for `query!()`](https://docs.rs/sqlx/0.5.5/sqlx/macro.query.html#offline-mode-requires-the-offline-feature)
189189
* [the README for `sqlx-cli`](sqlx-cli/README.md#enable-building-in-offline-mode-with-query)
190190

191-
To keep `sqlx-data.json` up-to-date you need to run `cargo sqlx prepare` before every commit that
191+
To keep `.sqlx` up-to-date you need to run `cargo sqlx prepare` before every commit that
192192
adds or changes a query; you can do this with a Git pre-commit hook:
193193

194194
```shell
195-
$ echo "cargo sqlx prepare > /dev/null 2>&1; git add sqlx-data.json > /dev/null" > .git/hooks/pre-commit
195+
$ echo "cargo sqlx prepare > /dev/null 2>&1; git add .sqlx > /dev/null" > .git/hooks/pre-commit
196196
```
197197

198198
Note that this may make committing take some time as it'll cause your project to be recompiled, and
199199
as an ergonomic choice it does _not_ block committing if `cargo sqlx prepare` fails.
200200

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`.
203212

204213
----
205214

@@ -253,9 +262,9 @@ Even Sisyphus would pity us.
253262
254263
### Why does my project using sqlx query macros not build on docs.rs?
255264
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 in a `.sqlx` directory 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.
257266

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` directory in your project.
259268

260269
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:
261270
```rs

sqlx-cli/README.md

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,44 +106,43 @@ error: cannot mix reversible migrations with simple migrations. All migrations s
106106

107107
### Enable building in "offline mode" with `query!()`
108108

109-
There are 3 steps to building with "offline mode":
109+
There are 2 steps to building with "offline mode":
110110

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
114112
- `cargo sqlx prepare`
115-
3. Build
113+
2. Build
116114

117115
Note: Saving query metadata must be run as `cargo sqlx`.
118116

119117
```bash
120118
cargo sqlx prepare
121119
```
122120

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.
125124

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
133127
```
134128

129+
Check this directory into version control and an active database connection will
130+
no longer be needed to build your project.
131+
135132
---
136133

137134
```bash
138135
cargo sqlx prepare --check
136+
# OR
137+
cargo sqlx prepare --check --workspace
139138
```
140139

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.
143142

144143
### Force building in offline mode
145144

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
147146
result in `cargo build` (trying to) access the database, you can set the `SQLX_OFFLINE` environment
148147
variable to `true`.
149148

@@ -152,8 +151,8 @@ still do the right thing and connect to the database.
152151

153152
### Include queries behind feature flags (such as queries inside of tests)
154153

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`.
157156

158157
This is how you would turn all targets and features on.
159158

0 commit comments

Comments
 (0)