Skip to content
Merged

Add #107

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_kx6-sb-ClLUo-70CrBAMA.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"changes":{"crates/vespertide-naming/Cargo.toml":"Patch","crates/vespertide-planner/Cargo.toml":"Patch","crates/vespertide-query/Cargo.toml":"Patch","crates/vespertide-config/Cargo.toml":"Patch","crates/vespertide-core/Cargo.toml":"Patch","crates/vespertide-exporter/Cargo.toml":"Patch","crates/vespertide-cli/Cargo.toml":"Patch","crates/vespertide/Cargo.toml":"Patch","crates/vespertide-loader/Cargo.toml":"Patch","crates/vespertide-macro/Cargo.toml":"Patch"},"note":"Add migration id","date":"2026-02-10T15:23:54.389369700Z"}
22 changes: 12 additions & 10 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/vespertide-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ clap = { version = "4", features = ["derive"] }
chrono = { version = "0.4", default-features = false, features = ["clock", "serde"] }
colored = "3"
dialoguer = "0.12"
uuid = { version = "1", features = ["v4"] }
serde_json = "1"
serde_yaml = "0.9"
schemars = "1.2"
Expand Down
2 changes: 2 additions & 0 deletions crates/vespertide-cli/src/commands/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ mod tests {
fn write_migration(cfg: &VespertideConfig) {
fs::create_dir_all(cfg.migrations_dir()).unwrap();
let plan = MigrationPlan {
id: String::new(),
comment: Some("init".into()),
created_at: Some("2024-01-01T00:00:00Z".into()),
version: 1,
Expand Down Expand Up @@ -248,6 +249,7 @@ mod tests {

// Create a migration with ModifyColumnType for SQLite, which generates multiple SQL statements
let plan = MigrationPlan {
id: String::new(),
comment: Some("modify column type".into()),
created_at: Some("2024-01-01T00:00:00Z".into()),
version: 1,
Expand Down
14 changes: 14 additions & 0 deletions crates/vespertide-cli/src/commands/revision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ pub async fn cmd_revision(message: String, fill_with_args: Vec<String>) -> Resul
prompt_enum_value,
)?;

plan.id = uuid::Uuid::new_v4().to_string();
plan.comment = Some(message);
if plan.created_at.is_none() {
// Record creation time in RFC3339 (UTC).
Expand Down Expand Up @@ -521,6 +522,7 @@ mod tests {
fn check_non_nullable_fk_add_column_fails() {
use vespertide_core::{ColumnDef, ColumnType, SimpleColumnType};
let plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 2,
Expand Down Expand Up @@ -567,6 +569,7 @@ mod tests {
fn check_nullable_fk_add_column_ok() {
use vespertide_core::{ColumnDef, ColumnType, SimpleColumnType};
let plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 2,
Expand Down Expand Up @@ -607,6 +610,7 @@ mod tests {
// Regular non-nullable column without FK should NOT be blocked
use vespertide_core::{ColumnDef, ColumnType, SimpleColumnType};
let plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 2,
Expand Down Expand Up @@ -671,6 +675,7 @@ mod tests {
use vespertide_core::MigrationPlan;

let mut plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 1,
Expand Down Expand Up @@ -712,6 +717,7 @@ mod tests {
use vespertide_core::MigrationPlan;

let mut plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 1,
Expand Down Expand Up @@ -744,6 +750,7 @@ mod tests {
use vespertide_core::MigrationPlan;

let mut plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 1,
Expand Down Expand Up @@ -786,6 +793,7 @@ mod tests {
use vespertide_core::MigrationPlan;

let mut plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 1,
Expand Down Expand Up @@ -828,6 +836,7 @@ mod tests {
use vespertide_core::MigrationPlan;

let mut plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 1,
Expand Down Expand Up @@ -888,6 +897,7 @@ mod tests {
use vespertide_core::MigrationPlan;

let mut plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 1,
Expand Down Expand Up @@ -1149,6 +1159,7 @@ mod tests {
use vespertide_core::MigrationPlan;

let mut plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 1,
Expand Down Expand Up @@ -1206,6 +1217,7 @@ mod tests {

// Plan with no missing fill_with values (nullable column)
let mut plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 1,
Expand Down Expand Up @@ -1249,6 +1261,7 @@ mod tests {
use vespertide_core::MigrationPlan;

let mut plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 1,
Expand Down Expand Up @@ -1299,6 +1312,7 @@ mod tests {
use vespertide_core::MigrationPlan;

let mut plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 1,
Expand Down
8 changes: 8 additions & 0 deletions crates/vespertide-cli/src/commands/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ mod tests {
write_model("users");

let plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 1,
Expand Down Expand Up @@ -267,6 +268,7 @@ mod tests {
write_model("users");

let plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 1,
Expand Down Expand Up @@ -307,6 +309,7 @@ mod tests {
write_model("users");

let plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 1,
Expand Down Expand Up @@ -341,6 +344,7 @@ mod tests {
#[serial]
async fn emit_sql_prints_created_at_and_comment_postgres() {
let plan = MigrationPlan {
id: String::new(),
comment: Some("with comment".into()),
created_at: Some("2024-01-02T00:00:00Z".into()),
version: 1,
Expand All @@ -357,6 +361,7 @@ mod tests {
#[serial]
async fn emit_sql_prints_created_at_and_comment_mysql() {
let plan = MigrationPlan {
id: String::new(),
comment: Some("with comment".into()),
created_at: Some("2024-01-02T00:00:00Z".into()),
version: 1,
Expand All @@ -373,6 +378,7 @@ mod tests {
#[serial]
async fn emit_sql_prints_created_at_and_comment_sqlite() {
let plan = MigrationPlan {
id: String::new(),
comment: Some("with comment".into()),
created_at: Some("2024-01-02T00:00:00Z".into()),
version: 1,
Expand All @@ -389,6 +395,7 @@ mod tests {
#[serial]
async fn emit_sql_multiple_queries() {
let plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 1,
Expand Down Expand Up @@ -434,6 +441,7 @@ mod tests {

// Create a migration that adds a NOT NULL column in SQLite, which generates multiple queries
let plan = MigrationPlan {
id: String::new(),
comment: None,
created_at: None,
version: 1,
Expand Down
1 change: 1 addition & 0 deletions crates/vespertide-cli/src/commands/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ mod tests {
fn write_migration(cfg: &VespertideConfig) {
fs::create_dir_all(cfg.migrations_dir()).unwrap();
let plan = MigrationPlan {
id: String::new(),
comment: Some("init".into()),
created_at: Some("2024-01-01T00:00:00Z".into()),
version: 1,
Expand Down
2 changes: 2 additions & 0 deletions crates/vespertide-cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,14 @@ mod tests {

fs::create_dir_all("migrations").unwrap();
let plan1 = MigrationPlan {
id: String::new(),
comment: Some("first".into()),
created_at: None,
version: 2,
actions: vec![],
};
let plan0 = MigrationPlan {
id: String::new(),
comment: Some("zero".into()),
created_at: None,
version: 1,
Expand Down
5 changes: 5 additions & 0 deletions crates/vespertide-core/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct MigrationPlan {
/// Unique identifier for this migration (UUID format).
/// Defaults to empty string for backward compatibility with old migration files.
#[serde(default)]
pub id: String,
pub comment: Option<String>,
#[serde(default)]
pub created_at: Option<String>,
Expand Down Expand Up @@ -810,6 +814,7 @@ mod tests {
#[test]
fn test_migration_plan_with_prefix() {
let plan = MigrationPlan {
id: String::new(),
comment: Some("test".into()),
created_at: None,
version: 1,
Expand Down
8 changes: 8 additions & 0 deletions crates/vespertide-core/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ pub enum MigrationError {
NotImplemented,
#[error("database error: {0}")]
DatabaseError(String),
#[error(
"migration id mismatch for version {version}: expected '{expected}', found '{found}' in database"
)]
IdMismatch {
version: u32,
expected: String,
found: String,
},
}
Loading