From 7b0bf9b2b83bb6c22ee35d48693bc723a21b8e7f Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Sat, 29 Aug 2026 14:44:44 +0200 Subject: [PATCH] Keep the stored row when a measurement category id is already taken --- .../providers/measurement_repository.dart | 27 ++++++++-- .../measurement_repository_test.dart | 50 +++++++++++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/lib/features/measurements/providers/measurement_repository.dart b/lib/features/measurements/providers/measurement_repository.dart index da8afaa28..9f536d305 100644 --- a/lib/features/measurements/providers/measurement_repository.dart +++ b/lib/features/measurements/providers/measurement_repository.dart @@ -568,22 +568,43 @@ class MeasurementRepository { await stmt.write(category.toCompanion().copyWith(metricType: const Value.absent())); } + /// Adds [category], keeping the stored row when its id is already taken. + /// + /// A typed category carries a derived id, so a row already holding it is + /// that same category, created on another device and synced in meanwhile. Future addLocalDriftCategory(MeasurementCategory category) async { _logger.finer('Adding local measurement category ${category.name}'); - await _db.into(_db.measurementCategoryTable).insert(category.toCompanion()); + await _db.transaction(() => _insertUnlessStored(category)); } /// Inserts a group together with its components, in a single transaction so - /// a group is never left without the children its readings live in. + /// a group is never left without the children its readings live in. Members + /// the database already holds keep their stored row, see + /// [addLocalDriftCategory]. Future addLocalDriftCategoryGroup(List categories) async { _logger.finer('Adding a local measurement group of ${categories.length} categories'); await _db.transaction(() async { for (final category in categories) { - await _db.into(_db.measurementCategoryTable).insert(category.toCompanion()); + await _insertUnlessStored(category); } }); } + /// Inserts [category] unless its id is taken, in which case the stored row + /// stands. Runs inside the caller's transaction, so the sync cannot insert + /// between the check and the insert. + Future _insertUnlessStored(MeasurementCategory category) async { + final id = category.id; + if (id != null) { + final query = _db.select(_db.measurementCategoryTable)..where((t) => t.id.equals(id)); + if (await query.getSingleOrNull() != null) { + _logger.info('Measurement category $id is already stored, keeping that row'); + return; + } + } + await _db.into(_db.measurementCategoryTable).insert(category.toCompanion()); + } + /// Persists the given display order: each category gets its list index as /// [MeasurementCategory.order]. Categories whose order is unchanged are not /// written, so no sync upload is queued for them. diff --git a/test/features/measurements/providers/measurement_repository_test.dart b/test/features/measurements/providers/measurement_repository_test.dart index 0752009ae..d920d8346 100644 --- a/test/features/measurements/providers/measurement_repository_test.dart +++ b/test/features/measurements/providers/measurement_repository_test.dart @@ -933,6 +933,56 @@ void main() { expect(emitted.single.isOfficial, isFalse); }); + test('addLocalDriftCategory keeps the row a stored id already has', () async { + await repo.addLocalDriftCategory( + MeasurementCategory(id: 'steps', name: 'Renamed by the user', unit: 'x'), + ); + + // Same derived id, as another device would send it down + await repo.addLocalDriftCategory( + MeasurementCategory( + id: 'steps', + name: 'Steps', + unit: '', + metricType: MetricType.steps, + ), + ); + + final emitted = await repo.watchAllWithoutEntries().first; + expect(emitted, hasLength(1)); + expect(emitted.single.name, 'Renamed by the user'); + }); + + test('addLocalDriftCategoryGroup only inserts the members that are missing', () async { + await repo.addLocalDriftCategory( + MeasurementCategory( + id: 'sleep', + name: 'Sleep', + unit: 'min', + metricType: MetricType.sleep, + ), + ); + + await repo.addLocalDriftCategoryGroup([ + MeasurementCategory( + id: 'sleep', + name: 'Sleep', + unit: 'min', + metricType: MetricType.sleep, + ), + MeasurementCategory( + id: 'sleep-deep', + name: 'Deep sleep', + unit: 'min', + metricType: MetricType.sleepDeep, + parentId: 'sleep', + ), + ]); + + final emitted = await repo.watchAllWithoutEntries().first; + expect(emitted.map((c) => c.id), unorderedEquals(['sleep', 'sleep-deep'])); + }); + test('isOfficial and metricType round-trip through the table', () async { await repo.addLocalDriftCategory( MeasurementCategory(