extern crate migrant_lib;
#[cfg(feature = "d-sqlite")]
use migrant_lib::{Config, Direction, Migrator, Settings};
#[cfg(feature = "d-sqlite")]
use std::env;
#[cfg(feature = "d-sqlite")]
fn run() -> Result<(), Box<dyn std::error::Error>> {
let path = env::current_dir()?;
let path = path.join("db/embedded_example.db");
let settings = Settings::configure_sqlite().database_path(&path)?.build()?;
let mut config = Config::with_settings(&settings);
config.setup()?;
config.use_cli_compatible_tags(true);
macro_rules! make_migration {
($tag:expr) => {
migrant_lib::EmbeddedMigration::with_tag($tag)
.up(include_str!(concat!(
"../migrations/managed/",
$tag,
"/up.sql"
)))
.down(include_str!(concat!(
"../migrations/managed/",
$tag,
"/down.sql"
)))
.boxed()
};
}
config.use_migrations(&[
make_migration!("20180105040947_initial"),
make_migration!("20180105040952_second"),
])?;
let config = config.reload()?;
println!("Applying migrations...");
Migrator::with_config(&config)
.all(true)
.show_output(false)
.swallow_completion(true)
.apply()?;
let config = config.reload()?;
migrant_lib::list(&config)?;
println!("\nUnapplying migrations...");
Migrator::with_config(&config)
.all(true)
.direction(Direction::Down)
.swallow_completion(true)
.apply()?;
let config = config.reload()?;
migrant_lib::list(&config)?;
Ok(())
}
#[cfg(not(feature = "d-sqlite"))]
fn run() -> Result<(), Box<dyn std::error::Error>> {
Err("d-sqlite database feature required")?;
Ok(())
}
pub fn main() {
if let Err(e) = run() {
println!("[ERROR] {}", e);
}
}