#migration #postgresql #sql-migration #sqlx #migrate #directory

sqlx-pg-migrate

A library to run migrations on a PostgreSQL database using SQLx

8 stable releases

1.2.0 Feb 9, 2022
1.1.0 Nov 19, 2020
1.0.5 Nov 19, 2020
1.0.4 Jun 27, 2020
1.0.2 Jun 25, 2020

#2732 in Database interfaces

28 downloads per month

MIT license

11KB
165 lines

sqlx-pg-migrate

Build Crates.io Documentation

A library to run migrations on a PostgreSQL database using SQLx. See documentation for how to use the library.


lib.rs:

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:

[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?;

Dependencies

~12MB
~302K SLoC