Move migration logic in settings_page into databasse

This commit is contained in:
Brandon Presley 2023-11-11 15:12:37 +13:00
parent 0eaac20e7e
commit 4097a79205
2 changed files with 7 additions and 3 deletions

View File

@ -22,12 +22,18 @@ class MyDatabase extends _$MyDatabase {
@override @override
MigrationStrategy get migration => MigrationStrategy( MigrationStrategy get migration => MigrationStrategy(
onCreate: (Migrator m) async { onCreate: (Migrator m) async {
print('Creating...');
await m.createAll(); await m.createAll();
var data = await (db.select(db.settings)..limit(1)).get(); var data = await (db.select(db.settings)..limit(1)).get();
if (data.isEmpty) await db.into(db.settings).insert(defaultSettings); if (data.isEmpty) await db.into(db.settings).insert(defaultSettings);
}, },
onUpgrade: (Migrator m, int from, int to) async { onUpgrade: (Migrator m, int from, int to) async {
if (from == 1) { if (from == 1) {
await m.create(db.gymSets);
await db.customInsert('''
INSERT INTO gym_sets(id, name, reps, weight, created, unit, hidden, image, sets, minutes, seconds, steps)
SELECT id, name, reps, weight, created, unit, hidden, image, sets, minutes, seconds, steps FROM sets
''');
await m.addColumn(settings, settings.darkColor); await m.addColumn(settings, settings.darkColor);
await db.customStatement(''' await db.customStatement('''
UPDATE settings SET dark_color = darkColor UPDATE settings SET dark_color = darkColor

View File

@ -95,11 +95,9 @@ class _SettingsPageState extends State<_SettingsPage> {
await file.copy(to); await file.copy(to);
print('Migrating...'); print('Migrating...');
db = MyDatabase(); db = MyDatabase();
// ignore: invalid_use_of_protected_member, invalid_use_of_visible_for_testing_member
final migrator = db.createMigrator(); final migrator = db.createMigrator();
await migrator.createAll(); await migrator.createAll();
await db.customInsert('''
INSERT INTO gym_sets(id, name, reps, weight, created, unit, hidden, image, sets, minutes, seconds, steps)
SELECT id, name, reps, weight, created, unit, hidden, image, sets, minutes, seconds, steps FROM sets''');
print('Migrated.'); print('Migrated.');
}, },
child: const Text("Import"))); child: const Text("Import")));