From 80a759c52fc662aa3fb4cb4208434106f74d9da1 Mon Sep 17 00:00:00 2001 From: malik1004x Date: Thu, 11 Dec 2025 17:31:13 +0100 Subject: [PATCH] fix: backup-compatible db location on all platforms (#2721) * fix: backup-compatible db location on all platforms * sync io for db move --- cw_core/lib/db/sqlite.dart | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/cw_core/lib/db/sqlite.dart b/cw_core/lib/db/sqlite.dart index 9e5c03f15..4ac263c81 100644 --- a/cw_core/lib/db/sqlite.dart +++ b/cw_core/lib/db/sqlite.dart @@ -4,18 +4,25 @@ import 'dart:io'; import 'package:cw_core/root_dir.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart'; -late Database db; +late Database db; Future initDb({String? pathOverride}) async { - if (Platform.isLinux || Platform.isWindows) { + if (Platform.isLinux || Platform.isWindows) { databaseFactory = databaseFactoryFfi; } - if (Platform.isWindows) { - pathOverride ??= "${(await getAppDir()).path}/cake.db"; + + // getAppDir is predictable on all platforms and ensures the db gets included in backups. + final dbFileOld = File("${await getDatabasesPath()}/cake.db"); + final dbFile = File("${(await getAppDir()).path}/cake.db"); + + if (dbFileOld.existsSync()) { + final copied = dbFileOld.copySync(dbFile.path); + if (copied.existsSync()) { + dbFileOld.deleteSync(); + } } - db = await openDatabase( - pathOverride ?? "cake.db", - version: 1, + + db = await openDatabase(dbFile.path, version: 1, onCreate: (Database db, int version) async { await db.execute( '''