Crate sqlx_pg_migrate
source · [−]Expand description
A library to run migrations on a PostgreSQL database using SQLx.
Make a directory that contains your migrations. The library will run thru
all the files in sorted order. The suggested naming convention is
000_first.sql
, 001_second.sql
and so on.
The library:
- Will create the DB if necessary.
- Will create a table named
sqlx_pg_migrate
to manage the migration state. - Will run everything in a single transaction, so all pending migrations are run, or nothing.
- Expects you to never delete or rename a migration.
- Expects you to not put a new migration between two existing ones.
- Expects file names and contents to be UTF-8.
- There are no rollbacks - just write a new migration.
You’ll need to add these two crates as dependencies:
[dependencies]
include_dir = "0.6"
sqlx-pg-migrate = "1.0"
The usage looks like this:
use sqlx_pg_migrate::migrate;
use include_dir::{include_dir, Dir};
// Use include_dir! to include your migrations into your binary.
// The path here is relative to your cargo root.
static MIGRATIONS: Dir = include_dir!("migrations");
// Somewhere, probably in main, call the migrate function with your DB URL
// and the included migrations.
migrate(&db_url, &MIGRATIONS).await?;
Enums
The various kinds of errors that can arise when running the migrations.
Functions
Runs the migrations contained in the directory. See module documentation for more information.