use serde::Serialize;
use crate::directive::MigrationDirectives;
use crate::error::WaypointError;
#[cfg(feature = "mysql")]
pub use crate::engines::mysql::migrate::{
execute as execute_mysql, execute_with_options as execute_mysql_with_options,
};
#[cfg(feature = "postgres")]
pub use crate::engines::postgres::migrate::{execute, execute_with_options};
#[derive(Debug, Serialize)]
pub struct MigrateReport {
pub migrations_applied: usize,
pub total_time_ms: i32,
pub details: Vec<MigrateDetail>,
pub hooks_executed: usize,
pub hooks_time_ms: i32,
}
#[derive(Debug, Serialize)]
pub struct MigrateDetail {
pub version: Option<String>,
pub description: String,
pub script: String,
pub execution_time_ms: i32,
}
pub(crate) enum GuardAction {
Continue,
Skip,
Error(WaypointError),
}
pub(crate) fn should_run_in_environment(
directives: &MigrationDirectives,
current_env: Option<&str>,
) -> bool {
if directives.env.is_empty() {
return true;
}
let env = match current_env {
Some(e) => e,
None => return true,
};
directives.env.iter().any(|e| e.eq_ignore_ascii_case(env))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_should_run_in_environment_no_directives() {
let directives = MigrationDirectives::default();
assert!(should_run_in_environment(&directives, Some("production")));
assert!(should_run_in_environment(&directives, None));
}
#[test]
fn test_should_run_in_environment_matches() {
let directives = MigrationDirectives {
env: vec!["production".to_string(), "staging".to_string()],
..Default::default()
};
assert!(should_run_in_environment(&directives, Some("production")));
assert!(should_run_in_environment(&directives, Some("staging")));
assert!(!should_run_in_environment(&directives, Some("dev")));
}
#[test]
fn test_should_run_in_environment_case_insensitive() {
let directives = MigrationDirectives {
env: vec!["PROD".to_string()],
..Default::default()
};
assert!(should_run_in_environment(&directives, Some("prod")));
assert!(should_run_in_environment(&directives, Some("PROD")));
assert!(should_run_in_environment(&directives, Some("Prod")));
assert!(!should_run_in_environment(&directives, Some("dev")));
}
#[test]
fn test_should_run_in_environment_no_env_configured() {
let directives = MigrationDirectives {
env: vec!["prod".to_string()],
..Default::default()
};
assert!(should_run_in_environment(&directives, None));
}
}