Skip to content

Add test for update streams with simplified table name #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import 'package:powersync_flutter_supabase_todolist_optional_sync_demo/models/sy
const todosTable = 'todos';
const listsTable = 'lists';

Schema makeSchema({synced = bool}) {
Schema makeSchema({required bool synced}) {
String syncedName(String table) {
if (synced) {
// results in lists, todos
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class PowerSyncUpdateNotification extends UpdateNotification {
Set<String> _friendlyTableNames(Iterable<String> originalTables) {
Set<String> tables = {};
for (var table in originalTables) {
var friendlyName = friendlyTableName(table);
if (friendlyName != null) {
if (friendlyTableName(table) case final friendlyName?) {
tables.add(friendlyName);
} else if (!table.startsWith('ps_')) {
tables.add(table);
Expand Down
14 changes: 10 additions & 4 deletions packages/powersync_core/lib/src/schema_logic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ Future<void> updateSchemaInIsolate(
}

String? friendlyTableName(String table) {
final re = RegExp(r"^ps_data__(.+)$");
final re2 = RegExp(r"^ps_data_local__(.+)$");
final match = re.firstMatch(table) ?? re2.firstMatch(table);
return match?.group(1);
const prefix1 = 'ps_data__';
const prefix2 = 'ps_data_local__';

if (table.startsWith(prefix2)) {
return table.substring(prefix2.length);
} else if (table.startsWith(prefix1)) {
return table.substring(prefix1.length);
} else {
return null;
}
}
24 changes: 24 additions & 0 deletions packages/powersync_core/test/watch_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:math';

import 'package:async/async.dart';
import 'package:powersync_core/powersync_core.dart';
import 'package:sqlite_async/sqlite_async.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -138,5 +139,28 @@ void main() {
UpdateNotification.single('assets')
]));
});

test('emits update events with friendly names', () async {
final powersync = await testUtils.setupPowerSync(
path: path,
schema: Schema([
Table.localOnly('users', [
Column.text('name'),
]),
Table('assets', [
Column.text('name'),
]),
]),
);

final updates = StreamQueue(powersync.updates);
await powersync
.execute('INSERT INTO users (id, name) VALUES (uuid(), ?)', ['test']);
await expectLater(updates, emits(UpdateNotification({'users'})));

await powersync.execute(
'INSERT INTO assets (id, name) VALUES (uuid(), ?)', ['test']);
await expectLater(updates, emits(UpdateNotification({'assets'})));
});
});
}