waypoint_core/commands/
migrate.rs1use serde::Serialize;
12
13use crate::directive::MigrationDirectives;
14use crate::error::WaypointError;
15
16#[cfg(feature = "mysql")]
22pub use crate::engines::mysql::migrate::{
23 execute as execute_mysql, execute_with_options as execute_mysql_with_options,
24};
25#[cfg(feature = "postgres")]
26pub use crate::engines::postgres::migrate::{execute, execute_with_options};
27
28#[derive(Debug, Serialize)]
32pub struct MigrateReport {
33 pub migrations_applied: usize,
35 pub total_time_ms: i32,
37 pub details: Vec<MigrateDetail>,
39 pub hooks_executed: usize,
41 pub hooks_time_ms: i32,
43}
44
45#[derive(Debug, Serialize)]
47pub struct MigrateDetail {
48 pub version: Option<String>,
50 pub description: String,
52 pub script: String,
54 pub execution_time_ms: i32,
56}
57
58pub(crate) enum GuardAction {
62 Continue,
64 Skip,
66 Error(WaypointError),
68}
69
70pub(crate) fn should_run_in_environment(
77 directives: &MigrationDirectives,
78 current_env: Option<&str>,
79) -> bool {
80 if directives.env.is_empty() {
81 return true;
82 }
83 let env = match current_env {
84 Some(e) => e,
85 None => return true,
86 };
87 directives.env.iter().any(|e| e.eq_ignore_ascii_case(env))
88}
89
90#[cfg(test)]
91mod tests {
92 use super::*;
93
94 #[test]
95 fn test_should_run_in_environment_no_directives() {
96 let directives = MigrationDirectives::default();
97 assert!(should_run_in_environment(&directives, Some("production")));
98 assert!(should_run_in_environment(&directives, None));
99 }
100
101 #[test]
102 fn test_should_run_in_environment_matches() {
103 let directives = MigrationDirectives {
104 env: vec!["production".to_string(), "staging".to_string()],
105 ..Default::default()
106 };
107 assert!(should_run_in_environment(&directives, Some("production")));
108 assert!(should_run_in_environment(&directives, Some("staging")));
109 assert!(!should_run_in_environment(&directives, Some("dev")));
110 }
111
112 #[test]
113 fn test_should_run_in_environment_case_insensitive() {
114 let directives = MigrationDirectives {
115 env: vec!["PROD".to_string()],
116 ..Default::default()
117 };
118 assert!(should_run_in_environment(&directives, Some("prod")));
119 assert!(should_run_in_environment(&directives, Some("PROD")));
120 assert!(should_run_in_environment(&directives, Some("Prod")));
121 assert!(!should_run_in_environment(&directives, Some("dev")));
122 }
123
124 #[test]
125 fn test_should_run_in_environment_no_env_configured() {
126 let directives = MigrationDirectives {
127 env: vec!["prod".to_string()],
128 ..Default::default()
129 };
130 assert!(should_run_in_environment(&directives, None));
131 }
132}