Skip to content

Commit 1ca10cd

Browse files
authored
Merge pull request #78 from dev-five-git/add-prefix
Add prefix
2 parents 56ab403 + 44c8c52 commit 1ca10cd

13 files changed

Lines changed: 1045 additions & 213 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"changes":{"crates/vespertide-config/Cargo.toml":"Patch","crates/vespertide-query/Cargo.toml":"Patch","crates/vespertide-naming/Cargo.toml":"Patch","crates/vespertide-cli/Cargo.toml":"Patch","crates/vespertide/Cargo.toml":"Patch","crates/vespertide-exporter/Cargo.toml":"Patch","crates/vespertide-macro/Cargo.toml":"Patch","crates/vespertide-core/Cargo.toml":"Patch","crates/vespertide-loader/Cargo.toml":"Patch","crates/vespertide-planner/Cargo.toml":"Patch"},"note":"Add prefix option","date":"2026-01-21T09:42:55.945270800Z"}

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
echo 'merge_derives = true' >> .rustfmt.toml
5050
echo 'use_small_heuristics = "Default"' >> .rustfmt.toml
5151
cargo fmt
52-
cargo tarpaulin --out Lcov Stdout --workspace --exclude app
52+
cargo tarpaulin --engine llvm --out Lcov Stdout --workspace --exclude app
5353
- name: Upload to codecov.io
5454
uses: codecov/codecov-action@v5
5555
with:

crates/vespertide-cli/src/commands/log.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,22 @@ pub fn cmd_log(backend: DatabaseBackend) -> Result<()> {
1515
return Ok(());
1616
}
1717

18+
// Apply prefix to all migration plans
19+
let prefix = config.prefix();
20+
let prefixed_plans: Vec<_> = plans.into_iter().map(|p| p.with_prefix(prefix)).collect();
21+
1822
println!(
1923
"{} {} {}",
2024
"Migrations".bright_cyan().bold(),
2125
"(oldest -> newest):".bright_white(),
22-
plans.len().to_string().bright_yellow().bold()
26+
prefixed_plans.len().to_string().bright_yellow().bold()
2327
);
2428
println!();
2529

2630
// Build baseline schema incrementally as we iterate through migrations
2731
let mut baseline_schema = Vec::new();
2832

29-
for plan in &plans {
33+
for plan in &prefixed_plans {
3034
println!(
3135
"{} {}",
3236
"Version:".bright_cyan().bold(),

crates/vespertide-cli/src/commands/sql.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,24 @@ pub fn cmd_sql(backend: DatabaseBackend) -> Result<()> {
1010
let current_models = load_models(&config)?;
1111
let applied_plans = load_migrations(&config)?;
1212

13-
// Reconstruct the baseline schema from applied migrations
14-
let baseline_schema = schema_from_plans(&applied_plans)
13+
// Reconstruct the baseline schema from applied migrations (with prefix applied)
14+
let prefix = config.prefix();
15+
let prefixed_plans: Vec<_> = applied_plans
16+
.into_iter()
17+
.map(|p| p.with_prefix(prefix))
18+
.collect();
19+
let baseline_schema = schema_from_plans(&prefixed_plans)
1520
.map_err(|e| anyhow::anyhow!("failed to reconstruct schema: {}", e))?;
1621

1722
// Plan next migration using the pre-computed baseline
18-
let plan = plan_next_migration_with_baseline(&current_models, &applied_plans, &baseline_schema)
19-
.map_err(|e| anyhow::anyhow!("planning error: {}", e))?;
23+
let plan =
24+
plan_next_migration_with_baseline(&current_models, &prefixed_plans, &baseline_schema)
25+
.map_err(|e| anyhow::anyhow!("planning error: {}", e))?;
26+
27+
// Apply prefix to the new plan for SQL generation
28+
let prefixed_plan = plan.with_prefix(prefix);
2029

21-
emit_sql(&plan, backend, &baseline_schema)
30+
emit_sql(&prefixed_plan, backend, &baseline_schema)
2231
}
2332

2433
fn emit_sql(

crates/vespertide-config/src/config.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ pub struct VespertideConfig {
8383
/// SeaORM-specific export configuration.
8484
#[serde(default)]
8585
pub seaorm: SeaOrmConfig,
86+
/// Prefix to add to all table names (including migration version table).
87+
/// Default: "" (no prefix)
88+
#[serde(default)]
89+
pub prefix: String,
8690
}
8791

8892
fn default_model_export_dir() -> PathBuf {
@@ -101,6 +105,7 @@ impl Default for VespertideConfig {
101105
migration_filename_pattern: default_migration_filename_pattern(),
102106
model_export_dir: default_model_export_dir(),
103107
seaorm: SeaOrmConfig::default(),
108+
prefix: String::new(),
104109
}
105110
}
106111
}
@@ -150,6 +155,20 @@ impl VespertideConfig {
150155
pub fn seaorm(&self) -> &SeaOrmConfig {
151156
&self.seaorm
152157
}
158+
159+
/// Prefix to add to all table names.
160+
pub fn prefix(&self) -> &str {
161+
&self.prefix
162+
}
163+
164+
/// Apply prefix to a table name.
165+
pub fn apply_prefix(&self, table_name: &str) -> String {
166+
if self.prefix.is_empty() {
167+
table_name.to_string()
168+
} else {
169+
format!("{}{}", self.prefix, table_name)
170+
}
171+
}
153172
}
154173

155174
#[cfg(test)]
@@ -173,5 +192,26 @@ mod tests {
173192
vec!["vespera::Schema".to_string()]
174193
);
175194
assert!(config.seaorm.extra_model_derives.is_empty());
195+
assert_eq!(config.prefix, "");
196+
}
197+
198+
#[test]
199+
fn test_vespertide_config_prefix() {
200+
let config = VespertideConfig {
201+
prefix: "myapp_".to_string(),
202+
..Default::default()
203+
};
204+
205+
assert_eq!(config.prefix(), "myapp_");
206+
assert_eq!(config.apply_prefix("users"), "myapp_users");
207+
assert_eq!(config.apply_prefix("posts"), "myapp_posts");
208+
}
209+
210+
#[test]
211+
fn test_vespertide_config_empty_prefix() {
212+
let config = VespertideConfig::default();
213+
214+
assert_eq!(config.prefix(), "");
215+
assert_eq!(config.apply_prefix("users"), "users");
176216
}
177217
}

0 commit comments

Comments
 (0)