Files
flutter/lib/database/powersync/tables/routines.dart
Roland Geider 95eb0ddb66 Merge PR #1314: drop legacy pre-2.7 workout session columns
Conflicts resolved onto master's datetime_start/datetime_end columns. The
other readers of the old columns go as well: the rebuild in
WorkoutSession.fromDb, which now falls back to now, and the lookup by bare
date in WorkoutLogsRepository.

Closes #1310
2026-11-08 11:55:31 +01:00

180 lines
6.4 KiB
Dart

/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2020 - 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 <http://www.gnu.org/licenses/>.
*/
import 'package:drift/drift.dart';
import 'package:powersync/powersync.dart' as ps;
import 'package:wger/database/converters/date_only_text_converter.dart';
import 'package:wger/database/converters/utc_datetime_converter.dart';
import 'package:wger/database/converters/workout_impression_converter.dart';
import 'package:wger/features/routines/models/log.dart';
import 'package:wger/features/routines/models/repetition_unit.dart';
import 'package:wger/features/routines/models/routine.dart';
import 'package:wger/features/routines/models/session.dart';
import 'package:wger/features/routines/models/weight_unit.dart';
@UseRowClass(Routine)
class RoutineTable extends Table {
@override
String get tableName => 'manager_routine';
IntColumn get id => integer()();
TextColumn get name => text()();
TextColumn get description => text()();
DateTimeColumn get created => dateTime().map(const UtcDateTimeConverter())();
TextColumn get start => text().map(const DateOnlyTextConverter())();
TextColumn get end => text().map(const DateOnlyTextConverter())();
BoolColumn get isTemplate => boolean().named('is_template')();
BoolColumn get isPublic => boolean().named('is_public')();
BoolColumn get fitInWeek => boolean().named('fit_in_week')();
}
const PowersyncRoutineTable = ps.Table(
'manager_routine',
[
ps.Column.text('name'),
ps.Column.text('description'),
ps.Column.text('created'),
ps.Column.text('start'),
ps.Column.text('end'),
ps.Column.integer('is_template'),
ps.Column.integer('is_public'),
ps.Column.integer('fit_in_week'),
],
);
@UseRowClass(Log)
class WorkoutLogTable extends Table {
@override
String get tableName => 'manager_workoutlog';
TextColumn get id => text().clientDefault(() => ps.uuid.v7())();
IntColumn get exerciseId => integer().named('exercise_id')();
IntColumn get routineId => integer().named('routine_id').nullable()();
TextColumn get sessionId => text().named('session_id').nullable()();
IntColumn get iteration => integer().nullable()();
IntColumn get slotEntryId => integer().named('slot_entry_id').nullable()();
RealColumn get rir => real().nullable()();
RealColumn get rirTarget => real().named('rir_target').nullable()();
RealColumn get repetitions => real().nullable()();
RealColumn get repetitionsTarget => real().nullable().named('repetitions_target')();
IntColumn get repetitionsUnitId => integer().named('repetitions_unit_id').nullable()();
RealColumn get weight => real().nullable()();
RealColumn get weightTarget => real().nullable().named('weight_target')();
IntColumn get weightUnitId => integer().nullable().named('weight_unit_id')();
DateTimeColumn get date => dateTime().map(const UtcDateTimeConverter())();
}
const PowersyncWorkoutLogTable = ps.Table(
'manager_workoutlog',
[
ps.Column.integer('exercise_id'),
ps.Column.integer('routine_id'),
ps.Column.text('session_id'),
ps.Column.integer('iteration'),
ps.Column.integer('slot_entry_id'),
ps.Column.real('rir'),
ps.Column.real('rir_target'),
ps.Column.real('repetitions'),
ps.Column.real('repetitions_target'),
ps.Column.integer('repetitions_unit_id'),
ps.Column.real('weight'),
ps.Column.real('weight_target'),
ps.Column.integer('weight_unit_id'),
ps.Column.text('date'),
],
indexes: [
ps.Index('exercise_idx', [ps.IndexedColumn('exercise_id')]),
ps.Index('slot_entry_idx', [ps.IndexedColumn('slot_entry_id')]),
ps.Index('routine_idx', [ps.IndexedColumn('routine_id')]),
ps.Index('session_idx', [ps.IndexedColumn('session_id')]),
ps.Index('repetitions_unit_idx', [ps.IndexedColumn('repetitions_unit_id')]),
ps.Index('weight_unit_idx', [ps.IndexedColumn('weight_unit_id')]),
],
);
@UseRowClass(WorkoutSession, constructor: 'fromDb')
class WorkoutSessionTable extends Table {
@override
String get tableName => 'manager_workoutsession';
TextColumn get id => text().clientDefault(() => ps.uuid.v7())();
IntColumn get routineId => integer().named('routine_id').nullable()();
IntColumn get dayId => integer().named('day_id').nullable()();
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())();
TextColumn get datetimeEnd =>
text().named('datetime_end').nullable().map(const DateTimeTextConverter())();
}
const PowersyncWorkoutSessionTable = ps.Table(
'manager_workoutsession',
[
ps.Column.integer('routine_id'),
ps.Column.integer('day_id'),
ps.Column.text('notes'),
ps.Column.text('impression'),
ps.Column.text('datetime_start'),
ps.Column.text('datetime_end'),
],
indexes: [
ps.Index('routine_idx', [ps.IndexedColumn('routine_id')]),
ps.Index('day_idx', [ps.IndexedColumn('day_id')]),
],
);
@UseRowClass(RepetitionUnit)
class RoutineRepetitionUnitTable extends Table {
@override
String get tableName => 'core_repetitionunit';
IntColumn get id => integer()();
TextColumn get name => text()();
}
const PowersyncRoutineRepetitionUnitTable = ps.Table(
'core_repetitionunit',
[
ps.Column.text('name'),
],
);
@UseRowClass(WeightUnit)
class RoutineWeightUnitTable extends Table {
@override
String get tableName => 'core_weightunit';
IntColumn get id => integer()();
TextColumn get name => text()();
}
const PowersyncRoutineWeightUnitTable = ps.Table(
'core_weightunit',
[
ps.Column.text('name'),
],
);