diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index bfc216250..bbe0bf3c2 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -569,14 +569,10 @@ public final class Database { /// Grants read-only access, starting SQLite 3.8.0 func readOnly(_ block: () throws -> T) throws -> T { + try beginReadOnly() return try throwingFirstError( - execute: { - try beginReadOnly() - return try block() - }, - finally: { - try endReadOnly() - }) + execute: block, + finally: endReadOnly) } // MARK: - Authorizer diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index 1501dfe46..9d39cd503 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -285,15 +285,14 @@ extension DatabaseWriter { // https://discuss.zetetic.net/t/using-the-sqlite-online-backup-api/2631 // So we'll drop all database objects one after the other. try writeWithoutTransaction { db in + // Prevent foreign keys from messing with drop table statements let foreignKeysEnabled = try Bool.fetchOne(db, sql: "PRAGMA foreign_keys")! + if foreignKeysEnabled { + try db.execute(sql: "PRAGMA foreign_keys = OFF") + } try throwingFirstError( execute: { - // Prevent foreign keys from messing with drop table statements - if foreignKeysEnabled { - try db.execute(sql: "PRAGMA foreign_keys = OFF") - } - // Remove all database objects, one after the other try db.inTransaction { let sql = "SELECT type, name FROM sqlite_master WHERE name NOT LIKE 'sqlite_%'" diff --git a/GRDB/Migration/Migration.swift b/GRDB/Migration/Migration.swift index a969721c9..24f44b3e0 100644 --- a/GRDB/Migration/Migration.swift +++ b/GRDB/Migration/Migration.swift @@ -46,15 +46,15 @@ struct Migration { } private func runWithDisabledForeignKeys(_ db: Database) throws { + // Support for database alterations described at + // https://www.sqlite.org/lang_altertable.html#otheralter + // + // > 1. If foreign key constraints are enabled, disable them using + // > PRAGMA foreign_keys=OFF. + try db.execute(sql: "PRAGMA foreign_keys = OFF") + try throwingFirstError( execute: { - // Support for database alterations described at - // https://www.sqlite.org/lang_altertable.html#otheralter - // - // > 1. If foreign key constraints are enabled, disable them using - // > PRAGMA foreign_keys=OFF. - try db.execute(sql: "PRAGMA foreign_keys = OFF") - // > 2. Start a transaction. try db.inTransaction(.immediate) { try migrate(db) diff --git a/GRDB/Utils/Utils.swift b/GRDB/Utils/Utils.swift index ecf9f3abf..75f426d49 100644 --- a/GRDB/Utils/Utils.swift +++ b/GRDB/Utils/Utils.swift @@ -126,27 +126,13 @@ extension Character { /// Makes sure the `finally` function is executed even if `execute` throws, and /// rethrows the eventual first thrown error. /// -/// For example: +/// Usage: /// -/// // Does not throw +/// try setup() /// try throwingFirstError( -/// execute: { }, -/// finally: { }) -/// -/// // throws A -/// try throwingFirstError( -/// execute: { throw A }, -/// finally: { }) -/// -/// // throws B -/// try throwingFirstError( -/// execute: { }, -/// finally: { throw B }) -/// -/// // throws A -/// try throwingFirstError( -/// execute: { throw A }, -/// finally: { throw B }) +/// execute: work, +/// finally: cleanup) +@inline(__always) func throwingFirstError(execute: () throws -> T, finally: () throws -> Void) throws -> T { do { let result = try execute()