From 55af29ccebb8cce59c33c4d3d33b3851ba54ffbe Mon Sep 17 00:00:00 2001 From: Yuuki Nishiyama Date: Wed, 18 Nov 2026 15:21:01 -0800 Subject: [PATCH] Create the database directory when it is missing SQLite does not create directories, so opening a database in a missing directory failed. SQLiteEngine stores databases in the Documents directory, which exists in an app but not, for example, when Swift package tests run on the iOS Simulator without a host app. Then getSQLiteInstance() returned nil and saves, table creation and syncs were skipped with only a log line (this made BatterySensor's testTablesAreCreated fail on a fresh simulator). - Create the parent directory before opening the DatabaseQueue. - Test: a database in a missing nested directory can be opened. Co-Authored-By: Claude Opus 5.5 --- .../db/sqlite/SQLiteEngine.swift | 11 +++++++++++ .../com_awareframework_ios_coreTests.swift | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/Sources/com.awareframework.ios.core/db/sqlite/SQLiteEngine.swift b/Sources/com.awareframework.ios.core/db/sqlite/SQLiteEngine.swift index 790efe9..b3a7285 100644 --- a/Sources/com.awareframework.ios.core/db/sqlite/SQLiteEngine.swift +++ b/Sources/com.awareframework.ios.core/db/sqlite/SQLiteEngine.swift @@ -107,6 +107,7 @@ open class SQLiteEngine: Engine { try db.execute(sql: "PRAGMA foreign_keys = ON") } + try createParentDirectory(ofDatabaseAt: path) let queue = try retryLockedDatabaseWork { try DatabaseQueue(path: path, configuration: configuration) } @@ -147,6 +148,16 @@ open class SQLiteEngine: Engine { 0.05 * Double(attempt + 1) } + /// Creates the directory that will hold the database file, if it is missing. SQLite does not create + /// directories, so opening the database fails without it. The Documents directory exists in an app, + /// but not, for example, when Swift package tests run on the iOS Simulator without a host app. + static func createParentDirectory(ofDatabaseAt path: String) throws { + let directory = URL(fileURLWithPath: path).deletingLastPathComponent() + if !FileManager.default.fileExists(atPath: directory.path) { + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + } + } + private static func fileURL(for path: String) -> URL { FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] .appendingPathComponent(sqliteFileName(for: path)) diff --git a/Tests/com.awareframework.ios.coreTests/com_awareframework_ios_coreTests.swift b/Tests/com.awareframework.ios.coreTests/com_awareframework_ios_coreTests.swift index e518f44..1cc3762 100644 --- a/Tests/com.awareframework.ios.coreTests/com_awareframework_ios_coreTests.swift +++ b/Tests/com.awareframework.ios.coreTests/com_awareframework_ios_coreTests.swift @@ -373,6 +373,24 @@ class UnitTests: XCTestCase { XCTAssertNotNil(original.completionHandler) } + /// The directory of the database file is created when it is missing, so the database can be opened. + func testCreateParentDirectoryOfDatabase() throws { + let root = FileManager.default.temporaryDirectory.appendingPathComponent("aware_dir_test_\(UUID().uuidString)") + defer { try? FileManager.default.removeItem(at: root) } + let path = root.appendingPathComponent("nested/Documents/a.sqlite").path + XCTAssertFalse(FileManager.default.fileExists(atPath: root.path)) + + try SQLiteEngine.createParentDirectory(ofDatabaseAt: path) + var isDirectory: ObjCBool = false + XCTAssertTrue(FileManager.default.fileExists(atPath: root.appendingPathComponent("nested/Documents").path, + isDirectory: &isDirectory)) + XCTAssertTrue(isDirectory.boolValue) + XCTAssertNoThrow(try DatabaseQueue(path: path)) + + // An existing directory is left as it is + XCTAssertNoThrow(try SQLiteEngine.createParentDirectory(ofDatabaseAt: path)) + } + /// Makes a SQLite engine with an empty table `A` in its own database file. private func makeEmptySQLiteEngine(_ dbPath: String) -> SQLiteEngine? { let sensor = AwareSensor()