Create measurement groups through the transactional repository helper

This commit is contained in:
Roland Geider
2026-08-25 13:58:16 +02:00
parent a2a601951c
commit 455e5fd85c
4 changed files with 113 additions and 42 deletions

View File

@@ -811,12 +811,7 @@ class HealthImporter {
return existing;
}
final category = MeasurementCategory(
id: deterministicCategoryId(userId, metric.metricType),
name: metric.canonicalName,
unit: metric.unit,
metricType: metric.metricType,
);
final category = MeasurementCategory.forMetricType(userId, metric.metricType);
await _measurements.addLocalDriftCategory(category);
categories.add(category);
return category;
@@ -843,18 +838,10 @@ class HealthImporter {
return null;
}
final parent =
existing ??
MeasurementCategory(
id: deterministicCategoryId(userId, metric.metricType),
name: metric.canonicalName,
unit: metric.unit,
metricType: metric.metricType,
);
if (existing == null) {
await _measurements.addLocalDriftCategory(parent);
categories.add(parent);
}
final parent = existing ?? MeasurementCategory.forMetricType(userId, metric.metricType);
// Collected and written in one transaction, so a group is never left
// without the children its readings live in
final toCreate = <MeasurementCategory>[if (existing == null) parent];
// The component categories come from the metric type, in the same order as
// the metric's health data types, so the caller can pair them by index.
@@ -868,18 +855,19 @@ class HealthImporter {
result.add(existingChild);
continue;
}
final child = MeasurementCategory(
id: deterministicCategoryId(userId, metricType),
name: metricType.canonicalName,
unit: metricType.defaultUnit,
metricType: metricType,
final child = MeasurementCategory.forMetricType(
userId,
metricType,
parentId: parent.id,
order: order,
);
await _measurements.addLocalDriftCategory(child);
categories.add(child);
toCreate.add(child);
result.add(child);
}
if (toCreate.isNotEmpty) {
await _measurements.addLocalDriftCategoryGroup(toCreate);
categories.addAll(toCreate);
}
return result;
}
}

View File

@@ -604,6 +604,23 @@ class MeasurementCategory with _$MeasurementCategory {
this.children = const [],
});
/// The typed category of [metricType] for [userId]: the deterministic id
/// the app and the server both derive, with the type's canonical name and
/// unit. [parentId] and [order] place a component in its group.
MeasurementCategory.forMetricType(
String userId,
MetricType metricType, {
String? parentId,
int order = 0,
}) : this(
id: deterministicCategoryId(userId, metricType),
name: metricType.canonicalName,
unit: metricType.defaultUnit,
metricType: metricType,
parentId: parentId,
order: order,
);
/// Maps a row of the local database. Rows synced before the 2.7 schema
/// change lack the new columns and read as NULL; they fall back to the
/// defaults until the full re-sync replaces them.

View File

@@ -189,14 +189,7 @@ final class MeasurementNotifier extends _$MeasurementNotifier {
await _repo.addLocalDriftCategoryGroup([
parent,
for (final (order, metricType) in parent.metricType.components.indexed)
MeasurementCategory(
id: deterministicCategoryId(userId, metricType),
name: metricType.canonicalName,
unit: metricType.defaultUnit,
metricType: metricType,
parentId: parent.id,
order: order,
),
MeasurementCategory.forMetricType(userId, metricType, parentId: parent.id, order: order),
]);
return parent.id;
}

View File

