From e79b4e25db9ea1954a135db1156446be4e496997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 24 Aug 2023 19:46:31 +0100 Subject: [PATCH] Apply Configuration.journalMode --- GRDB/Core/DatabasePool.swift | 37 +++++------------------------------ GRDB/Core/DatabaseQueue.swift | 12 ++++++++++++ 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 602cea60b..7dd0525fc 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -75,39 +75,12 @@ public final class DatabasePool { purpose: "reader.\(readerCount)") }) - // Activate WAL Mode unless readonly + // Set up journal mode unless readonly if !configuration.readonly { - try writer.sync { db in - let journalMode = try String.fetchOne(db, sql: "PRAGMA journal_mode = WAL") - guard journalMode == "wal" else { - throw DatabaseError(message: "could not activate WAL Mode at path: \(path)") - } - - // https://www.sqlite.org/pragma.html#pragma_synchronous - // > Many applications choose NORMAL when in WAL mode - try db.execute(sql: "PRAGMA synchronous = NORMAL") - - // Make sure a non-empty wal file exists. - // - // The presence of the wal file avoids an SQLITE_CANTOPEN (14) - // error when the user opens a pool and reads from it. - // See . - // - // The non-empty wal file avoids an SQLITE_ERROR (1) error - // when the user opens a pool and creates a wal snapshot - // (which happens when starting a ValueObservation). - // See . - let walPath = path + "-wal" - if try FileManager.default.fileExists(atPath: walPath) == false - || (URL(fileURLWithPath: walPath).resourceValues(forKeys: [.fileSizeKey]).fileSize ?? 0) == 0 - { - try db.inSavepoint { - try db.execute(sql: """ - CREATE TABLE grdb_issue_102 (id INTEGER PRIMARY KEY); - DROP TABLE grdb_issue_102; - """) - return .commit - } + switch configuration.journalMode { + case .default, .wal: + try writer.sync { + try $0.setUpWALMode() } } } diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index 9912e05c0..79252cb25 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -50,6 +50,18 @@ public final class DatabaseQueue { configuration: configuration, defaultLabel: "GRDB.DatabaseQueue") + // Set up journal mode unless readonly + if !configuration.readonly { + switch configuration.journalMode { + case .default: + break + case .wal: + try writer.sync { + try $0.setUpWALMode() + } + } + } + setupSuspension() // Be a nice iOS citizen, and don't consume too much memory