2023-04-09 14:39:41 +12:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-04-11 11:41:10 +12:00
|
|
|
import 'package:fmassive/gym_set.dart';
|
2023-04-11 13:14:26 +12:00
|
|
|
import 'package:fmassive/main.dart';
|
2023-04-09 14:39:41 +12:00
|
|
|
import 'package:moor/ffi.dart';
|
|
|
|
import 'package:moor/moor.dart';
|
|
|
|
import 'package:path/path.dart';
|
2023-04-11 13:14:26 +12:00
|
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
|
2023-04-09 14:39:41 +12:00
|
|
|
import 'settings.dart';
|
|
|
|
|
|
|
|
part 'database.g.dart';
|
|
|
|
|
2023-04-11 11:41:10 +12:00
|
|
|
@UseMoor(tables: [Settings, GymSets])
|
2023-04-09 14:39:41 +12:00
|
|
|
class MyDatabase extends _$MyDatabase {
|
|
|
|
MyDatabase() : super(_openConnection());
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get schemaVersion => 1;
|
|
|
|
|
|
|
|
@override
|
|
|
|
MigrationStrategy get migration => MigrationStrategy(
|
|
|
|
onCreate: (Migrator m) async {
|
|
|
|
await m.createAll();
|
2023-04-11 13:14:26 +12:00
|
|
|
var data = await db.select(db.settings).get();
|
|
|
|
if (data.isEmpty) await db.into(db.settings).insert(defaultSettings);
|
2023-04-09 14:39:41 +12:00
|
|
|
},
|
|
|
|
onUpgrade: (Migrator m, int from, int to) async {
|
|
|
|
// no migrations yet
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
LazyDatabase _openConnection() {
|
|
|
|
return LazyDatabase(() async {
|
2023-04-11 13:14:26 +12:00
|
|
|
final dbFolder = await getDatabasesPath();
|
|
|
|
final file = File(join(dbFolder, 'massive.db'));
|
2023-04-09 14:39:41 +12:00
|
|
|
return VmDatabase(file, logStatements: true);
|
|
|
|
});
|
|
|
|
}
|