docs/docs/00200-core-concepts/00100-databases/00500-migrations/00200-automatic-migrations.md
When you republish a module to an existing database using spacetime publish {database-name}, SpacetimeDB attempts to automatically migrate your database schema to match the new module definition. This allows you to update your module code and redeploy without losing existing data, as long as the changes are compatible.
:::note The "schema" refers to the collection of tables, reducers, procedures, views, and the types they depend on that are declared in your module code. :::
:::warning If you are upgrading an existing 1.x database to 2.0, review 1.x to 2.0 Upgrade Notes before publishing. :::
The following changes are always allowed and will not break existing clients:
Auto Inc annotations.Unique constraints.These changes are allowed by automatic migration, but may cause runtime errors for clients that haven't been updated:
Adding new columns to the end of a table with a default value. The new column must be added at the end of the table definition and must have a default value specified. Non-updated clients will not be aware of the new column.
Changing or removing reducers. Clients attempting to call the old version of a changed reducer or a removed reducer will receive runtime errors.
Changing tables from public to private. Clients subscribed to a newly-private table will receive runtime errors.
Removing Primary Key annotations. Non-updated clients will still use the old primary key as a unique key in their local cache, which can result in non-deterministic behavior when updates are received.
Removing indexes. This is only breaking in specific situations. The main issue occurs with subscription queries involving semijoins, such as:
tables.employee.leftSemijoin(
tables.dept,
(employee, dept) => employee.deptName.eq(dept.deptName)
)
For performance reasons, SpacetimeDB will only allow this kind of subscription query if there are indexes on both join columns (Employee.DeptName and Dept.DeptName). Removing either index will invalidate this subscription query, resulting in client-side runtime errors.
The following changes cannot be performed with automatic migration and will cause the publish to fail:
scheduling.Unique or Primary Key constraints. This could result in existing tables being in an invalid state.If you need to make changes that aren't supported by automatic migration, see Incremental Migrations for a production-ready pattern that allows complex schema changes without downtime or data loss.
For development and testing, you can use spacetime publish --delete-data to completely reset your database, but this should not be used in production as it permanently deletes all data.
--delete-data freely during early development when data loss is acceptableFor complex schema changes that aren't supported by automatic migration:
During automatic migrations, active client connections are maintained and subscriptions continue to function. However:
The SpacetimeDB team is working on enhanced migration capabilities, including:
Check the SpacetimeDB documentation and GitHub repository for the latest migration features and capabilities.