Skip to content

Commit 39e13a1

Browse files
committed
Support enum
1 parent 2665405 commit 39e13a1

4 files changed

Lines changed: 47 additions & 40 deletions

File tree

crates/vespertide-planner/src/diff.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,8 +1304,18 @@ mod tests {
13041304
use crate::diff::diff_schemas;
13051305

13061306
let from_schema = vec![
1307-
table("users", vec![col("id", ColumnType::Simple(SimpleColumnType::Integer))], vec![], vec![]),
1308-
table("posts", vec![col("id", ColumnType::Simple(SimpleColumnType::Integer))], vec![], vec![]),
1307+
table(
1308+
"users",
1309+
vec![col("id", ColumnType::Simple(SimpleColumnType::Integer))],
1310+
vec![],
1311+
vec![],
1312+
),
1313+
table(
1314+
"posts",
1315+
vec![col("id", ColumnType::Simple(SimpleColumnType::Integer))],
1316+
vec![],
1317+
vec![],
1318+
),
13091319
];
13101320

13111321
let to_schema = vec![
@@ -1324,8 +1334,16 @@ mod tests {
13241334
let plan = diff_schemas(&from_schema, &to_schema).unwrap();
13251335

13261336
// Should have: AddColumn (for users.name) and DeleteTable (for posts)
1327-
assert!(plan.actions.iter().any(|a| matches!(a, MigrationAction::AddColumn { .. })));
1328-
assert!(plan.actions.iter().any(|a| matches!(a, MigrationAction::DeleteTable { .. })));
1337+
assert!(
1338+
plan.actions
1339+
.iter()
1340+
.any(|a| matches!(a, MigrationAction::AddColumn { .. }))
1341+
);
1342+
assert!(
1343+
plan.actions
1344+
.iter()
1345+
.any(|a| matches!(a, MigrationAction::DeleteTable { .. }))
1346+
);
13291347

13301348
// The else branches in sort_delete_actions should handle AddColumn gracefully
13311349
// (returning empty string for table name, which sorts it to position 0)

crates/vespertide-query/src/sql/add_column.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,8 @@ mod tests {
498498
#[case::add_column_with_enum_type_mysql(DatabaseBackend::MySql)]
499499
#[case::add_column_with_enum_type_sqlite(DatabaseBackend::Sqlite)]
500500
fn test_add_column_with_enum_type(#[case] backend: DatabaseBackend) {
501-
use vespertide_core::ComplexColumnType;
502501
use insta::{assert_snapshot, with_settings};
502+
use vespertide_core::ComplexColumnType;
503503

504504
// Test that adding an enum column creates the enum type first (PostgreSQL only)
505505
let column = ColumnDef {
@@ -532,13 +532,7 @@ mod tests {
532532
constraints: vec![],
533533
indexes: vec![],
534534
}];
535-
let result = build_add_column(
536-
&backend,
537-
"users",
538-
&column,
539-
None,
540-
&current_schema,
541-
);
535+
let result = build_add_column(&backend, "users", &column, None, &current_schema);
542536
assert!(result.is_ok());
543537
let queries = result.unwrap();
544538
let sql = queries

crates/vespertide-query/src/sql/create_table.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,7 @@ mod tests {
404404
columns: vec!["id".into()],
405405
}];
406406

407-
let result =
408-
build_create_table(&backend, "users", &columns, &constraints);
407+
let result = build_create_table(&backend, "users", &columns, &constraints);
409408
assert!(result.is_ok());
410409
let queries = result.unwrap();
411410
let sql = queries

crates/vespertide-query/src/sql/modify_column_type.rs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,14 @@ pub fn build_modify_column_type(
185185
// Determine if we need to create a new enum type
186186
// - If old type was a different enum, we need to create the new one
187187
// - If old type was not an enum, we need to create the enum type
188-
let should_create = if let Some(old_col_type) = old_type {
189-
match old_col_type {
190-
ColumnType::Complex(ComplexColumnType::Enum { name: old_name, .. }) => {
191-
old_name != new_name
192-
}
193-
_ => true, // Old type wasn't an enum, need to create enum type
194-
}
188+
let should_create = if let Some(ColumnType::Complex(ComplexColumnType::Enum {
189+
name: old_name,
190+
..
191+
})) = old_type
192+
{
193+
old_name != new_name
195194
} else {
196-
// old_type is None - this means the column wasn't found in current schema
197-
// This should not happen as we're modifying an existing column
198-
// But we'll create the type to be safe
195+
// Either old_type is None or it wasn't an enum - need to create enum type
199196
true
200197
};
201198

@@ -339,10 +336,12 @@ mod tests {
339336
&[],
340337
);
341338
assert!(result.is_err());
342-
assert!(result
343-
.unwrap_err()
344-
.to_string()
345-
.contains("Table 'nonexistent_table' not found"));
339+
assert!(
340+
result
341+
.unwrap_err()
342+
.to_string()
343+
.contains("Table 'nonexistent_table' not found")
344+
);
346345
}
347346

348347
#[test]
@@ -371,10 +370,12 @@ mod tests {
371370
&current_schema,
372371
);
373372
assert!(result.is_err());
374-
assert!(result
375-
.unwrap_err()
376-
.to_string()
377-
.contains("Column 'nonexistent_column' not found"));
373+
assert!(
374+
result
375+
.unwrap_err()
376+
.to_string()
377+
.contains("Column 'nonexistent_column' not found")
378+
);
378379
}
379380

380381
#[rstest]
@@ -720,14 +721,9 @@ mod tests {
720721
indexes: vec![],
721722
}];
722723

723-
let result = build_modify_column_type(
724-
&backend,
725-
"users",
726-
"status",
727-
&new_type,
728-
&current_schema,
729-
)
730-
.unwrap();
724+
let result =
725+
build_modify_column_type(&backend, "users", "status", &new_type, &current_schema)
726+
.unwrap();
731727

732728
let sql = result
733729
.iter()

0 commit comments

Comments
 (0)