Skip to content

Commit 7cf5638

Browse files
Improve boolean hasSynced. Make tests more stable.
1 parent e06be2b commit 7cf5638

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

Diff for: core/src/commonIntegrationTest/kotlin/com/powersync/AttachmentsTest.kt

+4-7
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,12 @@ class AttachmentsTest {
275275
""",
276276
)
277277

278-
var nextRecord: Attachment? = attachmentQuery.awaitItem().first()
279-
if (nextRecord?.state == AttachmentState.ARCHIVED) {
280-
nextRecord = attachmentQuery.awaitItem().getOrNull(0)
278+
waitFor {
279+
var nextRecord: Attachment? = attachmentQuery.awaitItem().firstOrNull()
280+
// The record should have been deleted
281+
nextRecord shouldBe null
281282
}
282283

283-
// The record should have been deleted
284-
nextRecord shouldBe null
285-
286284
// The file should have been deleted from storage
287285
queue.localStorage.fileExists(localUri) shouldBe false
288286

@@ -522,7 +520,6 @@ class AttachmentsTest {
522520
remoteStorage = remote,
523521
attachmentsDirectory = getAttachmentsDir(),
524522
watchAttachments = { watchAttachments(database) },
525-
archivedCacheLimit = 0,
526523
errorHandler =
527524
object : SyncErrorHandler {
528525
override suspend fun onDownloadError(

Diff for: core/src/commonMain/kotlin/com/powersync/attachments/Attachment.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public data class Attachment(
7272
val localUri: String? = null,
7373
val mediaType: String? = null,
7474
val size: Long? = null,
75-
val hasSynced: Int = 0,
75+
val hasSynced: Boolean = false,
7676
val metaData: String? = null,
7777
) {
7878
public companion object {
@@ -91,7 +91,7 @@ public data class Attachment(
9191
mediaType = cursor.getStringOptional(name = "media_type"),
9292
size = cursor.getLongOptional("size"),
9393
state = AttachmentState.fromLong(cursor.getLong("state")),
94-
hasSynced = cursor.getLong("has_synced").toInt(),
94+
hasSynced = cursor.getLong("has_synced").toInt() > 0,
9595
metaData = cursor.getStringOptional("meta_data"),
9696
)
9797
}

Diff for: core/src/commonMain/kotlin/com/powersync/attachments/AttachmentQueue.kt

+22-7
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ public open class AttachmentQueue(
363363
(existingQueueItem.state == AttachmentState.ARCHIVED) {
364364
// The attachment is present again. Need to queue it for sync.
365365
// We might be able to optimize this in future.
366-
if (existingQueueItem.hasSynced == 1) {
366+
if (existingQueueItem.hasSynced) {
367367
// No remote action required, we can restore the record (avoids deletion).
368368
attachmentUpdates.add(
369369
existingQueueItem.copy(state = AttachmentState.SYNCED),
@@ -389,13 +389,25 @@ public open class AttachmentQueue(
389389
}
390390

391391
/**
392-
* Archive any items not specified in the watched items except for items pending delete.
392+
* Archive any items not specified in the watched items.
393+
* For QUEUED_DELETE or QUEUED_UPLOAD states, archive only if hasSynced is true.
394+
* For other states, archive if the record is not found in the items.
393395
*/
394396
currentAttachments
395-
.filter {
396-
it.state != AttachmentState.QUEUED_DELETE &&
397-
it.state != AttachmentState.QUEUED_UPLOAD &&
398-
null == items.find { update -> update.id == it.id }
397+
.filter { attachment ->
398+
val notInWatchedItems =
399+
items.find { update -> update.id == attachment.id } == null
400+
if (notInWatchedItems) {
401+
when (attachment.state) {
402+
// Archive these record if they have synced
403+
AttachmentState.QUEUED_DELETE, AttachmentState.QUEUED_UPLOAD -> attachment.hasSynced
404+
// Other states, such as QUEUED_DOWNLOAD can be archived if they are not present in watched items
405+
else -> true
406+
}
407+
} else {
408+
// The record is present in watched items, no need to archive it
409+
false
410+
}
399411
}.forEach {
400412
attachmentUpdates.add(it.copy(state = AttachmentState.ARCHIVED))
401413
}
@@ -484,7 +496,10 @@ public open class AttachmentQueue(
484496
db.writeTransaction { tx ->
485497
updateHook.invoke(tx, attachment)
486498
return@writeTransaction attachmentContext.upsertAttachment(
487-
attachment.copy(state = AttachmentState.QUEUED_DELETE),
499+
attachment.copy(
500+
state = AttachmentState.QUEUED_DELETE,
501+
hasSynced = false,
502+
),
488503
tx,
489504
)
490505
}

Diff for: core/src/commonMain/kotlin/com/powersync/attachments/implementation/AttachmentContextImpl.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public open class AttachmentContextImpl(
191191
updatedRecord.mediaType,
192192
updatedRecord.size,
193193
updatedRecord.state.ordinal,
194-
updatedRecord.hasSynced,
194+
if (updatedRecord.hasSynced) 1 else 0,
195195
updatedRecord.metaData,
196196
),
197197
)

Diff for: core/src/commonMain/kotlin/com/powersync/attachments/sync/SyncingService.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public open class SyncingService(
208208
attachment,
209209
)
210210
logger.i("Uploaded attachment \"${attachment.id}\" to Cloud Storage")
211-
return attachment.copy(state = AttachmentState.SYNCED, hasSynced = 1)
211+
return attachment.copy(state = AttachmentState.SYNCED, hasSynced = true)
212212
} catch (e: Exception) {
213213
logger.e("Upload attachment error for attachment $attachment: ${e.message}")
214214
if (errorHandler != null) {
@@ -246,7 +246,7 @@ public open class SyncingService(
246246
return attachment.copy(
247247
localUri = attachmentPath,
248248
state = AttachmentState.SYNCED,
249-
hasSynced = 1,
249+
hasSynced = true,
250250
)
251251
} catch (e: Exception) {
252252
if (errorHandler != null) {

0 commit comments

Comments
 (0)