fix: backup-compatible db location on all platforms (#2721)

* fix: backup-compatible db location on all platforms * sync io for db move

malik1004x committed Dec 11, 2025 at 17:31 UTC 80a759c52fc662aa3fb4cb4208434106f74d9da1
1 file changed +14 -7
cw_core/lib/db/sqlite.dart
+14 -7
@@ -4,18 +4,25 @@ import 'dart:io';
4 import 'package:cw_core/root_dir.dart';
5 import 'package:sqflite_common_ffi/sqflite_ffi.dart';
6
7 -late Database db;
7 +late Database db;
8
9 Future<void> initDb({String? pathOverride}) async {
10 - if (Platform.isLinux || Platform.isWindows) {
10 + if (Platform.isLinux || Platform.isWindows) {
11 databaseFactory = databaseFactoryFfi;
12 }
13 - if (Platform.isWindows) {
14 - pathOverride ??= "${(await getAppDir()).path}/cake.db";
13 +
14 + // getAppDir is predictable on all platforms and ensures the db gets included in backups.
15 + final dbFileOld = File("${await getDatabasesPath()}/cake.db");
16 + final dbFile = File("${(await getAppDir()).path}/cake.db");
17 +
18 + if (dbFileOld.existsSync()) {
19 + final copied = dbFileOld.copySync(dbFile.path);
20 + if (copied.existsSync()) {
21 + dbFileOld.deleteSync();
22 + }
23 }
16 - db = await openDatabase(
17 - pathOverride ?? "cake.db",
18 - version: 1,
24 +
25 + db = await openDatabase(dbFile.path, version: 1,
26 onCreate: (Database db, int version) async {
27 await db.execute(
28 '''