refactor: improve sync merging categories (#1559)

* feat: Add versioning to categories

* feat: use random UID for categories.

For legacy and migration we should assign uid on insert, and modify existing one as well in the migration.

* feat: sync category metadata

Add version, uid and lastModifiedAt fields to Category model to allow syncing.

* chore: fix category merging logic

Improve the category merging logic by matching using UIDs first, with a fallback to matching by name for legacy remote categories.

Previously, categories were only matched by name, which could lead to incorrect merges if names were changed. This change ensures more accurate synchronization by prioritizing the unique identifier. Conflict resolution is now based on the `version` field, and logging has been added for better visibility into the merging process.

* refactor: prioritize UID when restoring categories

If a category with the same UID exists, update it instead of creating a new one. Fallback to matching by name if no UID match is found.

* chore: add isSyncing flag like before.

This make sure the version is consistent, and it's not accidentally appended by the trigger, if it does then one device will always be ahead, than previous, and they need to make multiple changes to increase the version.

* Apply suggestion from @jobobby04

Use SY specific numbers(601, 602 for now)

Co-authored-by: jobobby04 <jobobby04@users.noreply.github.com>

* chore: commit review, re-order.

* chore: surround changes in // SY --> // SY <--

* refactor: fallback to existing category UID if backup UID is 0 during restore.

when dealing with old backups (backups created before we added UIDs). In those old backups, backupCategory.uid defaults to 0.
If a user restored an old backup, it would match by name, and then overwrite the newly generated local UID with 0. This would break the synchronization.

* refactor: change to 6xx

* feat: improve sync reliability for categories and settings

- Refactor `mergeCategoriesLists` to correctly match categories by name when UID matching fails, ensuring better reconciliation across devices.
- Fix a bug in category merging where multiple categories with UID 0 (common for non-synced items) caused data loss.
- Update `SyncManager` to detect changes in categories, sources, preferences, saved searches, and extension repos, ensuring they synchronize even when the library favorites haven't changed.
- Convert `BackupCategory` and `BackupExtensionRepos` to data classes to support robust content-aware comparison during the sync process.
- Fix data loss in `mergeSourcesLists`, `mergePreferencesLists`, and `mergeSavedSearchesLists` by retaining local versions when conflicting with remote data.

* fix(sync): properly sync category deletions across devices

Previously, the sync system could not distinguish between a category that was deleted locally and a new category created on another device, causing deleted categories to be restored from the remote backup.

- Update `SyncService` to use `lastSyncTimestamp` to deduce if a missing local category was deleted (if modified before last sync) or newly created remotely (if modified after).
- Update `SyncManager` to explicitly delete local categories that are absent from the merged remote backup, propagating deletions to other devices.
- Fix `RestoreOptions` in `SyncManager` to respect the user's sync preferences instead of hardcoding `categories = true`.

* chore: change it to 6xx and not 600.

* chore: don't need to change this.

* chore: use kotlin time duration units

---------

Co-authored-by: jobobby04 <jobobby04@users.noreply.github.com>
This commit is contained in:
KaiserBh
2026-04-04 03:49:37 +11:00
committed by GitHub
parent fe0b14ab97
commit ba75395648
11 changed files with 252 additions and 47 deletions
@@ -8,12 +8,18 @@ object CategoryMapper {
name: String,
order: Long,
flags: Long,
version: Long,
uid: Long,
lastModifiedAt: Long,
): Category {
return Category(
id = id,
name = name,
order = order,
flags = flags,
version = version,
uid = uid,
lastModifiedAt = lastModifiedAt,
)
}
}
@@ -42,6 +42,9 @@ class CategoryRepositoryImpl(
name = category.name,
order = category.order,
flags = category.flags,
version = category.version,
uid = category.uid,
last_modified_at = category.lastModifiedAt,
)
categoriesQueries.selectLastInsertedRowId()
}
@@ -67,6 +70,10 @@ class CategoryRepositoryImpl(
name = update.name,
order = update.order,
flags = update.flags,
version = update.version,
uid = update.uid,
last_modified_at = update.lastModifiedAt,
isSyncing = null,
categoryId = update.id,
)
}
@@ -6,11 +6,15 @@ CREATE TABLE categories(
name TEXT NOT NULL,
sort INTEGER NOT NULL,
flags INTEGER NOT NULL,
manga_order TEXT AS List<Long> NOT NULL
manga_order TEXT AS List<Long> NOT NULL,
version INTEGER NOT NULL DEFAULT 0,
uid INTEGER NOT NULL DEFAULT 0,
last_modified_at INTEGER NOT NULL DEFAULT 0,
is_syncing INTEGER NOT NULL DEFAULT 0
);
-- Insert system category
INSERT OR IGNORE INTO categories(_id, name, sort, flags, manga_order) VALUES (0, "", -1, 0, "");
INSERT OR IGNORE INTO categories(_id, name, sort, flags, manga_order, version, uid, last_modified_at, is_syncing) VALUES (0, "", -1, 0, "", 0, 0, 0, 0);
-- Disallow deletion of default category
CREATE TRIGGER IF NOT EXISTS system_category_delete_trigger BEFORE DELETE
ON categories
@@ -20,8 +24,29 @@ BEGIN SELECT CASE
END;
END;
CREATE TRIGGER update_category_version AFTER UPDATE ON categories
WHEN new.is_syncing = 0 AND (
new.name != old.name OR
new.sort != old.sort OR
new.flags != old.flags
)
BEGIN
UPDATE categories
SET version = version + 1,
last_modified_at = strftime('%s', 'now')
WHERE _id = new._id;
END;
CREATE TRIGGER insert_category_uid AFTER INSERT ON categories
BEGIN
UPDATE categories
SET uid = CASE WHEN uid = 0 THEN abs(random()) ELSE uid END,
last_modified_at = CASE WHEN last_modified_at = 0 THEN strftime('%s', 'now') ELSE last_modified_at END
WHERE _id = new._id;
END;
getCategory:
SELECT _id,name,sort,flags
SELECT _id,name,sort,flags,version,uid,last_modified_at
FROM categories
WHERE _id = :id
LIMIT 1;
@@ -31,7 +56,10 @@ SELECT
_id AS id,
name,
sort AS `order`,
flags
flags,
version,
uid,
last_modified_at
FROM categories
ORDER BY sort;
@@ -40,15 +68,18 @@ SELECT
C._id AS id,
C.name,
C.sort AS `order`,
C.flags
C.flags,
C.version,
C.uid,
C.last_modified_at
FROM categories C
JOIN mangas_categories MC
ON C._id = MC.category_id
WHERE MC.manga_id = :mangaId;
insert:
INSERT INTO categories(name, sort, flags, manga_order)
VALUES (:name, :order, :flags, "");
INSERT INTO categories(name, sort, flags, manga_order, version, uid, last_modified_at)
VALUES (:name, :order, :flags, "", :version, :uid, :last_modified_at);
delete:
DELETE FROM categories
@@ -58,7 +89,11 @@ update:
UPDATE categories
SET name = coalesce(:name, name),
sort = coalesce(:order, sort),
flags = coalesce(:flags, flags)
flags = coalesce(:flags, flags),
version = coalesce(:version, version),
uid = coalesce(:uid, uid),
last_modified_at = coalesce(:last_modified_at, last_modified_at),
is_syncing = coalesce(:isSyncing, is_syncing)
WHERE _id = :categoryId;
updateAllFlags:
@@ -66,4 +101,9 @@ UPDATE categories SET
flags = coalesce(?, flags);
selectLastInsertedRowId:
SELECT last_insert_rowid();
SELECT last_insert_rowid();
resetIsSyncing:
UPDATE categories
SET is_syncing = 0
WHERE is_syncing = 1;
@@ -0,0 +1,27 @@
ALTER TABLE categories ADD COLUMN version INTEGER NOT NULL DEFAULT 0;
ALTER TABLE categories ADD COLUMN uid INTEGER NOT NULL DEFAULT 0;
ALTER TABLE categories ADD COLUMN last_modified_at INTEGER NOT NULL DEFAULT 0;
ALTER TABLE categories ADD COLUMN is_syncing INTEGER NOT NULL DEFAULT 0;
UPDATE categories SET uid = abs(random());
CREATE TRIGGER insert_category_uid AFTER INSERT ON categories
BEGIN
UPDATE categories
SET uid = CASE WHEN uid = 0 THEN abs(random()) ELSE uid END,
last_modified_at = CASE WHEN last_modified_at = 0 THEN strftime('%s', 'now') ELSE last_modified_at END
WHERE _id = new._id;
END;
CREATE TRIGGER update_category_version AFTER UPDATE ON categories
WHEN new.is_syncing = 0 AND (
new.name != old.name OR
new.sort != old.sort OR
new.flags != old.flags
)
BEGIN
UPDATE categories
SET version = version + 1,
last_modified_at = strftime('%s', 'now')
WHERE _id = new._id;
END;