mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-10-05 07:59:14 +00:00
core: remove .exported db files when database is removed (#7615)
Co-authored-by: Evgeny @ SimpleX Chat <259188159+evgeny-simplex@users.noreply.github.com>
This commit is contained in:
co-authored by
Evgeny @ SimpleX Chat
parent
3d3dd421ff
commit
80f8757449
@@ -50,6 +50,10 @@ private let CHAT_DB_BAK: String = "_chat.db.bak"
|
||||
|
||||
private let AGENT_DB_BAK: String = "_agent.db.bak"
|
||||
|
||||
private let CHAT_DB_EXPORTED: String = "_chat.db.exported"
|
||||
|
||||
private let AGENT_DB_EXPORTED: String = "_agent.db.exported"
|
||||
|
||||
// Spec: spec/database.md#getDocumentsDirectory
|
||||
public func getDocumentsDirectory() -> URL {
|
||||
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
|
||||
@@ -113,6 +117,8 @@ public func deleteAppDatabaseAndFiles() {
|
||||
}
|
||||
try? fm.removeItem(atPath: dbPath + CHAT_DB_BAK)
|
||||
try? fm.removeItem(atPath: dbPath + AGENT_DB_BAK)
|
||||
try? fm.removeItem(atPath: dbPath + CHAT_DB_EXPORTED)
|
||||
try? fm.removeItem(atPath: dbPath + AGENT_DB_EXPORTED)
|
||||
try? fm.removeItem(at: getTempFilesDirectory())
|
||||
try? fm.removeItem(at: getMigrationTempFilesDirectory())
|
||||
try? fm.createDirectory(at: getTempFilesDirectory(), withIntermediateDirectories: true)
|
||||
@@ -210,6 +216,8 @@ public func removeLegacyDatabaseAndFiles() -> Bool {
|
||||
let r2 = nil != (try? fm.removeItem(atPath: dbPath.path + CHAT_DB))
|
||||
try? fm.removeItem(atPath: dbPath.path + AGENT_DB_BAK)
|
||||
try? fm.removeItem(atPath: dbPath.path + CHAT_DB_BAK)
|
||||
try? fm.removeItem(atPath: dbPath.path + AGENT_DB_EXPORTED)
|
||||
try? fm.removeItem(atPath: dbPath.path + CHAT_DB_EXPORTED)
|
||||
try? fm.removeItem(at: appFiles)
|
||||
return r1 && r2
|
||||
}
|
||||
|
||||
+4
@@ -556,12 +556,16 @@ suspend fun deleteChatAsync(m: ChatModel) {
|
||||
fun deleteChatDatabaseFilesAndState() {
|
||||
val chat = File(dataDir, chatDatabaseFileName)
|
||||
val chatBak = File(dataDir, "$chatDatabaseFileName.bak")
|
||||
val chatExported = File(dataDir, "$chatDatabaseFileName.exported")
|
||||
val agent = File(dataDir, agentDatabaseFileName)
|
||||
val agentBak = File(dataDir, "$agentDatabaseFileName.bak")
|
||||
val agentExported = File(dataDir, "$agentDatabaseFileName.exported")
|
||||
chat.delete()
|
||||
chatBak.delete()
|
||||
chatExported.delete()
|
||||
agent.delete()
|
||||
agentBak.delete()
|
||||
agentExported.delete()
|
||||
filesDir.deleteRecursively()
|
||||
filesDir.mkdir()
|
||||
remoteHostsDir.deleteRecursively()
|
||||
|
||||
@@ -124,11 +124,13 @@ deleteStorage = do
|
||||
fs <- lift storageFiles
|
||||
liftIO $ closeDBStore `withStores` fs
|
||||
remove `withDBs` fs
|
||||
removeExported `withDBs` fs
|
||||
mapM_ removeDir $ filesPath fs
|
||||
mapM_ removeDir $ assetsPath fs
|
||||
mapM_ removeDir =<< chatReadVar tempDirectory
|
||||
where
|
||||
remove f = whenM (doesFileExist f) $ removeFile f
|
||||
removeExported f = remove $ f <> ".exported"
|
||||
removeDir d = whenM (doesDirectoryExist d) $ removePathForcibly d
|
||||
|
||||
data StorageFiles = StorageFiles
|
||||
|
||||
@@ -1493,12 +1493,15 @@ testMaintenanceModeWithFiles ps = withXFTPServer $ do
|
||||
alice <## "chat stopped"
|
||||
alice ##> "/_db export {\"archivePath\": \"./tests/tmp/alice-chat.zip\"}"
|
||||
alice <## "ok"
|
||||
let exportedDBs = [tmpPath ps <> "/alice_chat.db.exported", tmpPath ps <> "/alice_agent.db.exported"]
|
||||
forM_ exportedDBs $ \f -> B.writeFile f ""
|
||||
alice ##> "/_db delete"
|
||||
alice <## "ok"
|
||||
-- cannot start chat after delete
|
||||
alice ##> "/_start"
|
||||
alice <## "error: chat store changed, please restart chat"
|
||||
doesDirectoryExist "./tests/tmp/alice_files" `shouldReturn` False
|
||||
forM_ exportedDBs $ \f -> doesFileExist f `shouldReturn` False
|
||||
alice ##> "/_db import {\"archivePath\": \"./tests/tmp/alice-chat.zip\"}"
|
||||
alice <## "ok"
|
||||
B.readFile "./tests/tmp/alice_files/test.jpg" `shouldReturn` src
|
||||
|
||||
Reference in New Issue
Block a user