mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
60 lines
2.1 KiB
Swift
60 lines
2.1 KiB
Swift
import GRDB
|
|
|
|
/// A type responsible for initializing the application database.
|
|
///
|
|
/// See AppDelegate.setupDatabase()
|
|
struct AppDatabase {
|
|
|
|
/// Creates a fully initialized database at path
|
|
static func openDatabase(atPath path: String) throws -> DatabaseQueue {
|
|
// Connect to the database
|
|
// See https://github.com/groue/GRDB.swift/#database-connections
|
|
dbQueue = try DatabaseQueue(path: path)
|
|
|
|
// Use DatabaseMigrator to define the database schema
|
|
// See https://github.com/groue/GRDB.swift/#migrations
|
|
try migrator.migrate(dbQueue)
|
|
|
|
return dbQueue
|
|
}
|
|
|
|
/// The DatabaseMigrator that defines the database schema.
|
|
///
|
|
/// This migrator is exposed so that migrations can be tested.
|
|
// See https://github.com/groue/GRDB.swift/#migrations
|
|
static var migrator: DatabaseMigrator {
|
|
var migrator = DatabaseMigrator()
|
|
|
|
migrator.registerMigration("createPlayer") { db in
|
|
// Create a table
|
|
// See https://github.com/groue/GRDB.swift#create-tables
|
|
try db.create(table: "player") { t in
|
|
// An integer primary key auto-generates unique IDs
|
|
t.column("id", .integer).primaryKey()
|
|
|
|
// Sort player names in a localized case insensitive fashion by default
|
|
// See https://github.com/groue/GRDB.swift/#unicode
|
|
t.column("name", .text).notNull().collate(.localizedCaseInsensitiveCompare)
|
|
|
|
t.column("score", .integer).notNull()
|
|
}
|
|
}
|
|
|
|
migrator.registerMigration("fixtures") { db in
|
|
// Populate the players table with random data
|
|
for _ in 0..<8 {
|
|
var player = Player(id: nil, name: Player.randomName(), score: Player.randomScore())
|
|
try player.insert(db)
|
|
}
|
|
}
|
|
|
|
// // Migrations for future application versions will be inserted here:
|
|
// migrator.registerMigration(...) { db in
|
|
// ...
|
|
// }
|
|
|
|
return migrator
|
|
}
|
|
}
|
|
|