2020-05-16 03:07:57 +00:00
# SQLx CLI
SQLx's associated command-line utility for managing databases, migrations, and enabling "offline"
mode with `sqlx::query!()` and friends.
2022-04-01 19:19:51 +00:00
## Install
2020-05-16 03:07:57 +00:00
2022-04-01 19:19:51 +00:00
### With Rust toolchain
2020-07-12 10:43:49 +00:00
2020-05-16 03:07:57 +00:00
```bash
2020-05-31 06:12:53 +00:00
# supports all databases supported by SQLx
2021-02-04 14:37:46 +00:00
$ cargo install sqlx-cli
2020-05-31 06:12:53 +00:00
# only for postgres
2022-01-03 21:32:58 +00:00
$ cargo install sqlx-cli --no-default-features --features native-tls,postgres
2021-11-10 02:02:32 +00:00
# use vendored OpenSSL (build from source)
$ cargo install sqlx-cli --features openssl-vendored
2021-12-29 23:49:49 +00:00
2022-01-03 21:32:58 +00:00
# use Rustls rather than OpenSSL (be sure to add the features for the databases you intend to use!)
2021-12-29 23:49:49 +00:00
$ cargo install sqlx-cli --no-default-features --features rustls
2024-10-02 18:55:21 +00:00
# only for sqlite and use the system sqlite library
$ cargo install sqlx-cli --no-default-features --features sqlite-unbundled
2020-05-16 03:07:57 +00:00
```
2022-06-01 20:53:47 +00:00
## Usage
2020-05-16 03:07:57 +00:00
2020-08-17 14:16:39 +00:00
All commands require that a database url is provided. This can be done either with the `--database-url` command line option or by setting `DATABASE_URL` , either in the environment or in a `.env` file
2020-05-16 03:07:57 +00:00
in the current working directory.
2020-05-19 01:50:15 +00:00
For more details, run `sqlx <command> --help` .
2020-05-16 03:07:57 +00:00
```dotenv
# Postgres
DATABASE_URL=postgres://postgres@localhost/my_database
```
2022-06-01 20:53:47 +00:00
### Create/drop the database at `DATABASE_URL`
2020-05-16 03:07:57 +00:00
```bash
sqlx database create
sqlx database drop
```
2022-04-01 19:19:51 +00:00
---
2022-06-01 20:53:47 +00:00
### Create and run migrations
2020-05-16 03:07:57 +00:00
```bash
2022-04-01 19:19:51 +00:00
sqlx migrate add < name >
2020-05-16 03:07:57 +00:00
```
2022-04-01 19:19:51 +00:00
2020-05-16 03:07:57 +00:00
Creates a new file in `migrations/<timestamp>-<name>.sql` . Add your database schema changes to
this new file.
---
2022-04-01 19:19:51 +00:00
2020-05-16 03:07:57 +00:00
```bash
2022-04-01 19:19:51 +00:00
sqlx migrate run
2020-05-16 03:07:57 +00:00
```
2022-04-01 19:19:51 +00:00
2020-05-16 03:07:57 +00:00
Compares the migration history of the running database against the `migrations/` folder and runs
any scripts that are still pending.
2022-04-01 19:19:51 +00:00
---
Users can provide the directory for the migration scripts to `sqlx migrate` subcommands with the `--source` flag.
```bash
sqlx migrate info --source ../relative/migrations
```
---
2022-06-01 20:53:47 +00:00
### Reverting Migrations
2021-10-01 20:44:48 +00:00
2023-08-01 18:16:43 +00:00
If you would like to create _reversible_ migrations with corresponding "up" and "down" scripts, you use the `-r` flag when creating the first migration:
2021-10-01 20:44:48 +00:00
```bash
$ sqlx migrate add -r < name >
Creating migrations/20211001154420_< name > .up.sql
Creating migrations/20211001154420_< name > .down.sql
```
After that, you can run these as above:
```bash
$ sqlx migrate run
Applied migrations/20211001154420 < name > (32.517835ms)
```
And reverts work as well:
```bash
$ sqlx migrate revert
Applied 20211001154420/revert < name >
```
2023-08-01 18:16:43 +00:00
**Note**: All the subsequent migrations will be reversible as well.
2021-10-01 20:44:48 +00:00
```bash
$ sqlx migrate add < name1 >
2023-08-01 18:16:43 +00:00
Creating migrations/20211001154420_< name > .up.sql
Creating migrations/20211001154420_< name > .down.sql
2021-10-01 20:44:48 +00:00
```
2022-06-01 20:53:47 +00:00
### Enable building in "offline mode" with `query!()`
2020-07-16 14:59:11 +00:00
2023-02-18 22:10:42 +00:00
There are 2 steps to building with "offline mode":
2022-05-31 23:59:37 +00:00
2023-02-18 22:10:42 +00:00
1. Save query metadata for offline usage
2022-05-31 23:59:37 +00:00
- `cargo sqlx prepare`
2023-02-18 22:10:42 +00:00
2. Build
2022-05-31 23:59:37 +00:00
Note: Saving query metadata must be run as `cargo sqlx` .
2020-05-16 03:07:57 +00:00
```bash
cargo sqlx prepare
```
2020-06-17 19:46:57 +00:00
2023-02-18 22:10:42 +00:00
Invoking `prepare` saves query metadata to `.sqlx` in the current directory.
For workspaces where several crates are using query macros, pass the `--workspace` flag
to generate a single `.sqlx` directory at the root of the workspace.
2021-05-30 22:52:25 +00:00
2023-02-18 22:10:42 +00:00
```bash
cargo sqlx prepare --workspace
2020-06-17 19:50:56 +00:00
```
2023-02-18 22:10:42 +00:00
Check this directory into version control and an active database connection will
no longer be needed to build your project.
2021-05-30 22:52:25 +00:00
---
2020-05-16 03:07:57 +00:00
```bash
cargo sqlx prepare --check
2023-02-18 22:10:42 +00:00
# OR
cargo sqlx prepare --check --workspace
2020-05-16 03:07:57 +00:00
```
2021-05-30 22:52:25 +00:00
2023-02-18 22:10:42 +00:00
Exits with a nonzero exit status if the data in `.sqlx` is out of date with the current
database schema or queries in the project. Intended for use in Continuous Integration.
2020-07-16 14:59:11 +00:00
2022-06-01 20:53:47 +00:00
### Force building in offline mode
2020-07-16 14:59:11 +00:00
2023-02-18 22:10:42 +00:00
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
2020-07-16 14:59:11 +00:00
result in `cargo build` (trying to) access the database, you can set the `SQLX_OFFLINE` environment
2020-10-28 12:41:57 +00:00
variable to `true` .
2021-05-30 22:52:25 +00:00
If you want to make this the default, just add it to your `.env` file. `cargo sqlx prepare` will
still do the right thing and connect to the database.
2022-02-16 05:14:04 +00:00
2022-06-08 21:56:56 +00:00
### Include queries behind feature flags (such as queries inside of tests)
2022-02-16 05:14:04 +00:00
2023-02-18 22:10:42 +00:00
In order for sqlx to be able to find queries behind certain feature flags or in tests, you need to turn them
on by passing arguments to `cargo` .
2022-02-16 05:14:04 +00:00
This is how you would turn all targets and features on.
2022-04-01 19:19:51 +00:00
2022-02-16 05:14:04 +00:00
```bash
cargo sqlx prepare -- --all-targets --all-features
```