Skip to content

Commit 0c0127b

Browse files
committed
More notes on migrations
1 parent 125a75b commit 0c0127b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

usage/use-case-examples/raw-tables.mdx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,43 @@ CREATE TRIGGER todo_lists_delete
246246
## Migrations
247247

248248
In PowerSync's [JSON-based view system](/architecture/client-architecture#schema) the client-side schema is applied to the schemaless data, meaning no migrations are required. Raw tables however are excluded from this, so it is the developers responsibility to manage migrations for these tables.
249+
250+
### Adding raw tables as a new table
251+
252+
When you're adding new tables to your sync rules, clients will start to sync data on those tables - even if the tables aren't mentioned
253+
in the client's schema yet.
254+
So at the time you're introducing a new raw table to your app, it's possible that PowerSync has already synced some data for that
255+
table, which would be stored in `ps_untyped`. When adding regular tables, PowerSync will automatically extract rows from `ps_untyped`.
256+
With raw tables, that step is your responsibility. To copy data, run these statements in a transaction after creating the table:
257+
258+
```
259+
INSERT INTO my_table (id, my_column, ...)
260+
SELECT id, data ->> 'my_column' FROM ps_untyped WHERE type = 'my_table';
261+
DELETE FROM ps_untyped WHERE type = 'my_table';
262+
```
263+
264+
This does not apply if you've been using the raw table from the beginning (and never called `connect()` without them) - you only
265+
need this for raw tables you already had locally.
266+
267+
Another workaround is to clear PowerSync data when changing raw tables and opt for a full resync.
268+
269+
### Migrating to raw tables
270+
271+
To migrate from PowerSync-managed tables to raw tables, first:
272+
273+
1. Open the database with the new schema mentioning raw tables. PowerSync will copy data from tables previously managed by PowerSync into
274+
`ps_untyped`.
275+
2. Create raw tables.
276+
3. Run the `INSERT FROM SELECT` statement to insert `ps_untyped` data into your raw tables.
277+
278+
### Migrations on raw tables
279+
280+
When adding new columns to raw tables, there currently isn't a way to re-sync that table to add those columns from the server - we are
281+
investigating possible workarounds and encourage users to try out if they need this.
282+
283+
To ensure the column values are accurate, you'd have to delete all data after a migration and wait for the next complete sync.
284+
285+
## Deleting data and raw tables
286+
287+
APIs that clear an entire PowerSync database, like e.g. `disconnectAndClear()`, don't affect raw tables.
288+
This should be kept in mind when you're using those methods - data from raw tables needs to be deleted explicitly.

0 commit comments

Comments
 (0)