-
Notifications
You must be signed in to change notification settings - Fork 30
Improve compatibility check for core extension #249
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/powersync_core/lib/src/database/core_version.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import 'package:sqlite_async/sqlite3_common.dart'; | ||
|
||
/// A parsed (major, minor, patch) version triple representing a version of the | ||
/// loaded core extension. | ||
extension type const PowerSyncCoreVersion((int, int, int) _tuple) { | ||
int get major => _tuple.$1; | ||
int get minor => _tuple.$2; | ||
int get patch => _tuple.$3; | ||
|
||
int compareTo(PowerSyncCoreVersion other) { | ||
return switch (major.compareTo(other.major)) { | ||
0 => switch (minor.compareTo(other.minor)) { | ||
0 => patch.compareTo(other.patch), | ||
var other => other, | ||
}, | ||
var other => other, | ||
}; | ||
} | ||
|
||
bool operator <(PowerSyncCoreVersion other) => compareTo(other) < 0; | ||
bool operator >=(PowerSyncCoreVersion other) => compareTo(other) >= 0; | ||
|
||
String get versionString => '$major.$minor.$patch'; | ||
|
||
void checkSupported() { | ||
const isWeb = bool.fromEnvironment('dart.library.js_interop'); | ||
|
||
if (this < minimum || this >= maximumExclusive) { | ||
var message = | ||
'Unsupported powersync extension version. This version of the ' | ||
'PowerSync SDK needs >=${minimum.versionString} ' | ||
'<${maximumExclusive.versionString}, ' | ||
'but detected version $versionString.'; | ||
if (isWeb) { | ||
message += | ||
'\nTry downloading the updated assets: https://docs.powersync.com/client-sdk-references/flutter/flutter-web-support#assets'; | ||
} | ||
|
||
throw SqliteException(1, message); | ||
} | ||
} | ||
|
||
/// Parses the output of `powersync_rs_version()`, e.g. `0.3.9/5d64f366`, into | ||
/// a [PowerSyncCoreVersion]. | ||
static PowerSyncCoreVersion parse(String version) { | ||
try { | ||
final [major, minor, patch] = | ||
version.split(RegExp(r'[./]')).take(3).map(int.parse).toList(); | ||
|
||
return PowerSyncCoreVersion((major, minor, patch)); | ||
} catch (e) { | ||
throw SqliteException(1, | ||
'Unsupported powersync extension version. Need >=0.2.0 <1.0.0, got: $version. Details: $e'); | ||
} | ||
} | ||
|
||
/// The minimum version of the sqlite core extensions we support. We check | ||
/// this version when opening databases to fail early and with an actionable | ||
/// error message. | ||
// Note: When updating this, also update the download URL in | ||
// scripts/init_powersync_core_binary.dart and the version ref in | ||
// packages/sqlite3_wasm_build/build.sh | ||
static const minimum = PowerSyncCoreVersion((0, 3, 11)); | ||
|
||
/// The first version of the core extensions that this version of the Dart | ||
/// SDK doesn't support. | ||
static const maximumExclusive = PowerSyncCoreVersion((1, 0, 0)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
packages/powersync_core/test/database/core_version_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import 'package:powersync_core/src/database/core_version.dart'; | ||
import 'package:sqlite3/common.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('PowerSyncCoreVersion', () { | ||
test('parse', () { | ||
expect(PowerSyncCoreVersion.parse('0.3.9/5d64f366'), (0, 3, 9)); | ||
}); | ||
|
||
test('compare', () { | ||
void expectLess(String a, String b) { | ||
final parsedA = PowerSyncCoreVersion.parse(a); | ||
final parsedB = PowerSyncCoreVersion.parse(b); | ||
|
||
expect(parsedA.compareTo(parsedB), -1); | ||
expect(parsedB.compareTo(parsedA), 1); | ||
|
||
expect(parsedA.compareTo(parsedA), 0); | ||
expect(parsedB.compareTo(parsedB), 0); | ||
} | ||
|
||
expectLess('0.1.0', '1.0.0'); | ||
expectLess('1.0.0', '1.2.0'); | ||
expectLess('0.3.9', '0.3.11'); | ||
}); | ||
|
||
test('checkSupported', () { | ||
expect(PowerSyncCoreVersion.parse('0.3.10').checkSupported, | ||
throwsA(isA<SqliteException>())); | ||
expect(PowerSyncCoreVersion.parse('1.0.0').checkSupported, | ||
throwsA(isA<SqliteException>())); | ||
|
||
PowerSyncCoreVersion.minimum.checkSupported(); | ||
expect(PowerSyncCoreVersion.maximumExclusive.checkSupported, | ||
throwsA(isA<SqliteException>())); | ||
}); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.