Struct migrant_lib::config::Config[][src]

pub struct Config { /* fields omitted */ }
Expand description

Full project configuration

Implementations

Define an explicit set of Migratable migrations to use.

The order of definition is the order in which they will be applied.

Note: When using explicit migrations, make sure any toggling of Config::use_cli_compatible_tags happens before the call to Config::use_migrations.

Example

The following uses a migrant config file for connection configuration and explicitly defines migrations with use_migrations.

extern crate migrant_lib;
use migrant_lib::{
    Config, search_for_settings_file,
    EmbeddedMigration, FileMigration, FnMigration
};

mod migrations {
    use super::*;
    pub struct Custom;
    impl Custom {
        pub fn up(_: migrant_lib::ConnConfig) -> Result<(), Box<dyn std::error::Error>> {
            print!(" <[Up!]>");
            Ok(())
        }
        pub fn down(_: migrant_lib::ConnConfig) -> Result<(), Box<dyn std::error::Error>> {
            print!(" <[Down!]>");
            Ok(())
        }
    }
}

let p = search_for_settings_file(&std::env::current_dir()?)
    .ok_or_else(|| "Settings file not found")?;
let mut config = Config::from_settings_file(&p)?;
config.use_migrations(&[
    EmbeddedMigration::with_tag("create-users-table")
        .up(include_str!("../migrations/embedded/create_users_table/up.sql"))
        .down(include_str!("../migrations/embedded/create_users_table/down.sql"))
        .boxed(),
    FileMigration::with_tag("create-places-table")
        .up("migrations/embedded/create_places_table/up.sql")?
        .down("migrations/embedded/create_places_table/down.sql")?
        .boxed(),
    FnMigration::with_tag("custom")
        .up(migrations::Custom::up)
        .down(migrations::Custom::down)
        .boxed(),
])?;

// Load applied migrations
let config = config.reload()?;

Migrations are explicitly defined

Toggle cli compatible tag validation.

Note: Make sure any calls to Config::use_cli_compatible_tags happen before any calls to Config::reload or Config::use_migrations since this is dependent on the tag format being used.

Defaults to false. When cli_compatible is set to true, migration tags will be validated in a manner compatible with the migrant CLI tool. Tags must be prefixed with a timestamp, following: [0-9]{14}_[a-z0-9-]+. When not enabled (the default), tag timestamps are optional and the migrant CLI tool will not be able to identify tags.

Check the current cli compatibility

Queries the database to reload the current applied migrations.

Note: Make sure any calls to Config::use_cli_compatible_tags happen before any calls to Config::reload since this is dependent on the tag format being used.

If the Config was initialized from a settings file, the settings will also be reloaded from the file. Returns a new Config instance.

Initialize a Config from a settings file at the given path. This does not query the database for applied migrations.

Initialize a Config using an explicitly created Settings object. This alleviates the need for a settings file. This does not query the database for applied migrations.

let settings = Settings::configure_sqlite()
    .database_path("/absolute/path/to/db.db")?
    .migration_location("/absolute/path/to/migration_dir")?
    .build()?;
let config = Config::with_settings(&settings);
// Setup migrations table
config.setup()?;

// Reload config, ping the database for applied migrations
let config = config.reload()?;

Initialize a new settings file in the given directory

Confirm the database can be accessed and setup the database migrations table if it doesn’t already exist

👎 Deprecated since 0.18.1:

renamed to migration_location

Return the absolute path to the directory containing migration folders

The location returned is dependent on whether an absolute or relative path was provided to migration_location in either a settings file or settings builder. If an absolute path was provided, that same path is returned. If a relative path was provided, the path returned will be relative to either the settings file’s directory if a settings file exists, or the current directory.

Return the absolute path to the directory containing migration folders

The location returned is dependent on whether an absolute or relative path was provided to migration_location in either a settings file or settings builder. If an absolute path was provided, that same path is returned. If a relative path was provided, the path returned will be relative to either the settings file’s directory if a settings file exists, or the current directory.

Return the database type

Return the absolute path to the database file. This is intended for sqlite databases only

Generate a database connection string. Not intended for file-based databases (sqlite)

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.