iOS/Sources/Shared/Database/GRDB+Initialization.swift

53 lines
1.7 KiB
Swift

import Foundation
import GRDB
public extension DatabaseQueue {
// Following GRDB cocnurrency rules, we have just one database instance
// https://swiftpackageindex.com/groue/grdb.swift/v6.29.3/documentation/grdb/concurrency#Concurrency-Rules
static var appDatabase: DatabaseQueue = {
do {
let database = try DatabaseQueue(path: databasePath())
// Create tables if needed
DatabaseQueue.tables().forEach { $0.createIfNeeded(database: database) }
#if targetEnvironment(simulator)
print("GRDB App database is stored at \(AppConstants.appGRDBFile.description)")
#endif
return database
} catch {
let errorMessage = "Failed to initialize GRDB, error: \(error.localizedDescription)"
Current.Log.error(errorMessage)
fatalError(errorMessage)
}
}()
static func databasePath() -> String {
// Path for tests
if ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil {
let tempDirectory = NSTemporaryDirectory()
return (tempDirectory as NSString).appendingPathComponent("test_database.sqlite")
} else {
// Path for App use
return AppConstants.appGRDBFile.path
}
}
internal static func tables() -> [DatabaseTableProtocol] {
[
HAppEntityTable(),
WatchConfigTable(),
CarPlayConfigTable(),
AssistPipelinesTable(),
ClientEventTable(),
AppEntityRegistryListForDisplayTable(),
AppPanelTable(),
CustomWidgetTable(),
]
}
}
protocol DatabaseTableProtocol {
func createIfNeeded(database: DatabaseQueue)
}