Crate migrant_lib [] [src]

Build Status crates.io:migrant_lib docs

Embeddable migration management

migrant_lib allows defining and embedding management of migrations in your compiled application.

Migrations can be defined as files or functions. Migrations can be either read from files at runtime or embedded in your executable at compile time. Migration tags must all be unique. Function migrations must have the signature fn(DbConn) -> Result<(), Box<std::error::Error>>. See the embedded/programmable example for a working sample. When working with migrations, the respective database feature must be enabled (postgresql / sqlite). The entirety of the database-specific connection library will be re-exported in the types module.

fn up(_: DbConn) -> Result<(), Box<std::error::Error>> {
    print!(" Up!");
    Ok(())
}

fn down(_: DbConn) -> Result<(), Box<std::error::Error>> {
    print!(" Down!");
    Ok(())
}

config.use_migrations(vec![
    FileMigration::with_tag("initial")?
        .up("migrations/embedded/initial/up.sql")?
        .down("migrations/embedded/initial/down.sql")?
        .boxed(),
    EmbeddedMigration::with_tag("second")?
        .up(include_str!("../migrations/embedded/second/up.sql"))
        .down(include_str!("../migrations/embedded/second/down.sql"))
        .boxed(),
    FnMigration::with_tag("custom")?
        .up(up)
        .down(down)
        .boxed(),
])?;

Migrations management identical to the migrant cli tool can also be embedded. This method only supports file-base migrations generated by migrant_lib (or migrant cli). See the migrant-cli-compatible example for a working sample.

Reexports

pub use errors::*;
pub use config::ConfigInitializer;
pub use config::Config;
pub use config::Settings;
pub use migration::FileMigration;
pub use migration::EmbeddedMigration;
pub use migration::FnMigration;

Modules

config

Configuration structs

errors

Error types

migration

Embedded / programmable migrations

types

Re-exported database-specific drivers

Structs

DbConn

Database connection wrapper

Migrator

Migration applicator

Enums

DbKind

Database type being used

Direction

Represents direction to apply migrations. Up -> up.sql Down -> down.sql

Traits

Migratable

A type that can be used to define database migrations

Functions

edit

Open a migration file containing tag in its name

list

List the currently applied and available migrations under migration_location

new

Create a new migration with the given tag

search_for_settings_file

Search for a Migrant.toml file in the current and parent directories

shell

Open a repl connection to the given Config settings