From 8a6a94f0fb76dad4e623d49879095151c4c880bc Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 16 Sep 2026 13:18:57 -0700 Subject: [PATCH] Report failures from writing to the database. --- .../RSDatabase/FMDatabase+Extras.swift | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/Modules/RSDatabase/Sources/RSDatabase/FMDatabase+Extras.swift b/Modules/RSDatabase/Sources/RSDatabase/FMDatabase+Extras.swift index b7401a295..5b3de7b2c 100644 --- a/Modules/RSDatabase/Sources/RSDatabase/FMDatabase+Extras.swift +++ b/Modules/RSDatabase/Sources/RSDatabase/FMDatabase+Extras.swift @@ -24,13 +24,20 @@ public extension FMDatabase { return database } - func executeUpdateInTransaction(_ sql: String, withArgumentsIn parameters: [Any]? = nil) { + /// Returns false if the update failed and the transaction was rolled back. + @discardableResult + func executeUpdateInTransaction(_ sql: String, withArgumentsIn parameters: [Any]? = nil) -> Bool { beginTransaction() guard executeUpdate(sql, withArgumentsIn: parameters) else { + // Capture the error before rollback() overwrites the last-error state. + let code = lastErrorCode() + let message = lastErrorMessage() ?? "unknown" + let path = databasePath() ?? "unknown" rollback() - return + Self.logger.error("Update failed on \(path, privacy: .public) — SQLite \(code, privacy: .public): \(message, privacy: .public)") + return false } - commit() + return commit() } private static let logger = Logger(subsystem: logSubsystem, category: "FMDatabase") @@ -68,13 +75,20 @@ public extension FMDatabase { } } - func insertRows(_ dictionaries: [DatabaseDictionary], insertType: RSDatabaseInsertType, tableName: String) { + /// Returns false if any row failed to insert. Every row is attempted either way. + @discardableResult + func insertRows(_ dictionaries: [DatabaseDictionary], insertType: RSDatabaseInsertType, tableName: String) -> Bool { + var didInsertAllRows = true for dictionary in dictionaries { - insertRow(dictionary, insertType: insertType, tableName: tableName) + if !insertRow(dictionary, insertType: insertType, tableName: tableName) { + didInsertAllRows = false + } } + return didInsertAllRows } - func insertRow(_ dictionary: DatabaseDictionary, insertType: RSDatabaseInsertType, tableName: String) { + @discardableResult + func insertRow(_ dictionary: DatabaseDictionary, insertType: RSDatabaseInsertType, tableName: String) -> Bool { rs_insertRow(with: dictionary, insertType: insertType, tableName: tableName) }