Skip to content

Commit a2538f6

Browse files
committed
Check extension version.
1 parent 72f774b commit a2538f6

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,42 @@ mixin PowerSyncDatabaseMixin implements SqliteConnection {
7878
.cast<UpdateNotification>();
7979

8080
await database.initialize();
81+
await _checkVersion();
8182
await database.execute('SELECT powersync_init()');
8283
await updateSchema(schema);
8384
await _updateHasSynced();
8485
}
8586

87+
/// Check that a supported version of the powersync extension is loaded.
88+
Future<void> _checkVersion() async {
89+
// Get version
90+
String version;
91+
try {
92+
final row =
93+
await database.get('SELECT powersync_rs_version() as version');
94+
version = row['version'];
95+
} catch (e) {
96+
throw SqliteException(
97+
1, 'The powersync extension is not loaded correctly. Details: $e');
98+
}
99+
100+
// Parse version
101+
List<int> versionInts;
102+
try {
103+
versionInts =
104+
version.split(RegExp(r'[./]')).take(3).map(int.parse).toList();
105+
} catch (e) {
106+
throw SqliteException(1,
107+
'Unsupported powersync extension version. Need ^0.2.0, got: $version. Details: $e');
108+
}
109+
110+
// Validate ^0.2.0
111+
if (versionInts[0] != 0 || versionInts[1] != 2 || versionInts[2] < 0) {
112+
throw SqliteException(1,
113+
'Unsupported powersync extension version. Need ^0.2.0, got: $version');
114+
}
115+
}
116+
86117
/// Wait for initialization to complete.
87118
///
88119
/// While initializing is automatic, this helps to catch and report initialization errors.

0 commit comments

Comments
 (0)