Keep the stored row when a measurement category id is already taken

This commit is contained in:
Roland Geider
2026-08-29 14:44:44 +02:00
parent 17395206f1
commit 7b0bf9b2b8
2 changed files with 74 additions and 3 deletions

View File

@@ -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<void> 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<void> addLocalDriftCategoryGroup(List<MeasurementCategory> 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<void> _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.

View File

@@ -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(