-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseline.rs
More file actions
94 lines (79 loc) · 2.45 KB
/
baseline.rs
File metadata and controls
94 lines (79 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use anyhow::Result;
use chrono::Utc;
use std::path::Path;
use crate::baseline::{delete_baselined_migrations, validate_baseline};
use crate::loader::discover_migrations;
use crate::state::{append_baseline, read_history, Baseline};
/// Create a baseline at the specified version
pub fn run(
project_root: &Path,
migrations_dir: &Path,
version: &str,
summary: Option<&str>,
dry_run: bool,
keep: bool,
) -> Result<()> {
let migrations_path = if migrations_dir.is_absolute() {
migrations_dir.to_path_buf()
} else {
project_root.join(migrations_dir)
};
if !migrations_path.exists() {
println!(
"No migrations directory found at: {}",
migrations_path.display()
);
return Ok(());
}
let available = discover_migrations(&migrations_path)?;
let state = read_history(&migrations_path)?;
// Validate the baseline
validate_baseline(version, &available, &state.applied, state.baseline.as_ref())?;
// Find migrations that would be deleted
let to_delete: Vec<_> = available
.iter()
.filter(|m| m.version.as_str() <= version)
.collect();
if dry_run {
println!("Dry run - no changes will be made");
println!();
}
println!(
"Creating baseline at version '{}'{}",
version,
if dry_run { " (dry run)" } else { "" }
);
println!();
if !to_delete.is_empty() && !keep {
println!(
"{} migration file(s) to delete:",
if dry_run { "Would delete" } else { "Deleting" }
);
for migration in &to_delete {
println!(" - {}", migration.id);
}
println!();
} else if keep {
println!("Keeping migration files (--keep flag)");
println!();
}
if dry_run {
return Ok(());
}
// Create the baseline
let baseline = Baseline {
version: version.to_string(),
created: Utc::now(),
summary: summary.map(|s| s.to_string()),
};
append_baseline(&migrations_path, &baseline)?;
println!("Added baseline to history file");
// Delete old migration files unless --keep was specified
if !keep && !to_delete.is_empty() {
let deleted = delete_baselined_migrations(version, &available)?;
println!("Deleted {} migration file(s)", deleted.len());
}
println!();
println!("Baseline created successfully at version '{}'", version);
Ok(())
}