Keep pre-2.7 measurement rows readable

This commit is contained in:
Roland Geider
2026-08-09 18:42:49 +02:00
parent bb7ed8fa23
commit df2791a1f7
8 changed files with 190 additions and 65 deletions

View File

@@ -4709,15 +4709,15 @@ class $MeasurementCategoryTableTable extends MeasurementCategoryTable
requiredDuringInsert: true,
);
@override
late final GeneratedColumnWithTypeConverter<MetricType, String> metricType =
late final GeneratedColumnWithTypeConverter<MetricType?, String> metricType =
GeneratedColumn<String>(
'metric_type',
aliasedName,
false,
true,
type: DriftSqlType.string,
requiredDuringInsert: true,
).withConverter<MetricType>(
$MeasurementCategoryTableTable.$convertermetricType,
requiredDuringInsert: false,
).withConverter<MetricType?>(
$MeasurementCategoryTableTable.$convertermetricTypen,
);
@override
late final GeneratedColumnWithTypeConverter<ChartType, String> chartType =
@@ -4760,7 +4760,7 @@ class $MeasurementCategoryTableTable extends MeasurementCategoryTable
late final GeneratedColumn<int> order = GeneratedColumn<int>(
'order',
aliasedName,
false,
true,
type: DriftSqlType.int,
requiredDuringInsert: false,
defaultValue: const Constant(0),
@@ -4772,7 +4772,7 @@ class $MeasurementCategoryTableTable extends MeasurementCategoryTable
late final GeneratedColumn<bool> isOfficial = GeneratedColumn<bool>(
'is_official',
aliasedName,
false,
true,
type: DriftSqlType.bool,
requiredDuringInsert: false,
defaultConstraints: GeneratedColumn.constraintIsAlways(
@@ -4849,7 +4849,7 @@ class $MeasurementCategoryTableTable extends MeasurementCategoryTable
@override
MeasurementCategory map(Map<String, dynamic> data, {String? tablePrefix}) {
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';
return MeasurementCategory(
return MeasurementCategory.fromDb(
id: attachedDatabase.typeMapping.read(
DriftSqlType.string,
data['${effectivePrefix}id'],
@@ -4862,18 +4862,18 @@ class $MeasurementCategoryTableTable extends MeasurementCategoryTable
DriftSqlType.string,
data['${effectivePrefix}unit'],
)!,
metricType: $MeasurementCategoryTableTable.$convertermetricType.fromSql(
attachedDatabase.typeMapping.read(
DriftSqlType.string,
data['${effectivePrefix}metric_type'],
)!,
),
chartType: $MeasurementCategoryTableTable.$converterchartType.fromSql(
attachedDatabase.typeMapping.read(
DriftSqlType.string,
data['${effectivePrefix}chart_type'],
),
),
metricType: $MeasurementCategoryTableTable.$convertermetricTypen.fromSql(
attachedDatabase.typeMapping.read(
DriftSqlType.string,
data['${effectivePrefix}metric_type'],
),
),
chartConfig: $MeasurementCategoryTableTable.$converterchartConfign.fromSql(
attachedDatabase.typeMapping.read(
DriftSqlType.string,
@@ -4887,11 +4887,11 @@ class $MeasurementCategoryTableTable extends MeasurementCategoryTable
order: attachedDatabase.typeMapping.read(
DriftSqlType.int,
data['${effectivePrefix}order'],
)!,
),
isOfficial: attachedDatabase.typeMapping.read(
DriftSqlType.bool,
data['${effectivePrefix}is_official'],
)!,
),
);
}
@@ -4902,6 +4902,9 @@ class $MeasurementCategoryTableTable extends MeasurementCategoryTable
static TypeConverter<MetricType, String> $convertermetricType =
const MeasurementMetricTypeConverter();
static TypeConverter<MetricType?, String?> $convertermetricTypen = NullAwareTypeConverter.wrap(
$convertermetricType,
);
static TypeConverter<ChartType, String?> $converterchartType =
const MeasurementChartTypeConverter();
static TypeConverter<Map<String, dynamic>, String> $converterchartConfig =
@@ -4914,12 +4917,12 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion<MeasurementCateg
final Value<String> id;
final Value<String> name;
final Value<String> unit;
final Value<MetricType> metricType;
final Value<MetricType?> metricType;
final Value<ChartType> chartType;
final Value<Map<String, dynamic>?> chartConfig;
final Value<String?> parentId;
final Value<int> order;
final Value<bool> isOfficial;
final Value<int?> order;
final Value<bool?> isOfficial;
final Value<int> rowid;
const MeasurementCategoryTableCompanion({
this.id = const Value.absent(),
@@ -4937,7 +4940,7 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion<MeasurementCateg
this.id = const Value.absent(),
required String name,
required String unit,
required MetricType metricType,
this.metricType = const Value.absent(),
this.chartType = const Value.absent(),
this.chartConfig = const Value.absent(),
this.parentId = const Value.absent(),
@@ -4945,8 +4948,7 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion<MeasurementCateg
this.isOfficial = const Value.absent(),
this.rowid = const Value.absent(),
}) : name = Value(name),
unit = Value(unit),
metricType = Value(metricType);
unit = Value(unit);
static Insertable<MeasurementCategory> custom({
Expression<String>? id,
Expression<String>? name,
@@ -4977,12 +4979,12 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion<MeasurementCateg
Value<String>? id,
Value<String>? name,
Value<String>? unit,
Value<MetricType>? metricType,
Value<MetricType?>? metricType,
Value<ChartType>? chartType,
Value<Map<String, dynamic>?>? chartConfig,
Value<String?>? parentId,
Value<int>? order,
Value<bool>? isOfficial,
Value<int?>? order,
Value<bool?>? isOfficial,
Value<int>? rowid,
}) {
return MeasurementCategoryTableCompanion(
@@ -5013,7 +5015,7 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion<MeasurementCateg
}
if (metricType.present) {
map['metric_type'] = Variable<String>(
$MeasurementCategoryTableTable.$convertermetricType.toSql(
$MeasurementCategoryTableTable.$convertermetricTypen.toSql(
metricType.value,
),
);
@@ -5126,7 +5128,7 @@ class $MeasurementEntryTableTable extends MeasurementEntryTable
late final GeneratedColumn<String> source = GeneratedColumn<String>(
'source',
aliasedName,
false,
true,
type: DriftSqlType.string,
requiredDuringInsert: false,
defaultValue: const Constant('user'),
@@ -5223,7 +5225,7 @@ class $MeasurementEntryTableTable extends MeasurementEntryTable
@override
MeasurementEntry map(Map<String, dynamic> data, {String? tablePrefix}) {
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';
return MeasurementEntry(
return MeasurementEntry.fromDb(
id: attachedDatabase.typeMapping.read(
DriftSqlType.string,
data['${effectivePrefix}id'],
@@ -5249,7 +5251,7 @@ class $MeasurementEntryTableTable extends MeasurementEntryTable
source: attachedDatabase.typeMapping.read(
DriftSqlType.string,
data['${effectivePrefix}source'],
)!,
),
externalId: attachedDatabase.typeMapping.read(
DriftSqlType.string,
data['${effectivePrefix}external_id'],
@@ -5280,7 +5282,7 @@ class MeasurementEntryTableCompanion extends UpdateCompanion<MeasurementEntry> {
final Value<DateTime> date;
final Value<double> value;
final Value<String> notes;
final Value<String> source;
final Value<String?> source;
final Value<String?> externalId;
final Value<Map<String, dynamic>?> extraData;
final Value<int> rowid;
@@ -5339,7 +5341,7 @@ class MeasurementEntryTableCompanion extends UpdateCompanion<MeasurementEntry> {
Value<DateTime>? date,
Value<double>? value,
Value<String>? notes,
Value<String>? source,
Value<String?>? source,
Value<String?>? externalId,
Value<Map<String, dynamic>?>? extraData,
Value<int>? rowid,
@@ -16349,12 +16351,12 @@ typedef $$MeasurementCategoryTableTableCreateCompanionBuilder =
Value<String> id,
required String name,
required String unit,
required MetricType metricType,
Value<MetricType?> metricType,
Value<ChartType> chartType,
Value<Map<String, dynamic>?> chartConfig,
Value<String?> parentId,
Value<int> order,
Value<bool> isOfficial,
Value<int?> order,
Value<bool?> isOfficial,
Value<int> rowid,
});
typedef $$MeasurementCategoryTableTableUpdateCompanionBuilder =
@@ -16362,12 +16364,12 @@ typedef $$MeasurementCategoryTableTableUpdateCompanionBuilder =
Value<String> id,
Value<String> name,
Value<String> unit,
Value<MetricType> metricType,
Value<MetricType?> metricType,
Value<ChartType> chartType,
Value<Map<String, dynamic>?> chartConfig,
Value<String?> parentId,
Value<int> order,
Value<bool> isOfficial,
Value<int?> order,
Value<bool?> isOfficial,
Value<int> rowid,
});
@@ -16449,7 +16451,7 @@ class $$MeasurementCategoryTableTableFilterComposer
builder: (column) => ColumnFilters(column),
);
ColumnWithTypeConverterFilters<MetricType, MetricType, String> get metricType =>
ColumnWithTypeConverterFilters<MetricType?, MetricType, String> get metricType =>
$composableBuilder(
column: $table.metricType,
builder: (column) => ColumnWithTypeConverterFilters(column),
@@ -16613,7 +16615,7 @@ class $$MeasurementCategoryTableTableAnnotationComposer
GeneratedColumn<String> get unit =>
$composableBuilder(column: $table.unit, builder: (column) => column);
GeneratedColumnWithTypeConverter<MetricType, String> get metricType => $composableBuilder(
GeneratedColumnWithTypeConverter<MetricType?, String> get metricType => $composableBuilder(
column: $table.metricType,
builder: (column) => column,
);
@@ -16724,12 +16726,12 @@ class $$MeasurementCategoryTableTableTableManager
Value<String> id = const Value.absent(),
Value<String> name = const Value.absent(),
Value<String> unit = const Value.absent(),
Value<MetricType> metricType = const Value.absent(),
Value<MetricType?> metricType = const Value.absent(),
Value<ChartType> chartType = const Value.absent(),
Value<Map<String, dynamic>?> chartConfig = const Value.absent(),
Value<String?> parentId = const Value.absent(),
Value<int> order = const Value.absent(),
Value<bool> isOfficial = const Value.absent(),
Value<int?> order = const Value.absent(),
Value<bool?> isOfficial = const Value.absent(),
Value<int> rowid = const Value.absent(),
}) => MeasurementCategoryTableCompanion(
id: id,
@@ -16748,12 +16750,12 @@ class $$MeasurementCategoryTableTableTableManager
Value<String> id = const Value.absent(),
required String name,
required String unit,
required MetricType metricType,
Value<MetricType?> metricType = const Value.absent(),
Value<ChartType> chartType = const Value.absent(),
Value<Map<String, dynamic>?> chartConfig = const Value.absent(),
Value<String?> parentId = const Value.absent(),
Value<int> order = const Value.absent(),
Value<bool> isOfficial = const Value.absent(),
Value<int?> order = const Value.absent(),
Value<bool?> isOfficial = const Value.absent(),
Value<int> rowid = const Value.absent(),
}) => MeasurementCategoryTableCompanion.insert(
id: id,
@@ -16864,7 +16866,7 @@ typedef $$MeasurementEntryTableTableCreateCompanionBuilder =
required DateTime date,
required double value,
required String notes,
Value<String> source,
Value<String?> source,
Value<String?> externalId,
Value<Map<String, dynamic>?> extraData,
Value<int> rowid,
@@ -16876,7 +16878,7 @@ typedef $$MeasurementEntryTableTableUpdateCompanionBuilder =
Value<DateTime> date,
Value<double> value,
Value<String> notes,
Value<String> source,
Value<String?> source,
Value<String?> externalId,
Value<Map<String, dynamic>?> extraData,
Value<int> rowid,
@@ -17143,7 +17145,7 @@ class $$MeasurementEntryTableTableTableManager
Value<DateTime> date = const Value.absent(),
Value<double> value = const Value.absent(),
Value<String> notes = const Value.absent(),
Value<String> source = const Value.absent(),
Value<String?> source = const Value.absent(),
Value<String?> externalId = const Value.absent(),
Value<Map<String, dynamic>?> extraData = const Value.absent(),
Value<int> rowid = const Value.absent(),
@@ -17165,7 +17167,7 @@ class $$MeasurementEntryTableTableTableManager
required DateTime date,
required double value,
required String notes,
Value<String> source = const Value.absent(),
Value<String?> source = const Value.absent(),
Value<String?> externalId = const Value.absent(),
Value<Map<String, dynamic>?> extraData = const Value.absent(),
Value<int> rowid = const Value.absent(),

View File

@@ -25,7 +25,7 @@ import 'package:wger/database/converters/utc_datetime_converter.dart';
import 'package:wger/features/measurements/models/measurement_category.dart';
import 'package:wger/features/measurements/models/measurement_entry.dart';
@UseRowClass(MeasurementCategory)
@UseRowClass(MeasurementCategory, constructor: 'fromDb')
class MeasurementCategoryTable extends Table {
@override
String get tableName => 'measurements_category';
@@ -34,8 +34,12 @@ class MeasurementCategoryTable extends Table {
TextColumn get name => text()();
TextColumn get unit => text()();
// The new 2.7 columns (metricType, order, isOfficial, source) are nullable:
// rows synced before the schema change lack the keys in their local ps_data
// and read as NULL until the full re-sync replaces them. The `fromDb`
// constructors map NULL to the defaults so the UI keeps working meanwhile.
TextColumn get metricType =>
text().named('metric_type').map(const MeasurementMetricTypeConverter())();
text().named('metric_type').map(const MeasurementMetricTypeConverter()).nullable()();
/// Chart the user picked, NULL for the one derived from the metric type.
TextColumn get chartType =>
@@ -52,11 +56,12 @@ class MeasurementCategoryTable extends Table {
text().named('parent_id').nullable().references(MeasurementCategoryTable, #id)();
/// Position in the category list; for children, the position within the group
IntColumn get order => integer().withDefault(const Constant(0))();
IntColumn get order => integer().withDefault(const Constant(0)).nullable()();
/// Server-managed official category (max. one per metric type and user).
/// Body weight entries live in the official `body_weight` category.
BoolColumn get isOfficial => boolean().named('is_official').withDefault(const Constant(false))();
BoolColumn get isOfficial =>
boolean().named('is_official').withDefault(const Constant(false)).nullable()();
}
const PowersyncMeasurementCategoryTable = ps.Table(
@@ -78,7 +83,7 @@ const PowersyncMeasurementCategoryTable = ps.Table(
],
);
@UseRowClass(MeasurementEntry)
@UseRowClass(MeasurementEntry, constructor: 'fromDb')
class MeasurementEntryTable extends Table {
@override
String get tableName => 'measurements_measurement';
@@ -93,7 +98,7 @@ class MeasurementEntryTable extends Table {
/// Where the reading came from; one of the server's `source` values:
/// `user` for manual entries or a health platform (`apple`, `google`).
TextColumn get source => text().withDefault(const Constant('user'))();
TextColumn get source => text().withDefault(const Constant('user')).nullable()();
/// Platform record UUID, used to deduplicate re-imports. `null` for manual
/// entries.

View File

@@ -573,6 +573,31 @@ class MeasurementCategory with _$MeasurementCategory {
this.children = const [],
});
/// 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.
MeasurementCategory.fromDb({
required String id,
required String name,
required String unit,
required ChartType chartType,
MetricType? metricType,
Map<String, dynamic>? chartConfig,
String? parentId,
int? order,
bool? isOfficial,
}) : this(
id: id,
name: name,
unit: unit,
metricType: metricType ?? MetricType.custom,
chartType: chartType,
chartConfig: chartConfig,
parentId: parentId,
order: order ?? 0,
isOfficial: isOfficial ?? false,
);
/// How this category is drawn, beyond the chart type: see [ChartSettings].
ChartSettings get chartSettings => ChartSettings.fromConfig(chartConfig);

View File

@@ -68,6 +68,29 @@ class MeasurementEntry with _$MeasurementEntry {
this.extraData,
});
/// 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.
MeasurementEntry.fromDb({
required String id,
required String categoryId,
required DateTime date,
required double value,
required String notes,
String? source,
String? externalId,
Map<String, dynamic>? extraData,
}) : this(
id: id,
categoryId: categoryId,
date: date,
value: value,
notes: notes,
source: source ?? 'user',
externalId: externalId,
extraData: extraData,
);
// Boilerplate
MeasurementEntryTableCompanion toCompanion() {
return MeasurementEntryTableCompanion(

View File

@@ -10,30 +10,33 @@ part of 'chart_range_setting.dart';
// ignore_for_file: type=lint, type=warning
/// The time range the measurement charts cover, shared by the overview, the
/// category detail screens and the weight screen: a pick follows the user
/// through them instead of every screen starting over at its own default.
/// through them instead of every screen starting over at its own default,
/// and it is persisted
///
/// keepAlive on purpose: autoDispose would reset the pick whenever no screen
/// is listening for a moment (a tab switch), which is exactly the surprise
/// this provider removes. It holds for the session; a restart starts over.
/// this provider removes.
@ProviderFor(ChartRangeSetting)
final chartRangeSettingProvider = ChartRangeSettingProvider._();
/// The time range the measurement charts cover, shared by the overview, the
/// category detail screens and the weight screen: a pick follows the user
/// through them instead of every screen starting over at its own default.
/// through them instead of every screen starting over at its own default,
/// and it is persisted
///
/// keepAlive on purpose: autoDispose would reset the pick whenever no screen
/// is listening for a moment (a tab switch), which is exactly the surprise
/// this provider removes. It holds for the session; a restart starts over.
/// this provider removes.
final class ChartRangeSettingProvider extends $NotifierProvider<ChartRangeSetting, ChartRange> {
/// The time range the measurement charts cover, shared by the overview, the
/// category detail screens and the weight screen: a pick follows the user
/// through them instead of every screen starting over at its own default.
/// through them instead of every screen starting over at its own default,
/// and it is persisted
///
/// keepAlive on purpose: autoDispose would reset the pick whenever no screen
/// is listening for a moment (a tab switch), which is exactly the surprise
/// this provider removes. It holds for the session; a restart starts over.
/// this provider removes.
ChartRangeSettingProvider._()
: super(
from: null,
@@ -61,15 +64,16 @@ final class ChartRangeSettingProvider extends $NotifierProvider<ChartRangeSettin
}
}
String _$chartRangeSettingHash() => r'ca0074ed6745f437866cdddccf93eec3b6e1dd7b';
String _$chartRangeSettingHash() => r'1baa3a7e0c1a7643fa93631c51f8f3672b54e30f';
/// The time range the measurement charts cover, shared by the overview, the
/// category detail screens and the weight screen: a pick follows the user
/// through them instead of every screen starting over at its own default.
/// through them instead of every screen starting over at its own default,
/// and it is persisted
///
/// keepAlive on purpose: autoDispose would reset the pick whenever no screen
/// is listening for a moment (a tab switch), which is exactly the surprise
/// this provider removes. It holds for the session; a restart starts over.
/// this provider removes.
abstract class _$ChartRangeSetting extends $Notifier<ChartRange> {
ChartRange build();

View File

@@ -575,7 +575,8 @@ class MeasurementRepository {
await _db.transaction(() async {
for (var i = 0; i < orderedIds.length; i++) {
final stmt = _db.update(_db.measurementCategoryTable)
..where((t) => t.id.equals(orderedIds[i]) & t.order.equals(i).not());
// IS NOT so legacy rows with a NULL order still get written
..where((t) => t.id.equals(orderedIds[i]) & t.order.isNotValue(i));
await stmt.write(MeasurementCategoryTableCompanion(order: Value(i)));
}
});

View File

@@ -5490,6 +5490,50 @@ class MockAppLocalizations extends _i1.Mock implements _i2.AppLocalizations {
)
as String);
@override
String relativeDaysAgo(int? count) =>
(super.noSuchMethod(
Invocation.method(#relativeDaysAgo, [count]),
returnValue: _i3.dummyValue<String>(
this,
Invocation.method(#relativeDaysAgo, [count]),
),
)
as String);
@override
String relativeWeeksAgo(int? count) =>
(super.noSuchMethod(
Invocation.method(#relativeWeeksAgo, [count]),
returnValue: _i3.dummyValue<String>(
this,
Invocation.method(#relativeWeeksAgo, [count]),
),
)
as String);
@override
String relativeMonthsAgo(int? count) =>
(super.noSuchMethod(
Invocation.method(#relativeMonthsAgo, [count]),
returnValue: _i3.dummyValue<String>(
this,
Invocation.method(#relativeMonthsAgo, [count]),
),
)
as String);
@override
String relativeYearsAgo(int? count) =>
(super.noSuchMethod(
Invocation.method(#relativeYearsAgo, [count]),
returnValue: _i3.dummyValue<String>(
this,
Invocation.method(#relativeYearsAgo, [count]),
),
)
as String);
@override
String sparkAverage(String? value) =>
(super.noSuchMethod(

View File

@@ -218,6 +218,27 @@ void main() {
(await repo.watchAllWithoutEntries().first).map((c) => c.id),
);
});
test('maps the NULL columns of a pre-2.7 row to the defaults', () async {
// A row synced before the 2.7 schema change carries no value for the new
// columns and reads NULL until a re-sync replaces it
await db.customStatement(
'INSERT INTO measurements_category (id, name, unit, metric_type, "order", is_official) '
"VALUES ('legacy', 'Old category', 'cm', NULL, NULL, NULL)",
);
await db.customStatement(
'INSERT INTO measurements_measurement (id, category_id, date, value, notes, source) '
"VALUES ('legacy-entry', 'legacy', '2026-01-01T00:00:00.000Z', 42, '', NULL)",
);
final category = (await repo.watchAllWithoutEntries().first).single;
expect(category.metricType, MetricType.custom);
expect(category.order, 0);
expect(category.isOfficial, isFalse);
final entry = (await repo.watchEntries('legacy', limit: 10).first).single;
expect(entry.source, 'user');
});
});
group('the import dedup queries', () {