toasty_cli/migration/config.rs
1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3
4/// Configuration for migration operations
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct MigrationConfig {
7 /// Path to the migrations folder
8 pub path: PathBuf,
9
10 /// Style of migration file prefixes
11 pub prefix_style: MigrationPrefixStyle,
12
13 /// Whether the history file should store and verify checksums of the migration files so that
14 /// they may not be changed.
15 pub checksums: bool,
16
17 /// Whether to add statement breakpoint comments to generated SQL migration files.
18 /// These comments mark boundaries where SQL statements should be split for execution.
19 /// This is needed because different databases have different batching capabilities:
20 /// some (like PostgreSQL) can execute multiple statements in one batch, while others
21 /// require each statement to be executed separately.
22 pub statement_breakpoints: bool,
23}
24
25/// Style for migration file name prefixes
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
27pub enum MigrationPrefixStyle {
28 /// Sequential numbering (e.g., 0001_, 0002_, 0003_)
29 Sequential,
30
31 /// Timestamp-based (e.g., 20240112_153045_)
32 Timestamp,
33}
34
35impl Default for MigrationConfig {
36 fn default() -> Self {
37 Self {
38 path: PathBuf::from("toasty"),
39 prefix_style: MigrationPrefixStyle::Sequential,
40 checksums: false,
41 statement_breakpoints: true,
42 }
43 }
44}
45
46impl MigrationConfig {
47 /// Create a new MigrationConfig with default values
48 pub fn new() -> Self {
49 Self::default()
50 }
51
52 /// Set the migrations path
53 pub fn path(mut self, path: impl Into<PathBuf>) -> Self {
54 self.path = path.into();
55 self
56 }
57
58 /// Set the migration prefix style
59 pub fn prefix_style(mut self, style: MigrationPrefixStyle) -> Self {
60 self.prefix_style = style;
61 self
62 }
63
64 /// Returns the directory of the migration files derived from `path`.
65 pub fn get_migrations_dir(&self) -> PathBuf {
66 self.path.join("migrations")
67 }
68
69 /// Returns the directory of the snapshot files derived from `path`.
70 pub fn get_snapshots_dir(&self) -> PathBuf {
71 self.path.join("snapshots")
72 }
73
74 /// Get the path to the history file
75 pub fn get_history_file_path(&self) -> PathBuf {
76 self.path.join("history.toml")
77 }
78}