Introduction
The PowerSync protocol is schemaless, and not directly affected by schema changes. Replicating data from the source database to sync buckets may be affected by server-side changes to the schema (in the case of Postgres), and may need reprocessing in some cases. The client-side schema is just a view on top of the schemaless data. Updating this client-side schema is immediate when the new version of the app runs, with no client-side migrations required. The developer is responsible for keeping client-side schema changes backwards-compatible with older versions of client apps. PowerSync has some functionality to assist with this:- Different Sync Rules can be applied based on parameters such as client version.
- Sync Rules can apply simple data transformations to keep data in a format compatible with older clients.
Client-Side Impact of Schema and Sync Rule Changes
As mentioned above, the PowerSync system itself is schemaless — the client syncs any data as received, in JSON format, regardless of the data model on the client. The schema as supplied on the client is only a view on top of the schemaless data.- If tables/collections not described by the client-side schema are synced, it is stored internally, but not accessible.
- Same applies for columns/field not described by the client-side schema.
-
When there is a type mismatch, SQLite’s
CAST
functionality is used to cast to the type described by the schema.- Data is internally stored as JSON.
-
SQLite’s
CAST
is used to cast values toTEXT
,INTEGER
orREAL
. -
Casting between types should never error, but it may not fully represent the original data. For example, casting an arbitrary string to
INTEGER
will likely result in a “0” value. - Full rules for casting between types are described in the SQLite documentation here.
- Removing a table/collection is handled on the client as if the table exists with no data.
-
Removing a column/field is handled on the client as if the values are
undefined
.
Postgres Specifics
PowerSync keeps the sync buckets up to date with any incremental data changes, as recorded in the Postgres WAL / received in the logical replication stream. This is also referred to as DML (Data Manipulation Language) queries. However, this does not include DDL (Data Definition Language), which includes:- Creating, dropping or renaming tables.
- Changing replica identity of a table.
- Adding, dropping or renaming columns.
- Changing the type of a column.
Postgres schema changes affecting Sync Rules
DROP table
Dropping a table is not directly detected by PowerSync, and previous data may be preserved. To make sure the data is removed,TRUNCATE
the table before dropping, or remove the table from Sync Rules.
CREATE table
The new table is detected as soon as data is inserted.DROP and re-CREATE table
This is a special case of combiningDROP
and CREATE
. If a dropped table is created again, and data is inserted into the new table, the schema change is detected by PowerSync. PowerSync will delete the old data in this case, as if TRUNCATE
was called before dropping.
RENAME table
A renamed table is handled similarly to dropping the old table, and creating a new table with the new name. The rename is only detected when data is inserted, updated or deleted to the new table. At this point, PowerSync effectively does aTRUNCATE
of the old table, and replicates the new table.
This may be a slow operation if the table is large, and all other replication will be blocked until the new table is replicated.
Change REPLICA IDENTITY
The replica identity of a table is considered changed if either:-
The type of replica identity changes (
DEFAULT
,INDEX
,FULL
,NOTHING
). - The name or type of columns part of the replica identity changes.
-
Using
REPLICA IDENTITY FULL
, and any column is added, removed, renamed, or the type changed. -
Using
REPLICA IDENTITY DEFAULT
, and the type of any column in the primary key is changed. -
Using
REPLICA IDENTITY INDEX
, and the type of any column in the replica index is changed. - The primary key or replica index is removed or changed.
Column changes
Column changes such as adding, dropping, renaming columns, or changing column types, are not automatically detected by PowerSync (unless it affects the replica identity as described above). Adding a column with aNULL
default value will generally not cause issues. Existing records will have a missing value instead of NULL
value, but those are generally treated the same on the client.
Adding a column with a different default value, whether it’s a static or computed value, will not have this default automatically replicated for existing rows. To propagate this value, make an update to every existing row.
Removing a column will not have the values automatically removed for existing rows on PowerSync. To propagate the change, make an update to every existing row.
Changing a column type, and/or changing the value of a column using an ALTER TABLE
statement, will not be automatically replicated to PowerSync. In some cases, the change will have no effect on PowerSync (for example changing between VARCHAR
and TEXT
types). When the values are expected to change, make an update to every existing row to propagate the changes.
Publication changes
A table is not replicated unless it is part of the powersync publication. If a table is added to the publication, it is treated the same as a new table, and any existing data is replicated. This may be a slow operation if the table is large, and all other replication will be blocked until the new table is replicated. There are additional changes that can be made to a table in a publication:- Which operations are replicated (insert, update, delete and truncate).
- Which rows are replicated (row filters).
MongoDB Specifics
Since MongoDB is schemaless, schema changes generally do not impact PowerSync. However, adding, dropping, and renaming collections require special consideration.Adding Collections
Sync Rules can include collections that do not yet exist in the source database. These collections will be created in MongoDB when data is first inserted. PowerSync will begin replicating changes as they occur in the source database.Dropping Collections
Due to a limitation in the replication process, dropping a collection does not immediately propagate to synced clients. To ensure the change is reflected, any additionalinsert
, update
, replace
, or delete
operation must be performed in any collection within a synced database.
Renaming Collections
Renaming a synced collection to a name that is not included in the Sync Rules has the same effect as dropping the collection. Renaming an unsynced collection to a name that is included in the Sync Rules triggers an initial snapshot replication. The time required for this process depends on the collection size. Circular renames (e.g., renamingtodos
→ todos_old
→ todos
) are not directly supported. To reprocess the database after such changes, a Sync Rules update must be deployed.
MySQL (Beta) Specifics
PowerSync keeps the sync buckets up to date with any incremental data changes as recorded in the MySQL binary log. The binary log also provides DDL (Data Definition Language) query updates, which include:- Creating, dropping or renaming tables.
- Truncating tables. (Not technically a schema change, but they appear in the query updates regardless.)
- Changing replica identity of a table. (Creation, deletion or modification of primary keys, unique indexes, etc.)
- Adding, dropping, renaming or changing the types of columns.
MySQL schema changes affecting Sync Rules
DROP table
PowerSync will detect when a table is dropped, and automatically remove the data from the sync buckets.CREATE table
Table creation is detected and handled the first time row events for the new table appear on the binary log.TRUNCATE table
PowerSync will detect truncate statements in the binary log, and consequently remove all data from the sync buckets for that table.RENAME table
A renamed table is handled similarly to dropping the old table, and then creating a new table with existing data under the new name. This may be a slow operation if the table is large, since the “new” table has to be re-replicated. Replication will be blocked until the new table is replicated.Change REPLICA IDENTITY
The replica identity of a table is considered to be changed if either:-
The type of replica identity changes (
DEFAULT
,INDEX
,FULL
,NOTHING
). - The name or type of columns which form part of the replica identity changes.
-
Using
REPLICA IDENTITY FULL
, and any column is added, removed, renamed, or the type changed. -
Using
REPLICA IDENTITY DEFAULT
, and the type of any column in the primary key is changed. -
Using
REPLICA IDENTITY INDEX
, and the type of any column in the replica index is changed. - The primary key or replica index is removed or changed.
Column changes
Column changes such as adding, dropping, renaming columns, or changing column types, are detected by PowerSync but will generally not result in re-replication. (Unless the replica identity was affected as described above). Adding a column with aNULL
default value will generally not cause issues. Existing records will have a missing value instead of NULL
value, but those are generally treated the same on the client.
Adding a column with a different default value, whether it’s a static or computed value, will not have this default automatically replicated for existing rows. To propagate this value, make an update to every existing row.
Removing a column will not have the values automatically removed for existing rows on PowerSync. To propagate the change, make an update to every existing row.
Changing a column type, and/or changing the default value of a column using an ALTER TABLE
statement, will not be automatically replicated to PowerSync.
In some cases, the change will have no effect on PowerSync (for example, changing between VARCHAR
and TEXT
types). When the values are expected to change, make an update to every existing row to propagate the changes.