From 4bfd811d97b8da4aebd03a5a66eea441f963c709 Mon Sep 17 00:00:00 2001 From: DominicGBauer Date: Sun, 7 Apr 2024 19:12:06 +0200 Subject: [PATCH 1/3] chore(attachments): minor improvements --- demos/supabase-todolist/pubspec.lock | 2 +- .../powersync_attachments_helper/CHANGELOG.md | 5 ++++ .../lib/src/attachments_queue.dart | 7 +++++- .../lib/src/attachments_service.dart | 4 ---- .../lib/src/syncing_service.dart | 24 +++++++++++++++++-- .../powersync_attachments_helper/pubspec.yaml | 4 ++-- 6 files changed, 36 insertions(+), 10 deletions(-) diff --git a/demos/supabase-todolist/pubspec.lock b/demos/supabase-todolist/pubspec.lock index 4e49469c..29c4f31d 100644 --- a/demos/supabase-todolist/pubspec.lock +++ b/demos/supabase-todolist/pubspec.lock @@ -429,7 +429,7 @@ packages: path: "../../packages/powersync_attachments_helper" relative: true source: path - version: "0.3.0" + version: "0.3.1" realtime_client: dependency: transitive description: diff --git a/packages/powersync_attachments_helper/CHANGELOG.md b/packages/powersync_attachments_helper/CHANGELOG.md index a5a24e7d..aeafc461 100644 --- a/packages/powersync_attachments_helper/CHANGELOG.md +++ b/packages/powersync_attachments_helper/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.3.1 + +- Add periodic syncing and deleting of attachments +- Minor improvements + ## 0.3.0 - BREAKING CHANGE: `reconcileId` has been removed in favour of `reconcileIds`. This will require a change to `watchIds` implementation which is shown in `example/getting_started.dart` diff --git a/packages/powersync_attachments_helper/lib/src/attachments_queue.dart b/packages/powersync_attachments_helper/lib/src/attachments_queue.dart index e8ed1965..2c38b6ef 100644 --- a/packages/powersync_attachments_helper/lib/src/attachments_queue.dart +++ b/packages/powersync_attachments_helper/lib/src/attachments_queue.dart @@ -33,6 +33,10 @@ abstract class AbstractAttachmentQueue { /// Return true if you want to ignore attachment Future Function(Attachment attachment, Object exception)? onUploadError; + /// Interval in minutes to periodically delete archived attachments + /// Default is 5 minutes + int intervalInMinutes; + AbstractAttachmentQueue( {required this.db, required this.remoteStorage, @@ -40,7 +44,7 @@ abstract class AbstractAttachmentQueue { this.attachmentsQueueTableName = defaultAttachmentsQueueTableName, this.onDownloadError, this.onUploadError, - performInitialSync = true}) { + this.intervalInMinutes = 5}) { attachmentsService = AttachmentsService( db, localStorage, attachmentDirectoryName, attachmentsQueueTableName); syncingService = SyncingService( @@ -68,6 +72,7 @@ abstract class AbstractAttachmentQueue { watchIds(); syncingService.watchAttachments(); + syncingService.startPeriodicSync(intervalInMinutes); db.statusStream.listen((status) { if (db.currentStatus.connected) { diff --git a/packages/powersync_attachments_helper/lib/src/attachments_service.dart b/packages/powersync_attachments_helper/lib/src/attachments_service.dart index 11fcdd37..6f9b8da4 100644 --- a/packages/powersync_attachments_helper/lib/src/attachments_service.dart +++ b/packages/powersync_attachments_helper/lib/src/attachments_service.dart @@ -93,10 +93,6 @@ class AttachmentsService { (id, filename, local_uri, media_type, size, timestamp, state) VALUES (?, ?, ?, ?, ?, ?, ?) ''', updatedRecords); - await db.executeBatch(''' - DELETE FROM $table WHERE id = ? - ''', ids); - return; } diff --git a/packages/powersync_attachments_helper/lib/src/syncing_service.dart b/packages/powersync_attachments_helper/lib/src/syncing_service.dart index baaf4d58..ec94ff8e 100644 --- a/packages/powersync_attachments_helper/lib/src/syncing_service.dart +++ b/packages/powersync_attachments_helper/lib/src/syncing_service.dart @@ -20,6 +20,7 @@ class SyncingService { final Future Function(Attachment attachment, Object exception)? onUploadError; bool isProcessing = false; + Timer? timer; SyncingService(this.db, this.remoteStorage, this.localStorage, this.attachmentsService, this.getLocalUri, @@ -166,8 +167,7 @@ class SyncingService { bool fileExists = await file.exists(); if (fileExists) { - log.info('ignore file $id.$fileExtension as it already exists'); - return; + continue; } log.info('Adding $id to queue'); @@ -179,4 +179,24 @@ class SyncingService { await attachmentsService.saveAttachments(attachments); } + + /// Delete attachments which have been archived + deleteArchivedAttachments() async { + await db.execute(''' + DELETE FROM ${attachmentsService.table} + WHERE state = ${AttachmentState.archived.index} + '''); + } + + /// Periodically sync attachments and delete archived attachments + void startPeriodicSync(int intervalInMinutes) { + timer?.cancel(); + + timer = Timer.periodic(Duration(minutes: intervalInMinutes), (timer) { + log.info('Syncing attachments'); + runSync(); + log.info('Deleting archived attachments'); + deleteArchivedAttachments(); + }); + } } diff --git a/packages/powersync_attachments_helper/pubspec.yaml b/packages/powersync_attachments_helper/pubspec.yaml index 70666b7c..bb599186 100644 --- a/packages/powersync_attachments_helper/pubspec.yaml +++ b/packages/powersync_attachments_helper/pubspec.yaml @@ -1,6 +1,6 @@ name: powersync_attachments_helper description: A helper library for handling attachments when using PowerSync. -version: 0.3.0 +version: 0.3.1 repository: https://github.com/powersync-ja/powersync.dart homepage: https://www.powersync.com/ environment: @@ -12,7 +12,7 @@ dependencies: powersync: ^1.2.2 logging: ^1.2.0 - sqlite_async: ^0.6.0 + sqlite_async: ^0.6.1 path_provider: ^2.1.2 dev_dependencies: From 54779a141b738f7faf185b03bc12d7515ba0a333 Mon Sep 17 00:00:00 2001 From: DominicGBauer Date: Sun, 7 Apr 2024 19:15:08 +0200 Subject: [PATCH 2/3] docs: update changelog --- packages/powersync_attachments_helper/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/powersync_attachments_helper/CHANGELOG.md b/packages/powersync_attachments_helper/CHANGELOG.md index aeafc461..1028766a 100644 --- a/packages/powersync_attachments_helper/CHANGELOG.md +++ b/packages/powersync_attachments_helper/CHANGELOG.md @@ -1,7 +1,8 @@ ## 0.3.1 - Add periodic syncing and deleting of attachments -- Minor improvements +- Remove unnecessary delete +- Fix loop ## 0.3.0 From 524765f1af1d13ac9184da22993065f15d0072ee Mon Sep 17 00:00:00 2001 From: DominicGBauer Date: Wed, 10 Apr 2024 13:54:33 +0200 Subject: [PATCH 3/3] chore: pr reverts --- .../powersync_attachments_helper/lib/src/attachments_queue.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/powersync_attachments_helper/lib/src/attachments_queue.dart b/packages/powersync_attachments_helper/lib/src/attachments_queue.dart index 2c38b6ef..f2f8e742 100644 --- a/packages/powersync_attachments_helper/lib/src/attachments_queue.dart +++ b/packages/powersync_attachments_helper/lib/src/attachments_queue.dart @@ -33,7 +33,7 @@ abstract class AbstractAttachmentQueue { /// Return true if you want to ignore attachment Future Function(Attachment attachment, Object exception)? onUploadError; - /// Interval in minutes to periodically delete archived attachments + /// Interval in minutes to periodically run [syncingService.startPeriodicSync] /// Default is 5 minutes int intervalInMinutes;