diff --git a/CHANGELOG.md b/CHANGELOG.md index be6021f2c..705dfa9f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,7 @@ All notable changes to this project will be documented in this file. GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: APIs flagged [**:fire: EXPERIMENTAL**](README.md#what-are-experimental-features). Those are unstable, and may break between any two minor releases of the library. - #### 5.x Releases @@ -70,9 +68,9 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: - [0.110.0](#01100), ... - + +- [#795](https://github.com/groue/GRDB.swift/pull/795): **Breaking Change** Enhanced support for WAL Checkpoints ## 5.0.0-beta.4 diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index fbe6a3605..da522e3f4 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -211,9 +211,9 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib /// This method must be called after database initialization func setup() throws { + setupBusyMode() setupDoubleQuotedStringLiterals() try setupForeignKeys() - setupBusyMode() setupDefaultFunctions() setupDefaultCollations() setupAuthorizer() @@ -621,11 +621,35 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib } } - // MARK: - Checkpoints + // MARK: - WAL Checkpoints - func checkpoint(_ kind: Database.CheckpointMode) throws { - let code = sqlite3_wal_checkpoint_v2(sqliteConnection, nil, kind.rawValue, nil, nil) - guard code == SQLITE_OK else { + /// Runs a WAL checkpoint. + /// + /// See https://www.sqlite.org/wal.html and + /// https://www.sqlite.org/c3ref/wal_checkpoint_v2.html for + /// more information. + /// + /// - parameter kind: The checkpoint mode (default passive) + /// - parameter dbName: The database name (default "main") + /// - returns: A tuple: + /// - `walFrameCount`: the total number of frames in the log file + /// - `checkpointedFrameCount`: the total number of checkpointed frames + /// in the log file + @discardableResult + public func checkpoint(_ kind: Database.CheckpointMode = .passive, on dbName: String? = "main") throws + -> (walFrameCount: Int, checkpointedFrameCount: Int) + { + SchedulingWatchdog.preconditionValidQueue(self) + var walFrameCount: CInt = -1 + var checkpointedFrameCount: CInt = -1 + let code = sqlite3_wal_checkpoint_v2(sqliteConnection, dbName, kind.rawValue, + &walFrameCount, &checkpointedFrameCount) + switch code { + case SQLITE_OK: + return (walFrameCount: Int(walFrameCount), checkpointedFrameCount: Int(checkpointedFrameCount)) + case SQLITE_MISUSE: + throw DatabaseError(resultCode: code) + default: throw DatabaseError(resultCode: code, message: lastErrorMessage) } } diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 2cab90a07..bf4d43f63 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -167,24 +167,6 @@ public final class DatabasePool: DatabaseWriter { } } -extension DatabasePool { - - // MARK: - WAL Checkpoints - - /// Runs a WAL checkpoint - /// - /// See https://www.sqlite.org/wal.html and - /// https://www.sqlite.org/c3ref/wal_checkpoint_v2.html) for - /// more information. - /// - /// - parameter kind: The checkpoint mode (default passive) - public func checkpoint(_ kind: Database.CheckpointMode = .passive) throws { - try writer.sync { db in - try db.checkpoint(kind) - } - } -} - extension DatabasePool { // MARK: - Memory management diff --git a/GRDB/Fixit/GRDB-5.0.swift b/GRDB/Fixit/GRDB-5.0.swift index f240beeff..caffee00c 100644 --- a/GRDB/Fixit/GRDB-5.0.swift +++ b/GRDB/Fixit/GRDB-5.0.swift @@ -86,6 +86,9 @@ extension DatabaseMigrator { } extension DatabasePool { + @available(*, unavailable, message: "Use pool.writeWithoutTransaction { $0.checkpoint() } instead") + public func checkpoint(_ kind: Database.CheckpointMode = .passive) throws { preconditionFailure() } + #if os(iOS) @available(*, unavailable, message: "Memory management is now enabled by default. This method does nothing.") public func setupMemoryManagement(in application: UIApplication) { preconditionFailure() } diff --git a/Tests/GRDBTests/DatabasePoolConcurrencyTests.swift b/Tests/GRDBTests/DatabasePoolConcurrencyTests.swift index 73e31caab..842da567e 100644 --- a/Tests/GRDBTests/DatabasePoolConcurrencyTests.swift +++ b/Tests/GRDBTests/DatabasePoolConcurrencyTests.swift @@ -442,8 +442,8 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { defer { s2.signal() } try dbPool.writeWithoutTransaction { db in try db.execute(sql: "DELETE FROM items") + try db.checkpoint() } - try dbPool.checkpoint() } catch { XCTFail("error: \(error)") } @@ -538,8 +538,8 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { defer { s2.signal() } try dbPool.writeWithoutTransaction { db in try db.execute(sql: "INSERT INTO items (id) VALUES (NULL)") + try db.checkpoint() } - try dbPool.checkpoint() } catch { XCTFail("error: \(error)") } @@ -759,8 +759,8 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { defer { s2.signal() } try dbPool.writeWithoutTransaction { db in try db.execute(sql: "DELETE FROM items") + try db.checkpoint() } - try dbPool.checkpoint() } catch { XCTFail("error: \(error)") } @@ -803,8 +803,8 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { defer { s2.signal() } try dbPool.writeWithoutTransaction { db in try db.execute(sql: "INSERT INTO items (id) VALUES (NULL)") + try db.checkpoint() } - try dbPool.checkpoint() } catch { XCTFail("error: \(error)") } diff --git a/Tests/GRDBTests/DatabaseSnapshotTests.swift b/Tests/GRDBTests/DatabaseSnapshotTests.swift index dc7bdb8e2..df3c8cb4a 100644 --- a/Tests/GRDBTests/DatabaseSnapshotTests.swift +++ b/Tests/GRDBTests/DatabaseSnapshotTests.swift @@ -270,7 +270,7 @@ class DatabaseSnapshotTests: GRDBTestCase { let counter = try Counter(dbPool: dbPool) try dbPool.write(counter.increment) let snapshot = try dbPool.makeSnapshot() - try? dbPool.checkpoint(.passive) // ignore if error or not, that's not the point + try? dbPool.writeWithoutTransaction { _ = try $0.checkpoint(.passive) } // ignore if error or not, that's not the point try XCTAssertEqual(snapshot.read(counter.value), 1) try dbPool.write(counter.increment) try XCTAssertEqual(snapshot.read(counter.value), 1) @@ -281,7 +281,7 @@ class DatabaseSnapshotTests: GRDBTestCase { let counter = try Counter(dbPool: dbPool) try dbPool.write(counter.increment) let snapshot = try dbPool.makeSnapshot() - try? dbPool.checkpoint(.full) // ignore if error or not, that's not the point + try? dbPool.writeWithoutTransaction { _ = try $0.checkpoint(.full) } // ignore if error or not, that's not the point try XCTAssertEqual(snapshot.read(counter.value), 1) try dbPool.write(counter.increment) try XCTAssertEqual(snapshot.read(counter.value), 1) @@ -292,7 +292,7 @@ class DatabaseSnapshotTests: GRDBTestCase { let counter = try Counter(dbPool: dbPool) try dbPool.write(counter.increment) let snapshot = try dbPool.makeSnapshot() - try? dbPool.checkpoint(.restart) // ignore if error or not, that's not the point + try? dbPool.writeWithoutTransaction { _ = try $0.checkpoint(.restart) } // ignore if error or not, that's not the point try XCTAssertEqual(snapshot.read(counter.value), 1) try dbPool.write(counter.increment) try XCTAssertEqual(snapshot.read(counter.value), 1) @@ -303,7 +303,7 @@ class DatabaseSnapshotTests: GRDBTestCase { let counter = try Counter(dbPool: dbPool) try dbPool.write(counter.increment) let snapshot = try dbPool.makeSnapshot() - try? dbPool.checkpoint(.truncate) // ignore if error or not, that's not the point + try? dbPool.writeWithoutTransaction { _ = try $0.checkpoint(.truncate) } // ignore if error or not, that's not the point try XCTAssertEqual(snapshot.read(counter.value), 1) try dbPool.write(counter.increment) try XCTAssertEqual(snapshot.read(counter.value), 1) diff --git a/Tests/GRDBTests/DatabaseTests.swift b/Tests/GRDBTests/DatabaseTests.swift index 14e735724..6bb8f265a 100644 --- a/Tests/GRDBTests/DatabaseTests.swift +++ b/Tests/GRDBTests/DatabaseTests.swift @@ -345,4 +345,34 @@ class DatabaseTests : GRDBTestCase { try db.execute(sql: "INSERT INTO t DEFAULT VALUES") } } + + func testCheckpoint() throws { + do { + // Not a WAL database + let dbQueue = try makeDatabaseQueue() + let result = try dbQueue.inDatabase { + try $0.checkpoint() + } + XCTAssertEqual(result.walFrameCount, -1) + XCTAssertEqual(result.checkpointedFrameCount, -1) + } + do { + // WAL database + let dbPool = try makeDatabasePool() + let result = try dbPool.writeWithoutTransaction { + try $0.checkpoint() + } + XCTAssertGreaterThanOrEqual(result.walFrameCount, 0) + XCTAssertGreaterThanOrEqual(result.checkpointedFrameCount, 0) + } + do { + // WAL database + TRUNCATE + let dbPool = try makeDatabasePool() + let result = try dbPool.writeWithoutTransaction { + try $0.checkpoint(.truncate) + } + XCTAssertEqual(result.walFrameCount, 0) + XCTAssertEqual(result.checkpointedFrameCount, 0) + } + } }