From 0580705b1ea1ac0b115ec9fac97296cfdf395758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 27 Feb 2020 21:10:35 +0100 Subject: [PATCH] Enhance App Group Container documentation with database schema check --- Documentation/AppGroupContainers.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Documentation/AppGroupContainers.md b/Documentation/AppGroupContainers.md index 1a0d1d176..21e11721e 100644 --- a/Documentation/AppGroupContainers.md +++ b/Documentation/AppGroupContainers.md @@ -48,8 +48,10 @@ Since several processes may open the database at the same time, protect the crea private func openDatabase(at databaseURL: URL) throws -> DatabasePool { let dbPool = try DatabasePool(path: databaseURL.path) + // Perform here other database setups, such as defining // the database schema with a DatabaseMigrator. + try migrator.migrate(dbPool) return dbPool } ``` @@ -58,7 +60,8 @@ Since several processes may open the database at the same time, protect the crea ```swift /// Returns an initialized database pool at the shared location databaseURL, - /// or nil if the database was not created yet. + /// or nil if the database is not created yet, or does not have the required + /// schema version. func openSharedReadOnlyDatabase(at databaseURL: URL) throws -> DatabasePool? { let coordinator = NSFileCoordinator(filePresenter: nil) var coordinatorError: NSError? @@ -81,7 +84,14 @@ Since several processes may open the database at the same time, protect the crea do { var configuration = Configuration() configuration.readonly = true - return try DatabasePool(path: databaseURL.path, configuration: configuration) + let dbPool = try DatabasePool(path: databaseURL.path, configuration: configuration) + + // Check here if the database schema is correct, for example + // with a DatabaseMigrator. + if try migrator.hasUnappliedMigrations(in: dbPool) { + return nil + } + return dbPool } catch { if FileManager.default.fileExists(atPath: databaseURL.path) { throw error