SQLx's associated command-line utility for managing databases, migrations, and enabling "offline"
mode with sqlx::query!() and friends.
# supports all databases supported by SQLx
$ cargo install --version=0.1.0-beta.1 sqlx-cli
# only for postgres
$ cargo install --version=0.1.0-beta.1 sqlx-cli --no-default-features --features postgresAll commands require DATABASE_URL to be set, either in the environment or in a .env file
in the current working directory.
database and migrate subcommands support only Postgres; MySQL and SQLite are TODO.
For more details, run sqlx <command> --help.
# Postgres
DATABASE_URL=postgres://postgres@localhost/my_databasesqlx database create
sqlx database drop$ sqlx migrate add <name>Creates a new file in migrations/<timestamp>-<name>.sql. Add your database schema changes to
this new file.
$ sqlx migration runCompares the migration history of the running database against the migrations/ folder and runs
any scripts that are still pending.
Note: must be run as cargo sqlx.
cargo sqlx prepareSaves query data to sqlx-data.json in the current directory; check this file into version control
and an active database connection will no longer be needed to build your project.
Has no effect unless the offline feature of sqlx is enabled in your project. Omitting that feature is the most likely cause if you get a sqlx-data.json file that looks like this:
{
"database": "PostgreSQL"
}cargo sqlx prepare --checkExits with a nonzero exit status if the data in sqlx-data.json is out of date with the current
database schema and queries in the project. Intended for use in Continuous Integration.
To make sure an accidentally-present DATABASE_URL environment variable or .env file does not
result in cargo build (trying to) access the database, you can set the SQLX_OFFLINE environment
variable.