Skip to content

Commit

Permalink
Merge pull request #31 from andreasley/fix-backuperror
Browse files Browse the repository at this point in the history
Improve backup error handling
  • Loading branch information
marcoarment authored Aug 26, 2024
2 parents d74a3e9 + 3c5625b commit f894c00
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 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 @@ -898,14 +902,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()
}

sqlite3_backup_finish(backup)
sqlite3_close(targetDbHandle)
guard stepResult == SQLITE_DONE else {
throw Blackbird.Database.Error.backupError(description: errorDesc(targetDbHandle))
}
}
}
}
Expand Down

0 comments on commit f894c00

Please sign in to comment.