mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-02 10:09:11 +01:00
Keep the stored row when a measurement category id is already taken
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user