Remove the measurement and session compat layers covered by a backfill

Closes #1320
This commit is contained in:
Roland Geider
2026-12-01 19:44:20 +01:00
parent 406260dd98
commit a6e68b48af
10 changed files with 104 additions and 192 deletions

View File

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

View File

@@ -4771,15 +4771,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,
true,
false,
type: DriftSqlType.string,
requiredDuringInsert: false,
).withConverter<MetricType?>(
$MeasurementCategoryTableTable.$convertermetricTypen,
requiredDuringInsert: true,
).withConverter<MetricType>(
$MeasurementCategoryTableTable.$convertermetricType,
);
@override
late final GeneratedColumnWithTypeConverter<ChartType, String> chartType =
@@ -4822,7 +4822,7 @@ class $MeasurementCategoryTableTable extends MeasurementCategoryTable
late final GeneratedColumn<int> order = GeneratedColumn<int>(
'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<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 =
@@ -5029,11 +5026,11 @@ 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<int> order;
final Value<bool?> isOfficial;
final Value<String?> dynamicType;
final Value<Map<String, dynamic>?> dynamicParams;
@@ -5056,7 +5053,7 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion<MeasurementCateg
this.id = const Value.absent(),
required String name,
required String unit,
this.metricType = const Value.absent(),
required MetricType metricType,
this.chartType = const Value.absent(),
this.chartConfig = const Value.absent(),
this.parentId = const Value.absent(),
@@ -5066,7 +5063,8 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion<MeasurementCateg
this.dynamicParams = const Value.absent(),
this.rowid = const Value.absent(),
}) : name = Value(name),
unit = Value(unit);
unit = Value(unit),
metricType = Value(metricType);
static Insertable<MeasurementCategory> custom({
Expression<String>? id,
Expression<String>? name,
@@ -5101,11 +5099,11 @@ 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<int>? order,
Value<bool?>? isOfficial,
Value<String?>? dynamicType,
Value<Map<String, dynamic>?>? dynamicParams,
@@ -5141,7 +5139,7 @@ class MeasurementCategoryTableCompanion extends UpdateCompanion<MeasurementCateg
}
if (metricType.present) {
map['metric_type'] = Variable<String>(
$MeasurementCategoryTableTable.$convertermetricTypen.toSql(
$MeasurementCategoryTableTable.$convertermetricType.toSql(
metricType.value,
),
);
@@ -5266,7 +5264,7 @@ class $MeasurementEntryTableTable extends MeasurementEntryTable
late final GeneratedColumn<String> source = GeneratedColumn<String>(
'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<String, dynamic> 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<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;
@@ -5479,7 +5477,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,
@@ -6585,15 +6583,15 @@ class $WorkoutSessionTableTable extends WorkoutSessionTable
$WorkoutSessionTableTable.$converterimpression,
);
@override
late final GeneratedColumnWithTypeConverter<DateTime?, String> datetimeStart =
late final GeneratedColumnWithTypeConverter<DateTime, String> datetimeStart =
GeneratedColumn<String>(
'datetime_start',
aliasedName,
true,
false,
type: DriftSqlType.string,
requiredDuringInsert: false,
).withConverter<DateTime?>(
$WorkoutSessionTableTable.$converterdatetimeStartn,
requiredDuringInsert: true,
).withConverter<DateTime>(
$WorkoutSessionTableTable.$converterdatetimeStart,
);
@override
late final GeneratedColumnWithTypeConverter<DateTime?, String> datetimeEnd =
@@ -6657,22 +6655,30 @@ class $WorkoutSessionTableTable extends WorkoutSessionTable
@override
WorkoutSession map(Map<String, dynamic> 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<WorkoutImpression, String> $converterimpression =
const WorkoutImpressionConverter();
static TypeConverter<DateTime, String> $converterdatetimeStart = const DateTimeTextConverter();
static TypeConverter<DateTime?, String?> $converterdatetimeStartn = NullAwareTypeConverter.wrap(
$converterdatetimeStart,
);
static TypeConverter<DateTime, String> $converterdatetimeEnd = const DateTimeTextConverter();
static TypeConverter<DateTime?, String?> $converterdatetimeEndn = NullAwareTypeConverter.wrap(
$converterdatetimeEnd,
@@ -6718,7 +6713,7 @@ class WorkoutSessionTableCompanion extends UpdateCompanion<WorkoutSession> {
final Value<int?> dayId;
final Value<String?> notes;
final Value<WorkoutImpression> impression;
final Value<DateTime?> datetimeStart;
final Value<DateTime> datetimeStart;
final Value<DateTime?> datetimeEnd;
final Value<int> rowid;
const WorkoutSessionTableCompanion({
@@ -6737,10 +6732,11 @@ class WorkoutSessionTableCompanion extends UpdateCompanion<WorkoutSession> {
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<WorkoutSession> custom({
Expression<String>? id,
Expression<int>? routineId,
@@ -6769,7 +6765,7 @@ class WorkoutSessionTableCompanion extends UpdateCompanion<WorkoutSession> {
Value<int?>? dayId,
Value<String?>? notes,
Value<WorkoutImpression>? impression,
Value<DateTime?>? datetimeStart,
Value<DateTime>? datetimeStart,
Value<DateTime?>? datetimeEnd,
Value<int>? rowid,
}) {
@@ -6807,7 +6803,7 @@ class WorkoutSessionTableCompanion extends UpdateCompanion<WorkoutSession> {
}
if (datetimeStart.present) {
map['datetime_start'] = Variable<String>(
$WorkoutSessionTableTable.$converterdatetimeStartn.toSql(
$WorkoutSessionTableTable.$converterdatetimeStart.toSql(
datetimeStart.value,
),
);
@@ -16533,11 +16529,11 @@ typedef $$MeasurementCategoryTableTableCreateCompanionBuilder =
Value<String> id,
required String name,
required String unit,
Value<MetricType?> metricType,
required MetricType metricType,
Value<ChartType> chartType,
Value<Map<String, dynamic>?> chartConfig,
Value<String?> parentId,
Value<int?> order,
Value<int> order,
Value<bool?> isOfficial,
Value<String?> dynamicType,
Value<Map<String, dynamic>?> dynamicParams,
@@ -16548,11 +16544,11 @@ 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<int> order,
Value<bool?> isOfficial,
Value<String?> dynamicType,
Value<Map<String, dynamic>?> dynamicParams,
@@ -16637,7 +16633,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),
@@ -16822,7 +16818,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,
);
@@ -16944,11 +16940,11 @@ 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<int> order = const Value.absent(),
Value<bool?> isOfficial = const Value.absent(),
Value<String?> dynamicType = const Value.absent(),
Value<Map<String, dynamic>?> dynamicParams = const Value.absent(),
@@ -16972,11 +16968,11 @@ class $$MeasurementCategoryTableTableTableManager
Value<String> id = const Value.absent(),
required String name,
required String unit,
Value<MetricType?> metricType = const Value.absent(),
required MetricType metricType,
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<int> order = const Value.absent(),
Value<bool?> isOfficial = const Value.absent(),
Value<String?> dynamicType = const Value.absent(),
Value<Map<String, dynamic>?> dynamicParams = const Value.absent(),
@@ -17091,7 +17087,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,
@@ -17103,7 +17099,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,
@@ -17370,7 +17366,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(),
@@ -17392,7 +17388,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(),
@@ -18186,7 +18182,7 @@ typedef $$WorkoutSessionTableTableCreateCompanionBuilder =
Value<int?> dayId,
Value<String?> notes,
required WorkoutImpression impression,
Value<DateTime?> datetimeStart,
required DateTime datetimeStart,
Value<DateTime?> datetimeEnd,
Value<int> rowid,
});
@@ -18197,7 +18193,7 @@ typedef $$WorkoutSessionTableTableUpdateCompanionBuilder =
Value<int?> dayId,
Value<String?> notes,
Value<WorkoutImpression> impression,
Value<DateTime?> datetimeStart,
Value<DateTime> datetimeStart,
Value<DateTime?> datetimeEnd,
Value<int> rowid,
});
@@ -18237,7 +18233,7 @@ class $$WorkoutSessionTableTableFilterComposer
builder: (column) => ColumnWithTypeConverterFilters(column),
);
ColumnWithTypeConverterFilters<DateTime?, DateTime, String> get datetimeStart =>
ColumnWithTypeConverterFilters<DateTime, DateTime, String> get datetimeStart =>
$composableBuilder(
column: $table.datetimeStart,
builder: (column) => ColumnWithTypeConverterFilters(column),
@@ -18320,7 +18316,7 @@ class $$WorkoutSessionTableTableAnnotationComposer
builder: (column) => column,
);
GeneratedColumnWithTypeConverter<DateTime?, String> get datetimeStart => $composableBuilder(
GeneratedColumnWithTypeConverter<DateTime, String> get datetimeStart => $composableBuilder(
column: $table.datetimeStart,
builder: (column) => column,
);
@@ -18373,7 +18369,7 @@ class $$WorkoutSessionTableTableTableManager
Value<int?> dayId = const Value.absent(),
Value<String?> notes = const Value.absent(),
Value<WorkoutImpression> impression = const Value.absent(),
Value<DateTime?> datetimeStart = const Value.absent(),
Value<DateTime> datetimeStart = const Value.absent(),
Value<DateTime?> datetimeEnd = const Value.absent(),
Value<int> rowid = const Value.absent(),
}) => WorkoutSessionTableCompanion(
@@ -18393,7 +18389,7 @@ class $$WorkoutSessionTableTableTableManager
Value<int?> dayId = const Value.absent(),
Value<String?> notes = const Value.absent(),
required WorkoutImpression impression,
Value<DateTime?> datetimeStart = const Value.absent(),
required DateTime datetimeStart,
Value<DateTime?> datetimeEnd = const Value.absent(),
Value<int> rowid = const Value.absent(),
}) => WorkoutSessionTableCompanion.insert(

View File

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

View File

@@ -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())();
}

View File

@@ -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<String, dynamic>? chartConfig,
String? parentId,
int? order,
required int order,
bool? isOfficial,
String? dynamicType,
Map<String, dynamic>? 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,

View File

@@ -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<String, dynamic>? extraData,
}) : this(
id: id,
categoryId: categoryId,
date: date,
value: value,
notes: notes,
source: source ?? measurementSourceUser,
externalId: externalId,
extraData: extraData,
);
// Boilerplate
MeasurementEntryTableCompanion toCompanion() {
return MeasurementEntryTableCompanion(

View File

@@ -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)));
}
});

View File

@@ -16,7 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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(),

View File

@@ -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');
});
});

View File

@@ -16,7 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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),
);