mirror of
https://github.com/clockworklabs/SpacetimeDB.git
synced 2026-05-09 00:59:46 -04:00
e8b34f75ca
## 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>
⚠️ Internal Crate ⚠️
This crate is intended for internal use only. It is not stable and may change without notice.