Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changepacks/changepack_log_DmywO0Aa5wafZc2erMz7C.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"changes":{"crates/vespertide/Cargo.toml":"Patch","crates/vespertide-planner/Cargo.toml":"Patch","crates/vespertide-core/Cargo.toml":"Patch","crates/vespertide-config/Cargo.toml":"Patch","crates/vespertide-query/Cargo.toml":"Patch","crates/vespertide-cli/Cargo.toml":"Patch","crates/vespertide-macro/Cargo.toml":"Patch"},"note":"Add migration validation","date":"2025-12-10T16:31:52.974551200Z"}
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion crates/vespertide-cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::PathBuf;
use anyhow::{Context, Result};
use vespertide_config::{FileFormat, VespertideConfig};
use vespertide_core::{MigrationPlan, TableDef};
use vespertide_planner::validate_schema;
use vespertide_planner::{validate_migration_plan, validate_schema};

/// Load vespertide.json config from current directory.
pub fn load_config() -> Result<VespertideConfig> {
Expand Down Expand Up @@ -86,6 +86,10 @@ pub fn load_migrations(config: &VespertideConfig) -> Result<Vec<MigrationPlan>>
.with_context(|| format!("parse migration: {}", path.display()))?
};

// Validate the migration plan
validate_migration_plan(&plan)
.with_context(|| format!("validate migration: {}", path.display()))?;

plans.push(plan);
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/vespertide-planner/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ pub enum PlannerError {
ConstraintColumnNotFound(String, String, String),
#[error("constraint has empty column list: {0}.{1}")]
EmptyConstraintColumns(String, String),
#[error("AddColumn requires fill_with when column is NOT NULL without default: {0}.{1}")]
MissingFillWith(String, String),
}
2 changes: 1 addition & 1 deletion crates/vespertide-planner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ pub use diff::diff_schemas;
pub use error::PlannerError;
pub use plan::plan_next_migration;
pub use schema::schema_from_plans;
pub use validate::validate_schema;
pub use validate::{validate_migration_plan, validate_schema};
128 changes: 127 additions & 1 deletion crates/vespertide-planner/src/validate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;

use vespertide_core::{IndexDef, TableConstraint, TableDef};
use vespertide_core::{IndexDef, MigrationAction, MigrationPlan, TableConstraint, TableDef};

use crate::error::PlannerError;

Expand Down Expand Up @@ -514,4 +514,130 @@ mod tests {
}
}
}

#[test]
fn validate_migration_plan_missing_fill_with() {
use vespertide_core::{ColumnDef, ColumnType, MigrationAction, MigrationPlan};

let plan = MigrationPlan {
comment: None,
created_at: None,
version: 1,
actions: vec![MigrationAction::AddColumn {
table: "users".into(),
column: ColumnDef {
name: "email".into(),
r#type: ColumnType::Text,
nullable: false,
default: None,
},
fill_with: None,
}],
};

let result = validate_migration_plan(&plan);
assert!(result.is_err());
match result.unwrap_err() {
PlannerError::MissingFillWith(table, column) => {
assert_eq!(table, "users");
assert_eq!(column, "email");
}
_ => panic!("expected MissingFillWith error"),
}
}

#[test]
fn validate_migration_plan_with_fill_with() {
use vespertide_core::{ColumnDef, ColumnType, MigrationAction, MigrationPlan};

let plan = MigrationPlan {
comment: None,
created_at: None,
version: 1,
actions: vec![MigrationAction::AddColumn {
table: "users".into(),
column: ColumnDef {
name: "email".into(),
r#type: ColumnType::Text,
nullable: false,
default: None,
},
fill_with: Some("[email protected]".into()),
}],
};

let result = validate_migration_plan(&plan);
assert!(result.is_ok());
}

#[test]
fn validate_migration_plan_nullable_column() {
use vespertide_core::{ColumnDef, ColumnType, MigrationAction, MigrationPlan};

let plan = MigrationPlan {
comment: None,
created_at: None,
version: 1,
actions: vec![MigrationAction::AddColumn {
table: "users".into(),
column: ColumnDef {
name: "email".into(),
r#type: ColumnType::Text,
nullable: true,
default: None,
},
fill_with: None,
}],
};

let result = validate_migration_plan(&plan);
assert!(result.is_ok());
}

#[test]
fn validate_migration_plan_with_default() {
use vespertide_core::{ColumnDef, ColumnType, MigrationAction, MigrationPlan};

let plan = MigrationPlan {
comment: None,
created_at: None,
version: 1,
actions: vec![MigrationAction::AddColumn {
table: "users".into(),
column: ColumnDef {
name: "email".into(),
r#type: ColumnType::Text,
nullable: false,
default: Some("[email protected]".into()),
},
fill_with: None,
}],
};

let result = validate_migration_plan(&plan);
assert!(result.is_ok());
}
}

/// Validate a migration plan for correctness.
/// Checks for:
/// - AddColumn actions with NOT NULL columns without default must have fill_with
pub fn validate_migration_plan(plan: &MigrationPlan) -> Result<(), PlannerError> {
for action in &plan.actions {
if let MigrationAction::AddColumn {
table,
column,
fill_with,
} = action
{
// If column is NOT NULL and has no default, fill_with is required
if !column.nullable && column.default.is_none() && fill_with.is_none() {
return Err(PlannerError::MissingFillWith(
table.clone(),
column.name.clone(),
));
}
}
}
Ok(())
}