|
1 | 1 | import 'package:drift/drift.dart';
|
2 | 2 | import 'package:drift_flutter/drift_flutter.dart';
|
| 3 | +import 'package:on_time_front/data/daos/place_dao.dart'; |
| 4 | +import 'package:on_time_front/data/daos/preparation_schedule_dao.dart'; |
| 5 | +import 'package:on_time_front/data/daos/preparation_user_dao.dart'; |
| 6 | + |
3 | 7 | import 'package:on_time_front/data/daos/schedule_dao.dart';
|
| 8 | +import 'package:on_time_front/data/daos/user_dao.dart'; |
| 9 | + |
4 | 10 | import '../data/tables/places_table.dart';
|
5 | 11 | import '../data/tables/schedules_table.dart';
|
| 12 | +import '../data/tables/user_table.dart'; |
| 13 | +import '../data/tables/preparation_schedule_table.dart'; |
| 14 | +import '../data/tables/preparation_user_table.dart'; |
6 | 15 |
|
7 | 16 | part 'database.g.dart';
|
8 | 17 |
|
9 |
| -@DriftDatabase(tables: [Places, Schedules], daos: [ScheduleDao]) |
| 18 | +@DriftDatabase(tables: [ |
| 19 | + Places, |
| 20 | + Schedules, |
| 21 | + Users, |
| 22 | + PreparationSchedules, |
| 23 | + PreparationUsers |
| 24 | +], daos: [ |
| 25 | + ScheduleDao, |
| 26 | + PlaceDao, |
| 27 | + UserDao, |
| 28 | + PreparationScheduleDao, |
| 29 | + PreparationUserDao |
| 30 | +]) |
10 | 31 | class AppDatabase extends _$AppDatabase {
|
11 |
| - // After generating code, this class needs to define a schemaVersion getter |
12 |
| - // and a constructor telling drift where the database should be stored. |
13 |
| - // These are described in the getting started guide: https://drift.simonbinder.eu/getting-started/#open |
14 | 32 | AppDatabase() : super(_openConnection());
|
15 | 33 |
|
16 | 34 | @override
|
17 |
| - int get schemaVersion => 1; |
| 35 | + int get schemaVersion => 3; |
| 36 | + |
| 37 | + @override |
| 38 | + MigrationStrategy get migration => MigrationStrategy( |
| 39 | + onCreate: (Migrator m) async { |
| 40 | + await m.createAll(); |
| 41 | + }, |
| 42 | + onUpgrade: (Migrator m, int from, int to) async { |
| 43 | + if (from == 1) { |
| 44 | + await m.createTable(preparationUsers); |
| 45 | + await m.createTable(preparationSchedules); |
| 46 | + } |
| 47 | + }, |
| 48 | + beforeOpen: (details) async { |
| 49 | + await customStatement('PRAGMA foreign_keys = ON'); |
| 50 | + }, |
| 51 | + ); |
18 | 52 |
|
19 | 53 | static QueryExecutor _openConnection() {
|
20 |
| - // driftDatabase from package:drift_flutter stores the database in |
21 |
| - // getApplicationDocumentsDirectory(). |
22 | 54 | return driftDatabase(name: 'my_database');
|
23 | 55 | }
|
24 | 56 | }
|
0 commit comments