Skip to content

Commit 418853d

Browse files
committed
Using new refreshSchema function to ensure watches are aware of changes made by updateSchema.
1 parent 630af65 commit 418853d

File tree

7 files changed

+41
-5
lines changed

7 files changed

+41
-5
lines changed

demos/local-only-todolist/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ graph TD
7171
## Limitations
7272

7373
`updateSchema` cannot be called inside a transaction, and it's recommended to perform the schema update when the database isn't connected.
74-
Additionally, the current implementation of the PowerSync SDK's `watch` method may not correctly track tables that are altered by `updateSchema`. As a result, you will likely need to refresh your `watch` calls after the schema update is complete. In this demo, the `watch` calls are refreshed by navigating to the login and signup pages after the user successfully logs in and the schema update is finished.
74+
Additionally, the current implementation of the PowerSync SDK's `watch` method may not correctly track tables that are altered by `updateSchema`. Note that `refreshSchema` can be executed after updating the schema to resolve this (the demo uses it in `switchToSyncedSchema` which can be viewed [here](./lib/models/schema.dart)).

demos/local-only-todolist/lib/models/schema.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,16 @@ Schema makeSchema({synced = bool}) {
7373
Table(table.name, table.columns,
7474
indexes: table.indexes, viewName: onlineName(table.name)),
7575
for (var table in tables)
76-
Table.localOnly('inactive_local_${table.name}', table.columns,
76+
Table.localOnly('local_${table.name}', table.columns,
7777
indexes: table.indexes, viewName: localName(table.name))
7878
]);
7979
}
8080

8181
switchToSyncedSchema(PowerSyncDatabase db, String userId) async {
8282
await db.updateSchema(makeSchema(synced: true));
83+
84+
// needed to ensure that watches/queries are aware of the updated schema
85+
await db.refreshSchema();
8386
await setSyncEnabled(true);
8487

8588
await db.writeTransaction((tx) async {

demos/local-only-todolist/lib/powersync.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,6 @@ Future<void> connectDatabase() async {
185185

186186
if (!isSyncMode) {
187187
await switchToSyncedSchema(db, getUserId());
188-
// Without closing and reopening the database the list pages breaks if there is no data in the tables when logging in/signing up.
189-
await db.close();
190-
await _openDatabase();
191188
}
192189

193190
currentConnector = SupabaseConnector(db);

packages/powersync/lib/src/database/native/native_powersync_database.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ class PowerSyncDatabaseImpl
264264
this.schema = schema;
265265
return updateSchemaInIsolate(database, schema);
266266
}
267+
268+
@override
269+
Future<void> refreshSchema() async {
270+
await database.refreshSchema();
271+
}
267272
}
268273

269274
class _PowerSyncDatabaseIsolateArgs {

packages/powersync/lib/src/database/powersync_database_impl_stub.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,9 @@ class PowerSyncDatabaseImpl
118118
Map<String, dynamic>? params}) {
119119
throw UnimplementedError();
120120
}
121+
122+
@override
123+
Future<void> refreshSchema() {
124+
throw UnimplementedError();
125+
}
121126
}

packages/powersync/lib/src/database/web/web_powersync_database.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,9 @@ class PowerSyncDatabaseImpl
210210
this.schema = schema;
211211
return database.writeLock((tx) => schema_logic.updateSchema(tx, schema));
212212
}
213+
214+
@override
215+
Future<void> refreshSchema() {
216+
return database.refreshSchema();
217+
}
213218
}

packages/powersync/test/offline_online_test.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:convert';
22

33
import 'package:powersync/powersync.dart';
4+
import 'package:sqlite_async/src/utils/shared_utils.dart';
45
import 'package:test/test.dart';
56

67
import 'utils/test_utils_impl.dart';
@@ -140,5 +141,25 @@ void main() {
140141
}
141142
]));
142143
});
144+
145+
test('Watch correct table after switching schema', () async {
146+
// Start with "offline-only" schema.
147+
// This does not record any operations to the crud queue.
148+
var db =
149+
await testUtils.setupPowerSync(path: path, schema: makeSchema(false));
150+
151+
final customerWatchTables =
152+
await getSourceTables(db, 'SELECT * FROM customers');
153+
154+
expect(
155+
customerWatchTables.contains('ps_data_local__local_customers'), true);
156+
await db.updateSchema(makeSchema(true));
157+
await db.refreshSchema();
158+
159+
final onlineCustomerWatchTables =
160+
await getSourceTables(db, 'SELECT * FROM customers');
161+
162+
expect(onlineCustomerWatchTables.contains('ps_data__customers'), true);
163+
});
143164
});
144165
}

0 commit comments

Comments
 (0)