Skip to content

Commit

Permalink
Improve backup error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasley committed Aug 26, 2024
1 parent 11170b8 commit 3c5625b
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions Sources/Blackbird/BlackbirdDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand All @@ -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)
Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit 3c5625b

Please sign in to comment.