toasty_cli/migrate/config.rs
1use crate::Flavor;
2
3use serde::{Deserialize, Serialize};
4use std::path::PathBuf;
5
6/// Configuration for migration operations.
7///
8/// Controls where migration files, snapshot files, and the history file are
9/// stored, how migration file names are prefixed (sequential numbers or
10/// timestamps), and which database flavor migrations target by default.
11///
12/// All paths are relative to the target package's directory.
13///
14/// # Examples
15///
16/// ```
17/// use toasty_cli::{MigrationConfig, MigrationPrefixStyle};
18///
19/// let config = MigrationConfig::new()
20/// .path("my_app/db")
21/// .prefix_style(MigrationPrefixStyle::Timestamp);
22///
23/// assert_eq!(
24/// config.get_migrations_dir(),
25/// std::path::PathBuf::from("my_app/db/migrations"),
26/// );
27/// assert_eq!(
28/// config.get_snapshots_dir(),
29/// std::path::PathBuf::from("my_app/db/snapshots"),
30/// );
31/// assert_eq!(
32/// config.get_history_file_path(),
33/// std::path::PathBuf::from("my_app/db/history.toml"),
34/// );
35/// ```
36#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
37pub struct MigrationConfig {
38 /// Path to the migrations folder
39 pub path: PathBuf,
40
41 /// Style of migration file prefixes
42 pub prefix_style: MigrationPrefixStyle,
43
44 /// Database flavor migrations target when `--flavor` is not passed.
45 #[serde(default, skip_serializing_if = "Option::is_none")]
46 pub flavor: Option<Flavor>,
47
48 /// Prefix applied to every table name when the schema is extracted. Must
49 /// match `Db::builder().table_name_prefix(..)`, which the extraction
50 /// constructor runs too early to observe.
51 #[serde(default, skip_serializing_if = "Option::is_none")]
52 pub table_name_prefix: Option<String>,
53}
54
55/// Controls the prefix format used when naming generated migration files.
56///
57/// The prefix appears at the start of the migration file name and determines
58/// the ordering of migration files on disk.
59///
60/// # Examples
61///
62/// ```
63/// use toasty_cli::MigrationPrefixStyle;
64///
65/// // Default is sequential
66/// let style = MigrationPrefixStyle::Sequential;
67/// assert_eq!(style, MigrationPrefixStyle::Sequential);
68/// ```
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
70pub enum MigrationPrefixStyle {
71 /// Sequential numbering (e.g., 0001_, 0002_, 0003_)
72 Sequential,
73
74 /// Timestamp-based (e.g., 20240112_153045_)
75 Timestamp,
76}
77
78impl Default for MigrationConfig {
79 fn default() -> Self {
80 Self {
81 path: PathBuf::from("toasty"),
82 prefix_style: MigrationPrefixStyle::Sequential,
83 flavor: None,
84 table_name_prefix: None,
85 }
86 }
87}
88
89impl MigrationConfig {
90 /// Create a new MigrationConfig with default values
91 pub fn new() -> Self {
92 Self::default()
93 }
94
95 /// Set the migrations path
96 pub fn path(mut self, path: impl Into<PathBuf>) -> Self {
97 self.path = path.into();
98 self
99 }
100
101 /// Set the migration prefix style
102 pub fn prefix_style(mut self, style: MigrationPrefixStyle) -> Self {
103 self.prefix_style = style;
104 self
105 }
106
107 /// Set the default database flavor
108 pub fn flavor(mut self, flavor: Flavor) -> Self {
109 self.flavor = Some(flavor);
110 self
111 }
112
113 /// Set the table name prefix the extracted schema is lowered with
114 pub fn table_name_prefix(mut self, prefix: impl Into<String>) -> Self {
115 self.table_name_prefix = Some(prefix.into());
116 self
117 }
118
119 /// Returns the directory of the migration files derived from `path`.
120 pub fn get_migrations_dir(&self) -> PathBuf {
121 self.path.join("migrations")
122 }
123
124 /// Returns the directory of the snapshot files derived from `path`.
125 pub fn get_snapshots_dir(&self) -> PathBuf {
126 self.path.join("snapshots")
127 }
128
129 /// Get the path to the history file
130 pub fn get_history_file_path(&self) -> PathBuf {
131 self.path.join("history.toml")
132 }
133}