Skip to content

Commit 16a79c5

Browse files
Radiokotsimolus3
andauthored
Do not split transactions when uploading data (#175)
* Do not split transactions when uploading data * Add test and changelog entry --------- Co-authored-by: Simon Binder <[email protected]>
1 parent 5f32d69 commit 16a79c5

File tree

6 files changed

+46
-13
lines changed

6 files changed

+46
-13
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 1.0.0-BETA31
44

55
* Added helpers for Attachment syncing.
6+
* Fix `getNextCrudTransaction()` only returning a single item.
67

78
## 1.0.0-BETA30
89

core/src/commonIntegrationTest/kotlin/com/powersync/DatabaseTest.kt

+21
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,25 @@ class DatabaseTest {
369369
val count = database.get("SELECT COUNT(*) from people") { it.getLong(0)!! }
370370
count shouldBe 1
371371
}
372+
373+
@Test
374+
fun testCrudTransaction() =
375+
databaseTest {
376+
database.execute("INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)", listOf("a", "[email protected]"))
377+
378+
database.writeTransaction {
379+
it.execute("INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)", listOf("b", "[email protected]"))
380+
it.execute("INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)", listOf("c", "[email protected]"))
381+
}
382+
383+
var transaction = database.getNextCrudTransaction()
384+
transaction!!.crud shouldHaveSize 1
385+
transaction.complete(null)
386+
387+
transaction = database.getNextCrudTransaction()
388+
transaction!!.crud shouldHaveSize 2
389+
transaction.complete(null)
390+
391+
database.getNextCrudTransaction() shouldBe null
392+
}
372393
}

core/src/commonMain/kotlin/com/powersync/bucket/BucketStorage.kt

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ internal interface BucketStorage {
1414

1515
fun nextCrudItem(transaction: PowerSyncTransaction): CrudEntry?
1616

17+
fun getCrudItemsByTransactionId(
18+
transactionId: Int,
19+
transaction: PowerSyncTransaction,
20+
): List<CrudEntry>
21+
1722
suspend fun hasCrud(): Boolean
1823

1924
fun hasCrud(transaction: PowerSyncTransaction): Boolean

core/src/commonMain/kotlin/com/powersync/bucket/BucketStorageImpl.kt

+14-3
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,24 @@ internal class BucketStorageImpl(
4141
return id ?: throw IllegalStateException("Client ID not found")
4242
}
4343

44-
override suspend fun nextCrudItem(): CrudEntry? = db.getOptional(sql = nextCrudQuery, mapper = nextCrudMapper)
44+
override suspend fun nextCrudItem(): CrudEntry? = db.getOptional(sql = nextCrudQuery, mapper = crudEntryMapper)
4545

4646
override fun nextCrudItem(transaction: PowerSyncTransaction): CrudEntry? =
47-
transaction.getOptional(sql = nextCrudQuery, mapper = nextCrudMapper)
47+
transaction.getOptional(sql = nextCrudQuery, mapper = crudEntryMapper)
48+
49+
override fun getCrudItemsByTransactionId(
50+
transactionId: Int,
51+
transaction: PowerSyncTransaction,
52+
): List<CrudEntry> =
53+
transaction.getAll(
54+
sql = transactionCrudQuery,
55+
parameters = listOf(transactionId),
56+
mapper = crudEntryMapper,
57+
)
4858

4959
private val nextCrudQuery = "SELECT id, tx_id, data FROM ${InternalTable.CRUD} ORDER BY id ASC LIMIT 1"
50-
private val nextCrudMapper: (SqlCursor) -> CrudEntry = { cursor ->
60+
private val transactionCrudQuery = "SELECT id, tx_id, data FROM ${InternalTable.CRUD} WHERE tx_id = ? ORDER BY id ASC"
61+
private val crudEntryMapper: (SqlCursor) -> CrudEntry = { cursor ->
5162
CrudEntry.fromRow(
5263
CrudRow(
5364
id = cursor.getString(0)!!,

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

+4-9
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,10 @@ internal class PowerSyncDatabaseImpl(
298298
if (txId == null) {
299299
listOf(entry)
300300
} else {
301-
transaction.getAll("SELECT id, tx_id, data FROM ps_crud ORDER BY id ASC LIMIT 1") {
302-
CrudEntry.fromRow(
303-
CrudRow(
304-
id = it.getString("id"),
305-
data = it.getString("data"),
306-
txId = it.getLongOptional("tx_id")?.toInt(),
307-
),
308-
)
309-
}
301+
bucketStorage.getCrudItemsByTransactionId(
302+
transactionId = txId,
303+
transaction = transaction,
304+
)
310305
}
311306

312307
return@readTransaction CrudTransaction(

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ development=true
1717
RELEASE_SIGNING_ENABLED=true
1818
# Library config
1919
GROUP=com.powersync
20-
LIBRARY_VERSION=1.0.0-BETA30
20+
LIBRARY_VERSION=1.0.0-BETA31
2121
GITHUB_REPO=https://github.com/powersync-ja/powersync-kotlin.git
2222
# POM
2323
POM_URL=https://github.com/powersync-ja/powersync-kotlin/

0 commit comments

Comments
 (0)