Skip to content

Commit 2688aea

Browse files
authored
Merge pull request #293 from powersync-ja/test-friendly-table-name
Add test for update streams with simplified table name
2 parents 12488fb + 1bf59b3 commit 2688aea

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

demos/supabase-todolist-optional-sync/lib/models/schema.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import 'package:powersync_flutter_supabase_todolist_optional_sync_demo/models/sy
1616
const todosTable = 'todos';
1717
const listsTable = 'lists';
1818

19-
Schema makeSchema({synced = bool}) {
19+
Schema makeSchema({required bool synced}) {
2020
String syncedName(String table) {
2121
if (synced) {
2222
// results in lists, todos

packages/powersync_core/lib/src/powersync_update_notification.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ class PowerSyncUpdateNotification extends UpdateNotification {
4545
Set<String> _friendlyTableNames(Iterable<String> originalTables) {
4646
Set<String> tables = {};
4747
for (var table in originalTables) {
48-
var friendlyName = friendlyTableName(table);
49-
if (friendlyName != null) {
48+
if (friendlyTableName(table) case final friendlyName?) {
5049
tables.add(friendlyName);
5150
} else if (!table.startsWith('ps_')) {
5251
tables.add(table);

packages/powersync_core/lib/src/schema_logic.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ Future<void> updateSchemaInIsolate(
2121
}
2222

2323
String? friendlyTableName(String table) {
24-
final re = RegExp(r"^ps_data__(.+)$");
25-
final re2 = RegExp(r"^ps_data_local__(.+)$");
26-
final match = re.firstMatch(table) ?? re2.firstMatch(table);
27-
return match?.group(1);
24+
const prefix1 = 'ps_data__';
25+
const prefix2 = 'ps_data_local__';
26+
27+
if (table.startsWith(prefix2)) {
28+
return table.substring(prefix2.length);
29+
} else if (table.startsWith(prefix1)) {
30+
return table.substring(prefix1.length);
31+
} else {
32+
return null;
33+
}
2834
}

packages/powersync_core/test/watch_test.dart

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

4+
import 'package:async/async.dart';
45
import 'package:powersync_core/powersync_core.dart';
56
import 'package:sqlite_async/sqlite_async.dart';
67
import 'package:test/test.dart';
@@ -138,5 +139,28 @@ void main() {
138139
UpdateNotification.single('assets')
139140
]));
140141
});
142+
143+
test('emits update events with friendly names', () async {
144+
final powersync = await testUtils.setupPowerSync(
145+
path: path,
146+
schema: Schema([
147+
Table.localOnly('users', [
148+
Column.text('name'),
149+
]),
150+
Table('assets', [
151+
Column.text('name'),
152+
]),
153+
]),
154+
);
155+
156+
final updates = StreamQueue(powersync.updates);
157+
await powersync
158+
.execute('INSERT INTO users (id, name) VALUES (uuid(), ?)', ['test']);
159+
await expectLater(updates, emits(UpdateNotification({'users'})));
160+
161+
await powersync.execute(
162+
'INSERT INTO assets (id, name) VALUES (uuid(), ?)', ['test']);
163+
await expectLater(updates, emits(UpdateNotification({'assets'})));
164+
});
141165
});
142166
}

0 commit comments

Comments
 (0)