@@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:collection/collection.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:health_bridge/health.dart';
@@ -97,6 +98,7 @@ void main() {
when(measurements.addLocalDrift(any)).thenAnswer((_) async {});
when(measurements.updateLocalDrift(any)).thenAnswer((_) async {});
when(measurements.addLocalDriftCategory(any)).thenAnswer((_) async {});
when(measurements.addLocalDriftCategoryGroup(any)).thenAnswer((_) async {});
when(measurements.getCategoriesOnce()).thenAnswer((_) async => <MeasurementCategory>[]);
when(measurements.getExternalIds(any)).thenAnswer((_) async => <String>{});
when(
@@ -477,9 +479,16 @@ void main() {
final count = await runImport();
expect(count, 4);
final createdCategories = verify(
measurements.addLocalDriftCategory(captureAny),
).captured.cast<MeasurementCategory>();
// Leaf categories are single inserts, a group arrives as one
// transactional write of the parent with its children
final createdCategories = [
...verify(
measurements.addLocalDriftCategory(captureAny),
).captured.cast<MeasurementCategory>(),
...verify(
measurements.addLocalDriftCategoryGroup(captureAny),
).captured.cast<List<MeasurementCategory>>().flattened,
];
expect(
createdCategories.map((c) => c.metricType),
containsAll([MetricType.bodyFat, MetricType.height, MetricType.bloodPressure]),
@@ -1111,8 +1120,8 @@ void main() {
measurements.addLocalDrift(captureAny),
).captured.cast<MeasurementEntry>();
final categories = verify(
measurements.addLocalDriftCategory(captureAny),
).captured.cast<MeasurementCategory>();
measurements.addLocalDriftCategoryGroup(captureAny),
).captured.cast<List<MeasurementCategory>>().flattened;
final typeOf = {for (final c in categories) c.id: c.metricType};
final byType = {for (final e in entries) typeOf[e.categoryId]: e.value};
@@ -1310,6 +1319,7 @@ void main() {
final count = await runImport();
expect(count, 2);
verifyNever(measurements.addLocalDriftCategory(any));
verifyNever(measurements.addLocalDriftCategoryGroup(any));
final entries = verify(
measurements.addLocalDrift(captureAny),
@@ -1318,6 +1328,63 @@ void main() {
expect(entries.firstWhere((e) => e.value == 80).categoryId, 'lower');
});
test('creates a group with its children in one transactional write', () async {
// A throw between single inserts would leave the group without the
// children its readings live in
when(measurements.getCategoriesOnce()).thenAnswer((_) async => <MeasurementCategory>[]);
stubReadings([
HealthReading(
type: HealthDataType.BLOOD_PRESSURE_SYSTOLIC,
value: 120,
date: DateTime(2026, 1, 3),
externalId: _idBp1,
),
]);
await runImport();
final written =
verify(
measurements.addLocalDriftCategoryGroup(captureAny),
).captured.single
as List<MeasurementCategory>;
verifyNever(measurements.addLocalDriftCategory(any));
final parent = written.firstWhere((c) => c.metricType == MetricType.bloodPressure);
expect(parent.parentId, isNull);
expect(written.where((c) => c.parentId == parent.id), hasLength(2));
});
test('creates the missing children of an existing group in one write', () async {
// The state a crash between the old single inserts could leave behind
final parent = MeasurementCategory(
id: 'bp',
name: 'Blood pressure',
unit: 'mmHg',
metricType: MetricType.bloodPressure,
);
when(measurements.getCategoriesOnce()).thenAnswer((_) async => [parent]);
stubReadings([
HealthReading(
type: HealthDataType.BLOOD_PRESSURE_SYSTOLIC,
value: 120,
date: DateTime(2026, 1, 3),
externalId: _idBp1,
),
]);
await runImport();
final written =
verify(
measurements.addLocalDriftCategoryGroup(captureAny),
).captured.single
as List<MeasurementCategory>;
verifyNever(measurements.addLocalDriftCategory(any));
// Only the children; the parent already exists
expect(written.map((c) => c.parentId), everyElement('bp'));
expect(written, hasLength(2));
});
test(
'a category with the canonical name is not adopted, only the metric type counts',
() async {
@@ -1377,6 +1444,7 @@ void main() {
// retried once the conflict is resolved
expect(count, 0);
verifyNever(measurements.addLocalDriftCategory(any));
verifyNever(measurements.addLocalDriftCategoryGroup(any));
verifyNever(measurements.addLocalDrift(any));
expect(await watermarkOf(MetricType.bloodPressure), '2020-01-01T00:00:00.000');
});
@@ -1646,7 +1714,12 @@ void main() {
/// The id of the sleep category with [metricType] among the ones the sync
/// created. Sleep is a group, so a run creates the group plus one child per
/// stage; consumes the recorded calls, so call it once per test.
String _sleepCategoryId(MockMeasurementRepository measurements, MetricType metricType) => verify(
measurements.addLocalDriftCategory(captureAny),
).captured.cast<MeasurementCategory>().firstWhere((c) => c.metricType == metricType).id!;
/// stage in one write; consumes the recorded calls, so call it once per test.
String _sleepCategoryId(MockMeasurementRepository measurements, MetricType metricType) =>
verify(
measurements.addLocalDriftCategoryGroup(captureAny),
).captured
.cast<List<MeasurementCategory>>()
.flattened
.firstWhere((c) => c.metricType == metricType)
.id!;