mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
README.md
This commit is contained in:
1 parent
2199dfb636
commit
b8696d80eb
2 files changed
+35
-20
No files matched your search
@@ -28,14 +28,22 @@ class DatabaseMigratorTests: GRDBTests {
|
||||
self.dbQueue = nil
|
||||
try! NSFileManager.defaultManager().removeItemAtPath(databasePath)
|
||||
}
|
||||
|
||||
|
||||
func testMigrator() {
|
||||
var migrator = DatabaseMigrator()
|
||||
migrator.registerMigration("createPersons") { db in
|
||||
try db.execute("CREATE TABLE persons (id INTEGER PRIMARY KEY, name TEXT)")
|
||||
try db.execute(
|
||||
"CREATE TABLE persons (" +
|
||||
"id INTEGER PRIMARY KEY, " +
|
||||
"name TEXT)")
|
||||
}
|
||||
migrator.registerMigration("createPets") { db in
|
||||
try db.execute("CREATE TABLE pets (id INTEGER PRIMARY KEY, masterID INTEGER NOT NULL REFERENCES persons(id), name TEXT)")
|
||||
try db.execute("CREATE TABLE pets (" +
|
||||
"id INTEGER PRIMARY KEY, " +
|
||||
"masterID INTEGER NOT NULL " +
|
||||
" REFERENCES persons(id) " +
|
||||
" ON DELETE CASCADE ON UPDATE CASCADE, " +
|
||||
"name TEXT)")
|
||||
}
|
||||
|
||||
assertNoError {
|
||||
@@ -45,11 +53,11 @@ class DatabaseMigratorTests: GRDBTests {
|
||||
XCTAssertTrue(db.tableExists("pets"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
migrator.registerMigration("destroyPersons") { db in
|
||||
try db.execute("DROP TABLE pets")
|
||||
}
|
||||
|
||||
|
||||
assertNoError {
|
||||
try migrator.migrate(dbQueue)
|
||||
try dbQueue.inDatabase { db -> Void in
|
||||
|
||||
@@ -19,31 +19,38 @@ A sqlite3 wrapper for Swift 2.
|
||||
## Usage (work in progress)
|
||||
|
||||
```swift
|
||||
// GRDB uses database queues just like ccgus/FMDB:
|
||||
// GRDB uses database queues for database accesses serialization,
|
||||
// just like ccgus/FMDB:
|
||||
|
||||
let dbQueue = try DatabaseQueue(path: "/tmp/GRDB.sqlite")
|
||||
|
||||
|
||||
// Queues serialize database accesses:
|
||||
// DatabaseMigrator sets up migrations:
|
||||
|
||||
try dbQueue.inDatabase { db -> Void in
|
||||
try db.execute(
|
||||
"CREATE TABLE persons (" +
|
||||
"id INTEGER PRIMARY KEY, " +
|
||||
"name TEXT, " +
|
||||
"age INT)")
|
||||
}
|
||||
|
||||
|
||||
// Transactions:
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
var migrator = DatabaseMigrator()
|
||||
migrator.registerMigration("createPersons") { db in
|
||||
try db.execute(
|
||||
"CREATE TABLE persons (" +
|
||||
"id INTEGER PRIMARY KEY, " +
|
||||
"name TEXT, " +
|
||||
"age INT)")
|
||||
|
||||
}
|
||||
migrator.registerMigration("createPets") { db in
|
||||
// Support for foreign keys is enabled by default:
|
||||
try db.execute("CREATE TABLE pets (" +
|
||||
"id INTEGER PRIMARY KEY, " +
|
||||
"masterID INTEGER NOT NULL " +
|
||||
" REFERENCES persons(id) " +
|
||||
" ON DELETE CASCADE ON UPDATE CASCADE, " +
|
||||
"name TEXT)")
|
||||
}
|
||||
|
||||
try migrator.migrate(dbQueue)
|
||||
|
||||
|
||||
// Transactions:
|
||||
|
||||
try dbQueue.inTransaction { db in
|
||||
try db.execute(
|
||||
"INSERT INTO persons (name, age) VALUES (?, ?)",
|
||||
bindings: ["Arthur", 36])
|
||||
|
||||
Reference in new issue
Block a user