2023-04-09 14:39:41 +12:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-11-10 23:43:06 +13:00
|
|
|
import 'package:fmassive/gym_sets.dart';
|
2023-04-11 13:14:26 +12:00
|
|
|
import 'package:fmassive/main.dart';
|
2023-04-13 18:58:26 +12:00
|
|
|
import 'package:fmassive/plans.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-13 18:58:26 +12:00
|
|
|
@UseMoor(tables: [Settings, GymSets, Plans])
|
2023-04-09 14:39:41 +12:00
|
|
|
class MyDatabase extends _$MyDatabase {
|
|
|
|
MyDatabase() : super(_openConnection());
|
|
|
|
|
|
|
|
@override
|
2023-11-10 18:32:37 +13:00
|
|
|
int get schemaVersion => 2;
|
2023-04-09 14:39:41 +12:00
|
|
|
|
|
|
|
@override
|
|
|
|
MigrationStrategy get migration => MigrationStrategy(
|
|
|
|
onCreate: (Migrator m) async {
|
|
|
|
await m.createAll();
|
2023-04-17 11:51:14 +12:00
|
|
|
var data = await (db.select(db.settings)..limit(1)).get();
|
2023-04-11 13:14:26 +12:00
|
|
|
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 {
|
2023-11-10 18:32:37 +13:00
|
|
|
if (from == 1) {
|
|
|
|
await m.addColumn(settings, settings.darkColor);
|
|
|
|
await db.customStatement('''
|
|
|
|
UPDATE settings SET dark_color = darkColor
|
|
|
|
''');
|
|
|
|
await m.addColumn(settings, settings.lightColor);
|
|
|
|
await db.customStatement('''
|
|
|
|
UPDATE settings SET light_color = lightColor
|
|
|
|
''');
|
|
|
|
await m.addColumn(settings, settings.showDate);
|
|
|
|
await db.customStatement('''
|
|
|
|
UPDATE settings SET show_date = showDate
|
|
|
|
''');
|
|
|
|
await m.addColumn(settings, settings.showSets);
|
|
|
|
await db.customStatement('''
|
|
|
|
UPDATE settings SET show_sets = showSets
|
|
|
|
''');
|
|
|
|
await m.addColumn(settings, settings.showUnit);
|
|
|
|
await db.customStatement('''
|
|
|
|
UPDATE settings SET show_unit = showUnit
|
|
|
|
''');
|
|
|
|
}
|
2023-04-09 14:39:41 +12:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|