Change a synced schema
Every sync checks that the app and the server have the same synced schema, meaning the same synced tables, columns, relations, and unique indexes. After you change the synced schema, installed apps stop syncing until they update. Plan each change as a release of both the server and the app.
Which changes stop sync
Here are common model changes and whether each one stops sync:
| Change | Stops sync? |
|---|---|
| Adding, removing, or renaming a synced table | Yes |
| Adding, removing, or renaming a column | Yes |
| Changing a column's type or whether it's nullable | Yes |
Changing a relation, including its onDelete action | Yes |
Changing the default parent of an onDelete=SetDefault relation | Yes |
| Adding, removing, or changing a unique index | Yes |
| Adding, removing, or changing a non-unique index | No |
| Renaming an index | No |
| Changing the order of fields | No |
Changing a table that doesn't sync, with database: server, client, or all | No |
What happens to outdated apps
An outdated app's sync fails with OfflineSyncTablesHashMismatchException, and retrying doesn't help until the app updates. Unsynced changes stay on the device and sync after the update, unless it removes their table or column.
Roll out a change
Before your first schema change, ship an app that catches OfflineSyncTablesHashMismatchException and asks the user to update. That way, installed apps can ask for the update when it's needed. The sync loop in Restart after the connection drops shows where to catch the exception.
Release a schema change in this order:
- Change the models and create the migration.
- Deploy the server and publish the app update at the same time.
Rename a table or column, or change a column's type
Offline sync sees a rename as removing one column or table and adding another. If a release does both and the removed one holds synced data, offline sync can't load the new schema. It throws OfflineSyncSchemaReconciliationException, which names the table and column. On a device, the error comes when the app opens the database. On the server, it comes when the server first uses the database. A change to a column's type can fail the same way.
To rename without the error, also rename offline sync's record of the column or table in the same migration:
- Rename the field or table in your model, and create the migration.
- In both the server and the client migration, update the column's row in
crdt_schema_columns, or the table's row incrdt_schema_tables, to the new name.
This keeps the sync history. It also works for apps that skip releases, because each app runs the migration when it updates. If you miss step 2, the exception's message says what to update.
For a type change, the message says which values to convert in the same migration.
Related
- Offline sync: the module's limits.
- Migrations: creating the migration for any table change.
- Set up offline sync: the sync loop that stops on a schema mismatch.