Commit e8b34f7
feat: Allow dropping empty tables during auto-migration (#4593)
## Summary
- Removing a table from the schema previously always triggered
`AutoMigrateError::RemoveTable`, forcing `--delete-data` which **nukes
the entire database**
- Now, empty tables can be dropped seamlessly during `spacetime publish`
- Non-empty tables fail with an actionable error guiding the user to
clear rows first
## Changes
- **`auto_migrate.rs`**: Replace hard `RemoveTable` error with
`AutoMigrateStep::RemoveTable`. Compute `removed_tables` set (same
pattern as `new_tables`) and pass to
`auto_migrate_indexes`/`auto_migrate_sequences`/`auto_migrate_constraints`
to skip sub-object diffs for removed tables (cascade handled by
`drop_table`)
- **`update.rs`**: Execute `RemoveTable` step — O(1) emptiness check via
`table_row_count_mut`, then `drop_table`. Fails with clear message if
table has data
- **`formatter.rs`** / **`termcolor_formatter.rs`**: Add
`format_remove_table` to `MigrationFormatter` trait + implementation
- **`publish.rs` (smoketests)**: Update existing test — removing empty
table now succeeds without flags
## Safety
- **Transaction safety**: Emptiness check and drop run in the same
`MutTx` — no window for concurrent inserts
- **Cascade**: `drop_table` already handles removing all indexes,
constraints, and sequences for the table
- **Sub-object filtering**: Indexes/constraints/sequences belonging to
removed tables are filtered from their respective diffs, preventing
orphan `RemoveIndex`/`RemoveConstraint`/`RemoveSequence` steps
- **Rollback**: If the emptiness check fails, the entire migration
aborts before any changes are applied
## Example error output (non-empty table)
```
Cannot remove table `MyTable`: table contains data. Clear the table's rows (e.g. via a reducer) before removing it from your schema.
```
## Test plan
- [x] `cargo check -p spacetimedb-schema -p spacetimedb-core` passes
- [x] All 14 `auto_migrate` schema tests pass (2 new:
`remove_table_produces_step`,
`remove_table_does_not_produce_orphan_sub_object_steps`)
- [x] All 3 `update` execution tests pass (2 new:
`remove_empty_table_succeeds`, `remove_nonempty_table_fails`)
- [x] Updated existing `auto_migration_errors` test (no longer expects
`RemoveTable` error)
- [x] Updated smoke test: `cli_can_publish_remove_empty_table` expects
success
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: clockwork-labs-bot <clockwork-labs-bot@users.noreply.github.com>1 parent fbec276 commit e8b34f7
File tree
4 files changed
+237
-42
lines changed- crates
- core/src/db
- schema/src
- auto_migrate
4 files changed
+237
-42
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
144 | 157 | | |
145 | 158 | | |
146 | 159 | | |
| |||
405 | 418 | | |
406 | 419 | | |
407 | 420 | | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
408 | 509 | | |
0 commit comments