feat(data): modèle de données Drift pour la persistance locale
Définit les tables Drift et les identifiants locaux (local_id.dart, tables.dart), régénère app_database.g.dart. build_runner, flutter analyze et build APK debug validés. Versionne les fichiers générés .g.dart : projet solo sans pipeline CI qui relance build_runner, on privilégie un clone immédiatement buildable. Retire la règle **/*.g.dart (et freezed/gr.dart, non utilisés) du .gitignore en conséquence. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@ -1,16 +1,35 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift_flutter/drift_flutter.dart';
|
||||
|
||||
final class AppDatabase extends GeneratedDatabase {
|
||||
AppDatabase(DatabaseConnection connection) : super.connect(connection);
|
||||
import 'tables.dart';
|
||||
|
||||
part 'app_database.g.dart';
|
||||
|
||||
@DriftDatabase(
|
||||
tables: [
|
||||
ActiveRestStates,
|
||||
ActiveSetResults,
|
||||
ActiveWorkoutSessions,
|
||||
ChangeLogEntries,
|
||||
Exercises,
|
||||
MediaAssets,
|
||||
ProgramExercises,
|
||||
Programs,
|
||||
WorkoutHistories,
|
||||
WorkoutHistorySetResults,
|
||||
WorkoutTemplateExerciseOverrides,
|
||||
WorkoutTemplatePrograms,
|
||||
WorkoutTemplates,
|
||||
],
|
||||
)
|
||||
final class AppDatabase extends _$AppDatabase {
|
||||
AppDatabase(super.executor);
|
||||
|
||||
factory AppDatabase.open() {
|
||||
return AppDatabase(
|
||||
driftDatabase(
|
||||
name: 'gametime',
|
||||
native: const DriftNativeOptions(
|
||||
shareAcrossIsolates: true,
|
||||
),
|
||||
native: const DriftNativeOptions(shareAcrossIsolates: true),
|
||||
),
|
||||
);
|
||||
}
|
||||
@ -19,5 +38,95 @@ final class AppDatabase extends GeneratedDatabase {
|
||||
int get schemaVersion => 1;
|
||||
|
||||
@override
|
||||
Iterable<TableInfo<Table, dynamic>> get allTables => const [];
|
||||
MigrationStrategy get migration {
|
||||
return MigrationStrategy(
|
||||
onCreate: (migrator) async {
|
||||
await migrator.createAll();
|
||||
await _createIndexes();
|
||||
},
|
||||
beforeOpen: (details) async {
|
||||
await customStatement('PRAGMA foreign_keys = ON');
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _createIndexes() async {
|
||||
for (final tableName in _syncableTableNames) {
|
||||
await customStatement(
|
||||
'CREATE INDEX IF NOT EXISTS idx_${tableName}_deleted_at '
|
||||
'ON $tableName (deleted_at)',
|
||||
);
|
||||
await customStatement(
|
||||
'CREATE INDEX IF NOT EXISTS idx_${tableName}_updated_at '
|
||||
'ON $tableName (updated_at)',
|
||||
);
|
||||
await customStatement(
|
||||
'CREATE INDEX IF NOT EXISTS idx_${tableName}_sync_state '
|
||||
'ON $tableName (sync_state)',
|
||||
);
|
||||
await customStatement(
|
||||
'CREATE INDEX IF NOT EXISTS idx_${tableName}_local_revision '
|
||||
'ON $tableName (local_revision)',
|
||||
);
|
||||
}
|
||||
|
||||
await customStatement(
|
||||
'CREATE INDEX IF NOT EXISTS idx_program_exercises_program_id '
|
||||
'ON program_exercises (program_id)',
|
||||
);
|
||||
await customStatement(
|
||||
'CREATE INDEX IF NOT EXISTS idx_workout_template_programs_template_id '
|
||||
'ON workout_template_programs (workout_template_id)',
|
||||
);
|
||||
await customStatement(
|
||||
'CREATE INDEX IF NOT EXISTS idx_active_set_results_session_id '
|
||||
'ON active_set_results (active_workout_session_id)',
|
||||
);
|
||||
await customStatement(
|
||||
'CREATE INDEX IF NOT EXISTS idx_active_rest_states_session_id '
|
||||
'ON active_rest_states (active_workout_session_id)',
|
||||
);
|
||||
await customStatement(
|
||||
'CREATE UNIQUE INDEX IF NOT EXISTS '
|
||||
'idx_active_workout_sessions_single_open '
|
||||
'ON active_workout_sessions ((1)) '
|
||||
'WHERE deleted_at IS NULL '
|
||||
"AND status IN ('running', 'paused', 'savedExit')",
|
||||
);
|
||||
await customStatement(
|
||||
'CREATE INDEX IF NOT EXISTS idx_workout_history_started_at '
|
||||
'ON workout_history (started_at)',
|
||||
);
|
||||
await customStatement(
|
||||
'CREATE INDEX IF NOT EXISTS idx_workout_history_set_results_history_id '
|
||||
'ON workout_history_set_results (workout_history_id)',
|
||||
);
|
||||
await customStatement(
|
||||
'CREATE INDEX IF NOT EXISTS idx_change_log_entity '
|
||||
'ON change_log (entity_type, entity_id)',
|
||||
);
|
||||
await customStatement(
|
||||
'CREATE INDEX IF NOT EXISTS idx_change_log_local_revision '
|
||||
'ON change_log (local_revision)',
|
||||
);
|
||||
await customStatement(
|
||||
'CREATE INDEX IF NOT EXISTS idx_change_log_synced_at '
|
||||
'ON change_log (synced_at)',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const _syncableTableNames = [
|
||||
'active_rest_states',
|
||||
'active_set_results',
|
||||
'active_workout_sessions',
|
||||
'exercises',
|
||||
'media_assets',
|
||||
'program_exercises',
|
||||
'programs',
|
||||
'workout_history',
|
||||
'workout_history_set_results',
|
||||
'workout_template_exercise_overrides',
|
||||
'workout_template_programs',
|
||||
'workout_templates',
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user