TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/launchbadge/sqlx/llms.txt
Use this file to discover all available pages before exploring further.
sqlx migrate command group (alias sqlx mig) manages the versioned SQL files that keep your database schema in sync with your application code. You can create new migration files, apply pending ones, revert the most recently applied migration, and check the current status of your database at a glance.
sqlx migrate add
Creates a new migration file in themigrations/ directory (created automatically if it does not exist). SQLx assigns a timestamp-based version prefix in the format YYYYMMDDHHmmss unless sequential versioning is detected.
Reversible migrations
Pass-r (or --reversible) to create a matched pair of .up.sql and .down.sql files. The up file is applied when you run sqlx migrate run; the down file is used when you run sqlx migrate revert.
Once you create a reversible migration, all subsequent migrations added in that directory are also created as reversible pairs. You can override this with
--simple to force a plain .sql file.Versioning options
| Flag | Description |
|---|---|
-r / --reversible | Create an .up.sql / .down.sql pair |
--simple | Create a plain .sql file (non-reversible) |
-t / --timestamp | Force timestamp versioning (YYYYMMDDHHmmss) |
-s / --sequential | Force sequential versioning (0001, 0002, …) |
sqlx migrate run
Compares the migration history recorded in the_sqlx_migrations table against the files on disk and applies every pending migration in ascending version order.
Dry run
Use--dry-run to list the migrations that would be applied without actually executing them:
Target version
Apply migrations only up to a specific version:sqlx migrate revert
Reverts the most recently applied migration by running its.down.sql file. Only reversible migrations can be reverted. Attempting to revert a simple migration returns an error.
--target-version to revert all migrations down to a specific version. Set it to 0 to revert everything:
sqlx migrate info
Shows the status of every migration — whether it is installed (applied) or pending. Also flags migrations whose on-disk checksum does not match the checksum recorded when they were applied.sqlx migrate build-script
Generates abuild.rs that tells Cargo to recompile your project whenever a new migration file is added. Run this once from your Cargo project root:
build.rs containing:
Common flags
Allsqlx migrate subcommands accept:
| Flag | Short | Description |
|---|---|---|
--source <DIR> | Path to the migrations directory (default: migrations/) | |
--database-url <URL> | -D | Override DATABASE_URL for this invocation |
--connect-timeout <SECONDS> | Maximum seconds to wait for a connection (default: 10) | |
--no-dotenv | Do not load .env files automatically | |
--config <PATH> | Path to sqlx.toml |
run and revert subcommands also accept:
| Flag | Description |
|---|---|
--dry-run | List migrations without applying them |
--ignore-missing | Ignore applied migrations that are no longer present on disk |
--target-version <VERSION> | Apply or revert only up to this version |
Custom migrations directory
Pass--source to use a directory other than migrations/:
sqlx.toml so you do not have to pass --source on every invocation.