Skip to content

Commit c0ea9c9

Browse files
committed
Implemented auto migration support for adding new columns at the end of a table.
1 parent 34201c6 commit c0ea9c9

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

crates/sats/src/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ impl HasLayout for ProductTypeLayoutView<'_> {
310310
impl ProductTypeLayoutView<'_> {
311311
/// Can `self` be changed compatibly to `new`?
312312
fn is_compatible_with(self, new: Self) -> bool {
313-
self.elements.len() == new.elements.len()
313+
self.elements.len() <= new.elements.len()
314314
&& self
315315
.elements
316316
.iter()

crates/schema/src/auto_migrate.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,17 @@ fn auto_migrate_table<'def>(plan: &mut AutoMigratePlan<'def>, old: &'def TableDe
386386
})
387387
.map(|col_diff| -> Result<_> {
388388
match col_diff {
389-
Diff::Add { new } => Err(AutoMigrateError::AddColumn {
390-
table: new.table_name.clone(),
391-
column: new.name.clone(),
389+
Diff::Add { new } => {
390+
if new.col_id >= old.columns.len().into() {
391+
Ok(Any(true)) // column type is inherently new as we're creating a new one
392+
} else {
393+
Err(AutoMigrateError::AddColumn {
394+
table: new.table_name.clone(),
395+
column: new.name.clone(),
396+
}
397+
.into())
398+
}
392399
}
393-
.into()),
394400
Diff::Remove { old } => Err(AutoMigrateError::RemoveColumn {
395401
table: old.table_name.clone(),
396402
column: old.name.clone(),
@@ -423,9 +429,9 @@ fn auto_migrate_table<'def>(plan: &mut AutoMigratePlan<'def>, old: &'def TableDe
423429
})
424430
.collect_all_errors::<Any>();
425431

426-
let ((), Any(row_type_changed)) = (type_ok, columns_ok).combine_errors()?;
432+
let ((), Any(column_type_changed)) = (type_ok, columns_ok).combine_errors()?;
427433

428-
if row_type_changed {
434+
if column_type_changed {
429435
plan.steps.push(AutoMigrateStep::ChangeColumns(key));
430436
}
431437

0 commit comments

Comments
 (0)