From 11170b8b99de863f5511d067bf14113140666674 Mon Sep 17 00:00:00 2001 From: Andreas Ley Date: Mon, 26 Aug 2024 13:47:26 +0200 Subject: [PATCH 1/2] Improve backup error handling --- Sources/Blackbird/BlackbirdDatabase.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/Blackbird/BlackbirdDatabase.swift b/Sources/Blackbird/BlackbirdDatabase.swift index 440ff8f..e2ded3a 100644 --- a/Sources/Blackbird/BlackbirdDatabase.swift +++ b/Sources/Blackbird/BlackbirdDatabase.swift @@ -898,11 +898,15 @@ extension Blackbird { let remainingPages = sqlite3_backup_remaining(backup) let totalPages = sqlite3_backup_pagecount(backup) let backedUpPages = totalPages - remainingPages - print("Backed up \(backedUpPages) pages of \(totalPages)\n") + print("Backed up \(backedUpPages) pages of \(totalPages)") } await Task.yield() } + + guard stepResult == SQLITE_DONE else { + throw Blackbird.Database.Error.backupError(description: errorDesc(targetDbHandle)) + } sqlite3_backup_finish(backup) sqlite3_close(targetDbHandle) From 3c5625b5f516b5cb78d286a824afa22c2b112022 Mon Sep 17 00:00:00 2001 From: Andreas Ley Date: Mon, 26 Aug 2024 13:56:12 +0200 Subject: [PATCH 2/2] Improve backup error handling --- Sources/Blackbird/BlackbirdDatabase.swift | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Sources/Blackbird/BlackbirdDatabase.swift b/Sources/Blackbird/BlackbirdDatabase.swift index e2ded3a..7618fbb 100644 --- a/Sources/Blackbird/BlackbirdDatabase.swift +++ b/Sources/Blackbird/BlackbirdDatabase.swift @@ -878,7 +878,9 @@ extension Blackbird { guard let targetDbHandle else { throw Error.cannotOpenDatabaseAtPath(path: targetPath, description: "SQLite cannot allocate memory") } - + + defer { sqlite3_close(targetDbHandle) } + guard openResult == SQLITE_OK else { let code = sqlite3_errcode(targetDbHandle) let msg = String(cString: sqlite3_errmsg(targetDbHandle), encoding: .utf8) ?? "(unknown)" @@ -890,6 +892,8 @@ extension Blackbird { throw Blackbird.Database.Error.backupError(description: errorDesc(targetDbHandle)) } + defer { sqlite3_backup_finish(backup) } + var stepResult = SQLITE_OK while stepResult == SQLITE_OK || stepResult == SQLITE_BUSY || stepResult == SQLITE_LOCKED { stepResult = sqlite3_backup_step(backup, pagesPerStep) @@ -903,13 +907,10 @@ extension Blackbird { await Task.yield() } - + guard stepResult == SQLITE_DONE else { throw Blackbird.Database.Error.backupError(description: errorDesc(targetDbHandle)) } - - sqlite3_backup_finish(backup) - sqlite3_close(targetDbHandle) } } }