diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp @@ -1667,6 +1667,7 @@ throw std::system_error(errno, std::generic_category(), errorStream.str()); } SQLiteQueryExecutor::generateBackupDataKey(); + SQLiteQueryExecutor::generateBackupLogDataKey(); SQLiteQueryExecutor::migrate(); } @@ -1678,17 +1679,21 @@ folly::Optional maybeBackupLogDataKey = CommSecureStore::get(CommSecureStore::backupLogDataKey); - if (SQLiteUtils::fileExists(databasePath) && maybeBackupDataKey && - maybeBackupLogDataKey) { - SQLiteQueryExecutor::backupDataKey = maybeBackupDataKey.value(); - SQLiteQueryExecutor::backupLogDataKey = maybeBackupLogDataKey.value(); - return; - } else if (SQLiteUtils::fileExists(databasePath) && maybeBackupDataKey) { + // In case of a non-existent database file, we always want to generate fresh + // key. + if (!SQLiteUtils::fileExists(databasePath) || !maybeBackupDataKey) { + SQLiteQueryExecutor::generateBackupDataKey(); + } else { SQLiteQueryExecutor::backupDataKey = maybeBackupDataKey.value(); + } + + // In case of a non-existent database file, we always want to generate fresh + // key. + if (!SQLiteUtils::fileExists(databasePath) || !maybeBackupLogDataKey) { SQLiteQueryExecutor::generateBackupLogDataKey(); - return; + } else { + SQLiteQueryExecutor::backupLogDataKey = maybeBackupLogDataKey.value(); } - SQLiteQueryExecutor::generateBackupDataKey(); }); } @@ -1819,7 +1824,6 @@ SQLiteQueryExecutor::backupDataKeySize); CommSecureStore::set(CommSecureStore::backupDataKey, backupDataKey); SQLiteQueryExecutor::backupDataKey = backupDataKey; - SQLiteQueryExecutor::generateBackupLogDataKey(); } void SQLiteQueryExecutor::generateBackupLogDataKey() {