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:

  1. Will create the DB if necessary.
  2. Will create a table named sqlx_pg_migrate to manage the migration state.
  3. Will run everything in a single transaction, so all pending migrations are run, or nothing.
  4. Expects you to never delete or rename a migration.
  5. Expects you to not put a new migration between two existing ones.
  6. Expects file names and contents to be UTF-8.
  7. There are no rollbacks - just write a new migration.

You’ll need to add these two crates as 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?;


The various kinds of errors that can arise when running the migrations.


Runs the migrations contained in the directory. See module documentation for more information.