diff --git a/AGENTS.md b/AGENTS.md index e49b79e45..c641970d5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -77,7 +77,7 @@ integration_test/ screenshot driver - **Codegen drift:** if a `*.g.dart` / `*.freezed.dart` is committed without the matching source change (or vice versa), the build is broken. Re-run `build_runner` and stage both. - **`// ignore:` comments:** don't add new ones to silence the analyzer. Fix the underlying issue. If genuinely unfixable, explain why in the same line. - **Drift migrations:** schema bumps need a `MigrationStrategy` that handles existing data. Don't drop and recreate. -- **New columns on PowerSync-synced tables:** there are no client-side migrations; the views regenerate for free, but pre-existing rows keep their old `ps_data` JSON and read NULL for the new column until re-downloaded (which only happens on a sync-rules change or a server-side backfill that touches the rows). So: make the Drift column `.nullable()` even when the domain is non-nullable, map NULL to defaults in a `fromDb` constructor wired via `@UseRowClass(Model, constructor: 'fromDb')` (model fields stay non-null, consumers untouched), and use NULL-safe SQL where the column is queried (`isNotValue(x)`, not `equals(x).not()`). Keep the defaults permanently: installs that skip versions re-open the NULL window until their first full sync. Reference: `lib/database/powersync/tables/measurements.dart` + the measurement models. +- **New columns on PowerSync-synced tables:** there are no client-side migrations; the views regenerate for free, but pre-existing rows keep their old `ps_data` JSON and read NULL for the new column until re-downloaded (which only happens on a sync-rules change or a server-side backfill that touches the rows). So: make the Drift column `.nullable()` even when the domain is non-nullable, map NULL to defaults in a `fromDb` constructor wired via `@UseRowClass(Model, constructor: 'fromDb')` (model fields stay non-null, consumers untouched), and use NULL-safe SQL where the column is queried (`isNotValue(x)`, not `equals(x).not()`). The server side must backfill: a migration that touches the rows (`UPDATE ... SET id = id`) and runs after (or together with) the `AddField`; a touch in an earlier migration doesn't carry a column added later, and without a backfill the rows stay NULL for good, not just until the next sync. With the backfill in a release, the compat layer stays for 3 months after it, tracked in an issue, then goes; an install that skips updates for longer can show errors until its first sync after the update (no data loss, it heals with that sync). Reference: `lib/database/powersync/tables/measurements.dart` + the measurement models. - **Date columns in Drift:** a Django `DateTimeField` column needs `UtcDateTimeConverter`, a `DateField` column needs `DateOnlyTextConverter` (never the other way round). A synced `DateField` must also be listed in `_dateOnlyFields` in `lib/powersync/connector.dart`, or the upload sends a full timestamp that Django rejects. - **PowerSync column types:** the `ps.Column` type must match the Drift column. Bool and int columns need `ps.Column.integer`; behind a `ps.Column.text` a bool reads `'0'` and Dart's `'0' != 0` turns every row `true`. In-memory tests can't catch this. - **REST write, local read:** a REST write reaches the local DB only after the next sync. Don't `getById` the row right after a POST; use the response or a nullable lookup with a fallback. diff --git a/lib/database/powersync/database.g.dart b/lib/database/powersync/database.g.dart index 9d19b8de9..f36caa21c 100644 --- a/lib/database/powersync/database.g.dart +++ b/lib/database/powersync/database.g.dart @@ -4771,15 +4771,15 @@ class $MeasurementCategoryTableTable extends MeasurementCategoryTable requiredDuringInsert: true, ); @override - late final GeneratedColumnWithTypeConverter metricType = + late final GeneratedColumnWithTypeConverter metricType = GeneratedColumn( 'metric_type', aliasedName, - true, + false, type: DriftSqlType.string, - requiredDuringInsert: false, - ).withConverter( - $MeasurementCategoryTableTable.$convertermetricTypen, + requiredDuringInsert: true, + ).withConverter( + $MeasurementCategoryTableTable.$convertermetricType, ); @override late final GeneratedColumnWithTypeConverter chartType = @@ -4822,7 +4822,7 @@ class $MeasurementCategoryTableTable extends MeasurementCategoryTable late final GeneratedColumn order = GeneratedColumn( 'order', aliasedName, - true, + false, type: DriftSqlType.int, requiredDuringInsert: false, defaultValue: const Constant(0), @@ -4958,18 +4958,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, @@ -4983,7 +4983,7 @@ class $MeasurementCategoryTableTable extends MeasurementCategoryTable order: attachedDatabase.typeMapping.read( DriftSqlType.int, data['${effectivePrefix}order'], - ), + )!, isOfficial: attachedDatabase.typeMapping.read( DriftSqlType.bool, data['${effectivePrefix}is_official'], @@ -5008,9 +5008,6 @@ class $MeasurementCategoryTableTable extends MeasurementCategoryTable static TypeConverter $convertermetricType = const MeasurementMetricTypeConverter(); - static TypeConverter $convertermetricTypen = NullAwareTypeConverter.wrap( - $convertermetricType, - ); static TypeConverter $converterchartType = const MeasurementChartTypeConverter(); static TypeConverter, String> $converterchartConfig = @@ -5029,11 +5026,11 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion id; final Value name; final Value unit; - final Value metricType; + final Value metricType; final Value chartType; final Value?> chartConfig; final Value parentId; - final Value order; + final Value order; final Value isOfficial; final Value dynamicType; final Value?> dynamicParams; @@ -5056,7 +5053,7 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion custom({ Expression? id, Expression? name, @@ -5101,11 +5099,11 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion? id, Value? name, Value? unit, - Value? metricType, + Value? metricType, Value? chartType, Value?>? chartConfig, Value? parentId, - Value? order, + Value? order, Value? isOfficial, Value? dynamicType, Value?>? dynamicParams, @@ -5141,7 +5139,7 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion( - $MeasurementCategoryTableTable.$convertermetricTypen.toSql( + $MeasurementCategoryTableTable.$convertermetricType.toSql( metricType.value, ), ); @@ -5266,7 +5264,7 @@ class $MeasurementEntryTableTable extends MeasurementEntryTable late final GeneratedColumn source = GeneratedColumn( 'source', aliasedName, - true, + false, type: DriftSqlType.string, requiredDuringInsert: false, defaultValue: const Constant('user'), @@ -5363,7 +5361,7 @@ class $MeasurementEntryTableTable extends MeasurementEntryTable @override MeasurementEntry map(Map data, {String? tablePrefix}) { final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : ''; - return MeasurementEntry.fromDb( + return MeasurementEntry( id: attachedDatabase.typeMapping.read( DriftSqlType.string, data['${effectivePrefix}id'], @@ -5389,7 +5387,7 @@ class $MeasurementEntryTableTable extends MeasurementEntryTable source: attachedDatabase.typeMapping.read( DriftSqlType.string, data['${effectivePrefix}source'], - ), + )!, externalId: attachedDatabase.typeMapping.read( DriftSqlType.string, data['${effectivePrefix}external_id'], @@ -5420,7 +5418,7 @@ class MeasurementEntryTableCompanion extends UpdateCompanion { final Value date; final Value value; final Value notes; - final Value source; + final Value source; final Value externalId; final Value?> extraData; final Value rowid; @@ -5479,7 +5477,7 @@ class MeasurementEntryTableCompanion extends UpdateCompanion { Value? date, Value? value, Value? notes, - Value? source, + Value? source, Value? externalId, Value?>? extraData, Value? rowid, @@ -6585,15 +6583,15 @@ class $WorkoutSessionTableTable extends WorkoutSessionTable $WorkoutSessionTableTable.$converterimpression, ); @override - late final GeneratedColumnWithTypeConverter datetimeStart = + late final GeneratedColumnWithTypeConverter datetimeStart = GeneratedColumn( 'datetime_start', aliasedName, - true, + false, type: DriftSqlType.string, - requiredDuringInsert: false, - ).withConverter( - $WorkoutSessionTableTable.$converterdatetimeStartn, + requiredDuringInsert: true, + ).withConverter( + $WorkoutSessionTableTable.$converterdatetimeStart, ); @override late final GeneratedColumnWithTypeConverter datetimeEnd = @@ -6657,22 +6655,30 @@ class $WorkoutSessionTableTable extends WorkoutSessionTable @override WorkoutSession map(Map data, {String? tablePrefix}) { final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : ''; - return WorkoutSession.fromDb( + return WorkoutSession( id: attachedDatabase.typeMapping.read( DriftSqlType.string, data['${effectivePrefix}id'], )!, - routineId: attachedDatabase.typeMapping.read( - DriftSqlType.int, - data['${effectivePrefix}routine_id'], - ), dayId: attachedDatabase.typeMapping.read( DriftSqlType.int, data['${effectivePrefix}day_id'], ), - notes: attachedDatabase.typeMapping.read( - DriftSqlType.string, - data['${effectivePrefix}notes'], + routineId: attachedDatabase.typeMapping.read( + DriftSqlType.int, + data['${effectivePrefix}routine_id'], + ), + datetimeStart: $WorkoutSessionTableTable.$converterdatetimeStart.fromSql( + attachedDatabase.typeMapping.read( + DriftSqlType.string, + data['${effectivePrefix}datetime_start'], + )!, + ), + datetimeEnd: $WorkoutSessionTableTable.$converterdatetimeEndn.fromSql( + attachedDatabase.typeMapping.read( + DriftSqlType.string, + data['${effectivePrefix}datetime_end'], + ), ), impression: $WorkoutSessionTableTable.$converterimpression.fromSql( attachedDatabase.typeMapping.read( @@ -6680,17 +6686,9 @@ class $WorkoutSessionTableTable extends WorkoutSessionTable data['${effectivePrefix}impression'], )!, ), - datetimeStart: $WorkoutSessionTableTable.$converterdatetimeStartn.fromSql( - attachedDatabase.typeMapping.read( - DriftSqlType.string, - data['${effectivePrefix}datetime_start'], - ), - ), - datetimeEnd: $WorkoutSessionTableTable.$converterdatetimeEndn.fromSql( - attachedDatabase.typeMapping.read( - DriftSqlType.string, - data['${effectivePrefix}datetime_end'], - ), + notes: attachedDatabase.typeMapping.read( + DriftSqlType.string, + data['${effectivePrefix}notes'], ), ); } @@ -6703,9 +6701,6 @@ class $WorkoutSessionTableTable extends WorkoutSessionTable static TypeConverter $converterimpression = const WorkoutImpressionConverter(); static TypeConverter $converterdatetimeStart = const DateTimeTextConverter(); - static TypeConverter $converterdatetimeStartn = NullAwareTypeConverter.wrap( - $converterdatetimeStart, - ); static TypeConverter $converterdatetimeEnd = const DateTimeTextConverter(); static TypeConverter $converterdatetimeEndn = NullAwareTypeConverter.wrap( $converterdatetimeEnd, @@ -6718,7 +6713,7 @@ class WorkoutSessionTableCompanion extends UpdateCompanion { final Value dayId; final Value notes; final Value impression; - final Value datetimeStart; + final Value datetimeStart; final Value datetimeEnd; final Value rowid; const WorkoutSessionTableCompanion({ @@ -6737,10 +6732,11 @@ class WorkoutSessionTableCompanion extends UpdateCompanion { this.dayId = const Value.absent(), this.notes = const Value.absent(), required WorkoutImpression impression, - this.datetimeStart = const Value.absent(), + required DateTime datetimeStart, this.datetimeEnd = const Value.absent(), this.rowid = const Value.absent(), - }) : impression = Value(impression); + }) : impression = Value(impression), + datetimeStart = Value(datetimeStart); static Insertable custom({ Expression? id, Expression? routineId, @@ -6769,7 +6765,7 @@ class WorkoutSessionTableCompanion extends UpdateCompanion { Value? dayId, Value? notes, Value? impression, - Value? datetimeStart, + Value? datetimeStart, Value? datetimeEnd, Value? rowid, }) { @@ -6807,7 +6803,7 @@ class WorkoutSessionTableCompanion extends UpdateCompanion { } if (datetimeStart.present) { map['datetime_start'] = Variable( - $WorkoutSessionTableTable.$converterdatetimeStartn.toSql( + $WorkoutSessionTableTable.$converterdatetimeStart.toSql( datetimeStart.value, ), ); @@ -16533,11 +16529,11 @@ typedef $$MeasurementCategoryTableTableCreateCompanionBuilder = Value id, required String name, required String unit, - Value metricType, + required MetricType metricType, Value chartType, Value?> chartConfig, Value parentId, - Value order, + Value order, Value isOfficial, Value dynamicType, Value?> dynamicParams, @@ -16548,11 +16544,11 @@ typedef $$MeasurementCategoryTableTableUpdateCompanionBuilder = Value id, Value name, Value unit, - Value metricType, + Value metricType, Value chartType, Value?> chartConfig, Value parentId, - Value order, + Value order, Value isOfficial, Value dynamicType, Value?> dynamicParams, @@ -16637,7 +16633,7 @@ class $$MeasurementCategoryTableTableFilterComposer builder: (column) => ColumnFilters(column), ); - ColumnWithTypeConverterFilters get metricType => + ColumnWithTypeConverterFilters get metricType => $composableBuilder( column: $table.metricType, builder: (column) => ColumnWithTypeConverterFilters(column), @@ -16822,7 +16818,7 @@ class $$MeasurementCategoryTableTableAnnotationComposer GeneratedColumn get unit => $composableBuilder(column: $table.unit, builder: (column) => column); - GeneratedColumnWithTypeConverter get metricType => $composableBuilder( + GeneratedColumnWithTypeConverter get metricType => $composableBuilder( column: $table.metricType, builder: (column) => column, ); @@ -16944,11 +16940,11 @@ class $$MeasurementCategoryTableTableTableManager Value id = const Value.absent(), Value name = const Value.absent(), Value unit = const Value.absent(), - Value metricType = const Value.absent(), + Value metricType = const Value.absent(), Value chartType = const Value.absent(), Value?> chartConfig = const Value.absent(), Value parentId = const Value.absent(), - Value order = const Value.absent(), + Value order = const Value.absent(), Value isOfficial = const Value.absent(), Value dynamicType = const Value.absent(), Value?> dynamicParams = const Value.absent(), @@ -16972,11 +16968,11 @@ class $$MeasurementCategoryTableTableTableManager Value id = const Value.absent(), required String name, required String unit, - Value metricType = const Value.absent(), + required MetricType metricType, Value chartType = const Value.absent(), Value?> chartConfig = const Value.absent(), Value parentId = const Value.absent(), - Value order = const Value.absent(), + Value order = const Value.absent(), Value isOfficial = const Value.absent(), Value dynamicType = const Value.absent(), Value?> dynamicParams = const Value.absent(), @@ -17091,7 +17087,7 @@ typedef $$MeasurementEntryTableTableCreateCompanionBuilder = required DateTime date, required double value, required String notes, - Value source, + Value source, Value externalId, Value?> extraData, Value rowid, @@ -17103,7 +17099,7 @@ typedef $$MeasurementEntryTableTableUpdateCompanionBuilder = Value date, Value value, Value notes, - Value source, + Value source, Value externalId, Value?> extraData, Value rowid, @@ -17370,7 +17366,7 @@ class $$MeasurementEntryTableTableTableManager Value date = const Value.absent(), Value value = const Value.absent(), Value notes = const Value.absent(), - Value source = const Value.absent(), + Value source = const Value.absent(), Value externalId = const Value.absent(), Value?> extraData = const Value.absent(), Value rowid = const Value.absent(), @@ -17392,7 +17388,7 @@ class $$MeasurementEntryTableTableTableManager required DateTime date, required double value, required String notes, - Value source = const Value.absent(), + Value source = const Value.absent(), Value externalId = const Value.absent(), Value?> extraData = const Value.absent(), Value rowid = const Value.absent(), @@ -18186,7 +18182,7 @@ typedef $$WorkoutSessionTableTableCreateCompanionBuilder = Value dayId, Value notes, required WorkoutImpression impression, - Value datetimeStart, + required DateTime datetimeStart, Value datetimeEnd, Value rowid, }); @@ -18197,7 +18193,7 @@ typedef $$WorkoutSessionTableTableUpdateCompanionBuilder = Value dayId, Value notes, Value impression, - Value datetimeStart, + Value datetimeStart, Value datetimeEnd, Value rowid, }); @@ -18237,7 +18233,7 @@ class $$WorkoutSessionTableTableFilterComposer builder: (column) => ColumnWithTypeConverterFilters(column), ); - ColumnWithTypeConverterFilters get datetimeStart => + ColumnWithTypeConverterFilters get datetimeStart => $composableBuilder( column: $table.datetimeStart, builder: (column) => ColumnWithTypeConverterFilters(column), @@ -18320,7 +18316,7 @@ class $$WorkoutSessionTableTableAnnotationComposer builder: (column) => column, ); - GeneratedColumnWithTypeConverter get datetimeStart => $composableBuilder( + GeneratedColumnWithTypeConverter get datetimeStart => $composableBuilder( column: $table.datetimeStart, builder: (column) => column, ); @@ -18373,7 +18369,7 @@ class $$WorkoutSessionTableTableTableManager Value dayId = const Value.absent(), Value notes = const Value.absent(), Value impression = const Value.absent(), - Value datetimeStart = const Value.absent(), + Value datetimeStart = const Value.absent(), Value datetimeEnd = const Value.absent(), Value rowid = const Value.absent(), }) => WorkoutSessionTableCompanion( @@ -18393,7 +18389,7 @@ class $$WorkoutSessionTableTableTableManager Value dayId = const Value.absent(), Value notes = const Value.absent(), required WorkoutImpression impression, - Value datetimeStart = const Value.absent(), + required DateTime datetimeStart, Value datetimeEnd = const Value.absent(), Value rowid = const Value.absent(), }) => WorkoutSessionTableCompanion.insert( diff --git a/lib/database/powersync/tables/measurements.dart b/lib/database/powersync/tables/measurements.dart index d90f87e65..55a4fce30 100644 --- a/lib/database/powersync/tables/measurements.dart +++ b/lib/database/powersync/tables/measurements.dart @@ -34,12 +34,8 @@ 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()).nullable()(); + text().named('metric_type').map(const MeasurementMetricTypeConverter())(); /// Chart the user picked, NULL for the one derived from the metric type. TextColumn get chartType => @@ -56,7 +52,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)).nullable()(); + IntColumn get order => integer().withDefault(const Constant(0))(); + + // isOfficial and dynamicType are nullable: the server's backfill touch ran + // before their columns were added, so rows not changed since lack the keys + // in their ps_data and read as NULL. MeasurementCategory.fromDb maps NULL to + // the defaults. /// Server-managed official category (max. one per metric type and user). /// Body weight entries live in the official `body_weight` category. @@ -97,7 +98,7 @@ const PowersyncMeasurementCategoryTable = ps.Table( ], ); -@UseRowClass(MeasurementEntry, constructor: 'fromDb') +@UseRowClass(MeasurementEntry) class MeasurementEntryTable extends Table { @override String get tableName => 'measurements_measurement'; @@ -112,7 +113,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')).nullable()(); + TextColumn get source => text().withDefault(const Constant('user'))(); /// Platform record UUID, used to deduplicate re-imports. `null` for manual /// entries. diff --git a/lib/database/powersync/tables/routines.dart b/lib/database/powersync/tables/routines.dart index 1e53aa19c..a41fbbd8b 100644 --- a/lib/database/powersync/tables/routines.dart +++ b/lib/database/powersync/tables/routines.dart @@ -111,7 +111,7 @@ const PowersyncWorkoutLogTable = ps.Table( ], ); -@UseRowClass(WorkoutSession, constructor: 'fromDb') +@UseRowClass(WorkoutSession) class WorkoutSessionTable extends Table { @override String get tableName => 'manager_workoutsession'; @@ -122,10 +122,8 @@ class WorkoutSessionTable extends Table { TextColumn get notes => text().nullable()(); TextColumn get impression => text().map(const WorkoutImpressionConverter())(); - // Nullable: rows replicated before 2.7 have no such key in the stored JSON - // and read as NULL until they are downloaded again. TextColumn get datetimeStart => - text().named('datetime_start').nullable().map(const DateTimeTextConverter())(); + text().named('datetime_start').map(const DateTimeTextConverter())(); TextColumn get datetimeEnd => text().named('datetime_end').nullable().map(const DateTimeTextConverter())(); } diff --git a/lib/features/measurements/models/measurement_category.dart b/lib/features/measurements/models/measurement_category.dart index 658da2e43..bebd3f167 100644 --- a/lib/features/measurements/models/measurement_category.dart +++ b/lib/features/measurements/models/measurement_category.dart @@ -610,18 +610,18 @@ class MeasurementCategory with _$MeasurementCategory { 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. + /// Maps a row of the local database. [isOfficial] and [dynamicType] read as + /// NULL for rows the server's backfill didn't cover (see the table) and fall + /// back to the defaults. MeasurementCategory.fromDb({ required String id, required String name, required String unit, + required MetricType metricType, required ChartType chartType, - MetricType? metricType, Map? chartConfig, String? parentId, - int? order, + required int order, bool? isOfficial, String? dynamicType, Map? dynamicParams, @@ -629,11 +629,11 @@ class MeasurementCategory with _$MeasurementCategory { id: id, name: name, unit: unit, - metricType: metricType ?? MetricType.custom, + metricType: metricType, chartType: chartType, chartConfig: chartConfig, parentId: parentId, - order: order ?? 0, + order: order, isOfficial: isOfficial ?? false, dynamicType: dynamicType ?? noDynamicType, dynamicParams: dynamicParams, diff --git a/lib/features/measurements/models/measurement_entry.dart b/lib/features/measurements/models/measurement_entry.dart index ee65dfec0..05aeeefba 100644 --- a/lib/features/measurements/models/measurement_entry.dart +++ b/lib/features/measurements/models/measurement_entry.dart @@ -69,29 +69,6 @@ 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? extraData, - }) : this( - id: id, - categoryId: categoryId, - date: date, - value: value, - notes: notes, - source: source ?? measurementSourceUser, - externalId: externalId, - extraData: extraData, - ); - // Boilerplate MeasurementEntryTableCompanion toCompanion() { return MeasurementEntryTableCompanion( diff --git a/lib/features/measurements/providers/measurement_repository.dart b/lib/features/measurements/providers/measurement_repository.dart index 9f536d305..eac1b8dc1 100644 --- a/lib/features/measurements/providers/measurement_repository.dart +++ b/lib/features/measurements/providers/measurement_repository.dart @@ -613,8 +613,7 @@ class MeasurementRepository { await _db.transaction(() async { for (var i = 0; i < orderedIds.length; i++) { final stmt = _db.update(_db.measurementCategoryTable) - // IS NOT so legacy rows with a NULL order still get written - ..where((t) => t.id.equals(orderedIds[i]) & t.order.isNotValue(i)); + ..where((t) => t.id.equals(orderedIds[i]) & t.order.equals(i).not()); await stmt.write(MeasurementCategoryTableCompanion(order: Value(i))); } }); diff --git a/lib/features/routines/models/session.dart b/lib/features/routines/models/session.dart index 72053ebd8..c2281bb2c 100644 --- a/lib/features/routines/models/session.dart +++ b/lib/features/routines/models/session.dart @@ -16,7 +16,6 @@ * along with this program. If not, see . */ -import 'package:clock/clock.dart'; import 'package:drift/drift.dart' as drift; import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:material_ui/material_ui.dart'; @@ -87,31 +86,6 @@ class WorkoutSession with _$WorkoutSession { this.logs = const [], }); - /// Builds the model from a database row. - /// - /// Rows that were replicated before 2.7 have no `datetime_start` in their - /// stored JSON and read as NULL until they are downloaded again; they start - /// now. Application code uses the default constructor. - factory WorkoutSession.fromDb({ - String? id, - int? routineId, - int? dayId, - String? notes, - WorkoutImpression impression = WorkoutImpression.neutral, - DateTime? datetimeStart, - DateTime? datetimeEnd, - }) { - return WorkoutSession( - id: id, - routineId: routineId, - dayId: dayId, - notes: notes, - impression: impression, - datetimeStart: datetimeStart ?? clock.now(), - datetimeEnd: datetimeEnd, - ); - } - WorkoutSessionTableCompanion toCompanion() { return WorkoutSessionTableCompanion( id: id != null ? drift.Value(id!) : const drift.Value.absent(), diff --git a/test/features/measurements/providers/measurement_repository_test.dart b/test/features/measurements/providers/measurement_repository_test.dart index d920d8346..c265035b7 100644 --- a/test/features/measurements/providers/measurement_repository_test.dart +++ b/test/features/measurements/providers/measurement_repository_test.dart @@ -219,27 +219,19 @@ void main() { ); }); - 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 + test('maps NULL in is_official and dynamic_type to the defaults', () async { + // The server's backfill ran before these columns were added, so rows not + // changed since carry no value for them in their ps_data 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)", + 'INSERT INTO measurements_category ' + '(id, name, unit, metric_type, "order", is_official, dynamic_type) ' + "VALUES ('legacy', 'Old category', 'cm', 'custom', 0, NULL, NULL)", ); final category = (await repo.watchAllWithoutEntries().first).single; - expect(category.metricType, MetricType.custom); - expect(category.order, 0); expect(category.isOfficial, isFalse); expect(category.dynamicType, noDynamicType); expect(category.isCalculated, isFalse); - - final entry = (await repo.watchEntries('legacy', limit: 10).first).single; - expect(entry.source, 'user'); }); }); diff --git a/test/features/routines/models/session_test.dart b/test/features/routines/models/session_test.dart index a38ff263b..0389dc459 100644 --- a/test/features/routines/models/session_test.dart +++ b/test/features/routines/models/session_test.dart @@ -16,7 +16,6 @@ * along with this program. If not, see . */ -import 'package:clock/clock.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:timezone/data/latest.dart' as tzdata; import 'package:wger/core/consts.dart'; @@ -24,35 +23,11 @@ import 'package:wger/features/routines/models/log.dart'; import 'package:wger/features/routines/models/session.dart'; void main() { - group('WorkoutSession.fromDb', () { - test('takes the new columns when they are set', () { - final session = WorkoutSession.fromDb( - routineId: 1, - datetimeStart: DateTime(2026, 4, 15, 18, 30), - datetimeEnd: DateTime(2026, 4, 15, 19, 45), - ); - - expect(session.datetimeStart, DateTime(2026, 4, 15, 18, 30)); - expect(session.datetimeEnd, DateTime(2026, 4, 15, 19, 45)); - }); - - test('a row without a start starts now', () { - final now = DateTime(2026, 4, 15, 18, 30); - final session = withClock( - Clock.fixed(now), - () => WorkoutSession.fromDb(routineId: 1, datetimeEnd: null), - ); - - expect(session.datetimeStart, now); - expect(session.datetimeEnd, isNull); - }); - }); - group('WorkoutSession.localDayIn', () { setUpAll(tzdata.initializeTimeZones); test('cuts the day in the owner zone, not the device one', () { - final session = WorkoutSession.fromDb( + final session = WorkoutSession( routineId: 1, datetimeStart: DateTime.utc(2026, 8, 10, 12, 30), ); @@ -61,7 +36,7 @@ void main() { }); test('falls back to the device day while no zone is reported', () { - final session = WorkoutSession.fromDb( + final session = WorkoutSession( routineId: 1, datetimeStart: DateTime(2026, 8, 10, 23, 30), );