Skip to content

Commit

Permalink
Touch old record to ensure backup provider picks it up for sync later
Browse files Browse the repository at this point in the history
  • Loading branch information
theachoem committed Jan 25, 2025
1 parent fe74bd8 commit efee8f2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/core/databases/adapters/base_db_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ abstract class BaseDbAdapter<T extends BaseDbModel> {
Map<String, dynamic>? options,
});

Future<T?> touch(T record);
Future<T?> set(T record);
Future<void> setAll(List<T> records);

Expand All @@ -29,7 +30,7 @@ abstract class BaseDbAdapter<T extends BaseDbModel> {

T modelFromJson(Map<String, dynamic> json);

Future<void> afterCommit(int id) async {
Future<void> afterCommit([int? id]) async {
debugPrint("BaseDbAdapter#afterCommit $id");

for (FutureOr<void> Function() globalCallback in _globalListeners) {
Expand Down
20 changes: 19 additions & 1 deletion lib/core/databases/adapters/objectbox/base_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ abstract class BaseBox<B extends BaseObjectBox, T extends BaseDbModel> extends B
return CollectionDbModel<T>(items: docs);
}

@override
Future<T?> touch(
T record, {
bool runCallbacks = true,
}) async {
B constructed = await modelToObject(record);

constructed.touch();
await box.putAsync(constructed, mode: PutMode.put);

if (runCallbacks) afterCommit(record.id);
return record;
}

@override
Future<T?> set(
T record, {
Expand All @@ -101,13 +115,17 @@ abstract class BaseBox<B extends BaseObjectBox, T extends BaseDbModel> extends B
}

@override
Future<void> setAll(List<T> records) async {
Future<void> setAll(
List<T> records, {
bool runCallbacks = true,
}) async {
List<B> objects = await modelsToObjects(records.whereType<T>().toList());

for (B obj in objects) {
obj.setDeviceId();
}

if (runCallbacks) afterCommit();
await box.putManyAsync(objects, mode: PutMode.put);
}

Expand Down
17 changes: 17 additions & 0 deletions lib/core/databases/adapters/objectbox/entities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ abstract class BaseObjectBox<T> {
lastSavedDeviceId = kDeviceInfo.id;
}

void touch();

DateTime? permanentlyDeletedAt;
String? lastSavedDeviceId;
}
Expand Down Expand Up @@ -82,6 +84,11 @@ class StoryObjectBox extends BaseObjectBox {
updatedAt = DateTime.now();
permanentlyDeletedAt = DateTime.now();
}

@override
void touch() {
updatedAt = DateTime.now();
}
}

@Entity()
Expand Down Expand Up @@ -126,6 +133,11 @@ class TagObjectBox extends BaseObjectBox {
updatedAt = DateTime.now();
permanentlyDeletedAt = DateTime.now();
}

@override
void touch() {
updatedAt = DateTime.now();
}
}

@Entity()
Expand Down Expand Up @@ -163,4 +175,9 @@ class PreferenceObjectBox extends BaseObjectBox {
updatedAt = DateTime.now();
permanentlyDeletedAt = DateTime.now();
}

@override
void touch() {
updatedAt = DateTime.now();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class BaseSqliteDbAdapter extends BaseDbAdapter {
throw UnimplementedError();
}

@override
Future<BaseDbModel?> touch(BaseDbModel record) {
throw UnimplementedError();
}

@override
Future<BaseDbModel?> set(BaseDbModel record) {
throw UnimplementedError();
Expand Down
6 changes: 6 additions & 0 deletions lib/core/services/restore_backup_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ class RestoreBackupService {
if (existingRecord != null) {
if (existingRecord.updatedAt != null && newRecord.updatedAt != null) {
bool newContent = existingRecord.updatedAt!.isBefore(newRecord.updatedAt!);
bool unSyncContent = existingRecord.updatedAt!.isBefore(newRecord.updatedAt!);

if (newContent) {
await db.set(newRecord);
} else if (unSyncContent) {
// Update `updatedAt` to mark the record as unsynced, ensuring the backup provider picks it up later.
// This prevents the app from incorrectly assuming the database is fully synced after restoration.
await db.touch(existingRecord);
}
}
} else {
Expand Down

0 comments on commit efee8f2

Please sign in to comment.