/* * This file is part of wger Workout Manager . * Copyright (c) 2026 wger Team * * wger Workout Manager is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ import 'package:drift/drift.dart'; import 'package:powersync/powersync.dart' as ps; import 'package:wger/database/converters/json_map_converter.dart'; import 'package:wger/database/converters/measurement_chart_type_converter.dart'; import 'package:wger/database/converters/measurement_metric_type_converter.dart'; 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) class MeasurementCategoryTable extends Table { @override String get tableName => 'measurements_category'; TextColumn get id => text().clientDefault(() => ps.uuid.v7())(); TextColumn get name => text()(); TextColumn get unit => text()(); TextColumn get metricType => text().named('metric_type').map(const MeasurementMetricTypeConverter())(); /// Chart the user picked, NULL for the one derived from the metric type. TextColumn get chartType => text().named('chart_type').nullable().map(const MeasurementChartTypeConverter())(); /// Multi-value groups: parent category id (max. one level of nesting; only /// leaf categories carry entries). TextColumn get parentId => 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))(); /// 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))(); } const PowersyncMeasurementCategoryTable = ps.Table( 'measurements_category', [ ps.Column.text('name'), ps.Column.text('unit'), ps.Column.text('metric_type'), ps.Column.text('chart_type'), ps.Column.text('parent_id'), ps.Column.integer('order'), // Postgres boolean; integer so the view yields 0/1, not the string '0' ps.Column.integer('is_official'), ], indexes: [ ps.Index('parent_idx', [ps.IndexedColumn('parent_id')]), ], ); @UseRowClass(MeasurementEntry) class MeasurementEntryTable extends Table { @override String get tableName => 'measurements_measurement'; TextColumn get id => text().clientDefault(() => ps.uuid.v7())(); TextColumn get categoryId => text().named('category_id').references(MeasurementCategoryTable, #id)(); DateTimeColumn get date => dateTime().map(const UtcDateTimeConverter())(); RealColumn get value => real()(); TextColumn get notes => text()(); /// 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'))(); /// Platform record UUID, used to deduplicate re-imports. `null` for manual /// entries. TextColumn get externalId => text().named('external_id').nullable()(); /// Per-entry metadata (server JSONField). The `unit` key holds the unit the /// value was entered in; absent, the category unit applies. TextColumn get extraData => text().named('extra_data').map(const JsonMapConverter()).nullable()(); } const PowersyncMeasurementEntryTable = ps.Table( 'measurements_measurement', [ ps.Column.text('category_id'), ps.Column.text('date'), ps.Column.real('value'), ps.Column.text('notes'), ps.Column.text('source'), ps.Column.text('external_id'), // JSON object, synced and stored as text ps.Column.text('extra_data'), ], indexes: [ // Entries are read per category and bounded by date (the charts read a // range, the lists sort by it). The date is stored as an ISO string, which // sorts the same way the timestamps do, so a range is one index scan ps.Index('category_idx', [ ps.IndexedColumn('category_id'), ps.IndexedColumn('date'), ]), ps.Index('external_id_idx', [ps.IndexedColumn('external_id')]), ], );