From 949b640c8193a82017c03010c9103499fd021c86 Mon Sep 17 00:00:00 2001 From: The one with the braid Date: Wed, 3 Jan 2024 13:42:33 +0100 Subject: [PATCH] chore: migrate sqlite file location - avoid using getDatabasePath - use getApplicationSupportDirectory instead Signed-off-by: The one with the braid --- .../builder.dart | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/utils/matrix_sdk_extensions/flutter_matrix_dart_sdk_database/builder.dart b/lib/utils/matrix_sdk_extensions/flutter_matrix_dart_sdk_database/builder.dart index 9ed3220c4..548bf68be 100644 --- a/lib/utils/matrix_sdk_extensions/flutter_matrix_dart_sdk_database/builder.dart +++ b/lib/utils/matrix_sdk_extensions/flutter_matrix_dart_sdk_database/builder.dart @@ -75,6 +75,10 @@ Future _constructDatabase(Client client) async { final path = await getApplicationSupportDirectory(); final sqlFilePath = '$path/${client.clientName}.sqlite'; + // migrating from petty unreliable `getDatabasePath` + // See : https://pub.dev/packages/sqflite_common_ffi#limitations + await _migrateLegacyLocation(sqlFilePath, client.clientName); + fileStoragePath = await getTemporaryDirectory(); database = await openDatabase( sqlFilePath, @@ -91,5 +95,20 @@ Future _constructDatabase(Client client) async { ); } +Future _migrateLegacyLocation( + String sqlFilePath, + String clientName, +) async { + final oldPath = await getDatabasesPath(); + final oldFilePath = '$oldPath/$clientName.sqlite'; + if (oldFilePath == sqlFilePath) return; + + final maybeOldFile = File(oldFilePath); + if (await maybeOldFile.exists()) { + await maybeOldFile.copy(sqlFilePath); + await maybeOldFile.delete(); + } +} + Future _applySQLCipher(Database db, String cipher) => db.rawQuery("PRAGMA KEY='$cipher'");