Skip to content

Commit 5113f5c

Browse files
committed
Fix version check, raise minimum version
1 parent a99e791 commit 5113f5c

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.powersync.db.crud.CrudRow
1414
import com.powersync.db.crud.CrudTransaction
1515
import com.powersync.db.internal.InternalDatabaseImpl
1616
import com.powersync.db.internal.InternalTable
17+
import com.powersync.db.internal.PowerSyncVersion
1718
import com.powersync.db.schema.Schema
1819
import com.powersync.db.schema.toSerializable
1920
import com.powersync.sync.PriorityStatusEntry
@@ -461,20 +462,9 @@ internal class PowerSyncDatabaseImpl(
461462
* Check that a supported version of the powersync extension is loaded.
462463
*/
463464
private fun checkVersion(powerSyncVersion: String) {
464-
// Parse version
465-
val versionInts: List<Int> =
466-
try {
467-
powerSyncVersion
468-
.split(Regex("[./]"))
469-
.take(3)
470-
.map { it.toInt() }
471-
} catch (e: Exception) {
472-
throw Exception("Unsupported powersync extension version. Need ^0.2.0, got: $powerSyncVersion. Details: $e")
473-
}
474-
475-
// Validate ^0.2.0
476-
if (versionInts[0] != 0 || versionInts[1] < 2 || versionInts[2] < 0) {
477-
throw Exception("Unsupported powersync extension version. Need ^0.2.0, got: $powerSyncVersion")
465+
val version = PowerSyncVersion.parse(powerSyncVersion)
466+
if (version < PowerSyncVersion.MINIMUM) {
467+
PowerSyncVersion.mismatchError(powerSyncVersion)
478468
}
479469
}
480470
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.powersync.db.internal
2+
3+
internal data class PowerSyncVersion(val major: Int, val minor: Int, val patch: Int): Comparable<PowerSyncVersion> {
4+
override fun compareTo(other: PowerSyncVersion): Int {
5+
return when (val compareMajor = major.compareTo(other.major)) {
6+
0 -> when (val compareMinor = minor.compareTo(other.minor)) {
7+
0 -> patch.compareTo(other.patch)
8+
else -> compareMinor
9+
}
10+
else -> compareMajor
11+
}
12+
}
13+
14+
override fun toString(): String {
15+
return "$major.$minor.$patch"
16+
}
17+
18+
companion object {
19+
val MINIMUM: PowerSyncVersion = PowerSyncVersion(0, 3, 13)
20+
21+
fun parse(from: String): PowerSyncVersion {
22+
val versionInts: List<Int> =
23+
try {
24+
from
25+
.split(Regex("[./]"))
26+
.take(3)
27+
.map { it.toInt() }
28+
} catch (e: Exception) {
29+
mismatchError(from, e.toString())
30+
}
31+
32+
return PowerSyncVersion(versionInts[0], versionInts[1], versionInts[2])
33+
}
34+
35+
fun mismatchError(actualVersion: String, details: String? = null): Nothing {
36+
var message = "Unsupported PowerSync extension version (need ^$MINIMUM, got $actualVersion)."
37+
if (details != null) {
38+
message = " Details: $details"
39+
}
40+
41+
throw Exception(message)
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)