Skip to main content
Version: Next

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:

ChangeStops sync?
Adding, removing, or renaming a synced tableYes
Adding, removing, or renaming a columnYes
Changing a column's type or whether it's nullableYes
Changing a relation, including its onDelete actionYes
Changing the default parent of an onDelete=SetDefault relationYes
Adding, removing, or changing a unique indexYes
Adding, removing, or changing a non-unique indexNo
Renaming an indexNo
Changing the order of fieldsNo
Changing a table that doesn't sync, with database: server, client, or allNo

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:

  1. Change the models and create the migration.
  2. 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:

  1. Rename the field or table in your model, and create the migration.
  2. In both the server and the client migration, update the column's row in crdt_schema_columns, or the table's row in crdt_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.