diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b04a036c..19d5025e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ Release Notes ============= +## Next Release + +**New** + +- `DatabaseReader.backup(to destination: DatabaseWriter)` backups a database to another ([documentation](https://github.com/groue/GRDB.swift#backup)). + + ## 0.59.1 Released April 25, 2016 diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index c9186dfd4..5a6bcd4b5 100644 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -314,6 +314,18 @@ 567156651CB16729007DC145 /* DatabaseQueueFileAttributesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 563363DB1C95891E000BE133 /* DatabaseQueueFileAttributesTests.swift */; }; 567156671CB16729007DC145 /* GRDBCipher.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 560FC54D1CB003810014AA8E /* GRDBCipher.framework */; }; 567156711CB18050007DC145 /* EncryptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567156701CB18050007DC145 /* EncryptionTests.swift */; }; + 5672DE591CDB72520022BA81 /* DatabaseQueueBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE581CDB72520022BA81 /* DatabaseQueueBackupTests.swift */; }; + 5672DE5A1CDB72520022BA81 /* DatabaseQueueBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE581CDB72520022BA81 /* DatabaseQueueBackupTests.swift */; }; + 5672DE5B1CDB72520022BA81 /* DatabaseQueueBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE581CDB72520022BA81 /* DatabaseQueueBackupTests.swift */; }; + 5672DE5C1CDB72520022BA81 /* DatabaseQueueBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE581CDB72520022BA81 /* DatabaseQueueBackupTests.swift */; }; + 5672DE5D1CDB72520022BA81 /* DatabaseQueueBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE581CDB72520022BA81 /* DatabaseQueueBackupTests.swift */; }; + 5672DE5E1CDB72520022BA81 /* DatabaseQueueBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE581CDB72520022BA81 /* DatabaseQueueBackupTests.swift */; }; + 5672DE671CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE661CDB751D0022BA81 /* DatabasePoolBackupTests.swift */; }; + 5672DE681CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE661CDB751D0022BA81 /* DatabasePoolBackupTests.swift */; }; + 5672DE691CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE661CDB751D0022BA81 /* DatabasePoolBackupTests.swift */; }; + 5672DE6A1CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE661CDB751D0022BA81 /* DatabasePoolBackupTests.swift */; }; + 5672DE6B1CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE661CDB751D0022BA81 /* DatabasePoolBackupTests.swift */; }; + 5672DE6C1CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE661CDB751D0022BA81 /* DatabasePoolBackupTests.swift */; }; 5683C2691B4D445E00296494 /* libsqlite3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5683C2681B4D445E00296494 /* libsqlite3.dylib */; }; 568BE5231CB0371D00270F93 /* GRDBCipher.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 560FC54D1CB003810014AA8E /* GRDBCipher.framework */; }; 568E1CB21CB037BA008D97A6 /* sqlite3.h in Headers */ = {isa = PBXBuildFile; fileRef = 568BE5191CB035A900270F93 /* sqlite3.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -870,6 +882,8 @@ 567156151CB142AA007DC145 /* ReadOnlyDatabaseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReadOnlyDatabaseTests.swift; sourceTree = ""; }; 5671566C1CB16729007DC145 /* GRDBCipherOSXEncryptedTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GRDBCipherOSXEncryptedTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 567156701CB18050007DC145 /* EncryptionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EncryptionTests.swift; sourceTree = ""; }; + 5672DE581CDB72520022BA81 /* DatabaseQueueBackupTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueBackupTests.swift; sourceTree = ""; }; + 5672DE661CDB751D0022BA81 /* DatabasePoolBackupTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabasePoolBackupTests.swift; sourceTree = ""; }; 5683C2681B4D445E00296494 /* libsqlite3.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libsqlite3.dylib; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.4.sdk/usr/lib/libsqlite3.dylib; sourceTree = DEVELOPER_DIR; }; 568BE5191CB035A900270F93 /* sqlite3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = sqlite3.h; path = SQLCipher/src/sqlite3.h; sourceTree = ""; }; 568BE51F1CB0360500270F93 /* GRDB.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = GRDB.xcconfig; path = SQLCipher/GRDB.xcconfig; sourceTree = ""; }; @@ -1153,6 +1167,7 @@ 560A37A91C90084600949E71 /* DatabasePool */ = { isa = PBXGroup; children = ( + 5672DE661CDB751D0022BA81 /* DatabasePoolBackupTests.swift */, 569531331C919DF200CF1A2B /* DatabasePoolCollationTests.swift */, 560A37AA1C90085D00949E71 /* DatabasePoolConcurrencyTests.swift */, 563363DE1C959295000BE133 /* DatabasePoolFileAttributesTests.swift */, @@ -1236,6 +1251,7 @@ 563363BB1C93FD32000BE133 /* DatabaseQueue */ = { isa = PBXGroup; children = ( + 5672DE581CDB72520022BA81 /* DatabaseQueueBackupTests.swift */, 563363BC1C93FD5E000BE133 /* DatabaseQueueConcurrencyTests.swift */, 563363DB1C95891E000BE133 /* DatabaseQueueFileAttributesTests.swift */, ); @@ -2244,6 +2260,7 @@ 560FC5761CB00B880014AA8E /* DetachedRowTests.swift in Sources */, 560FC5771CB00B880014AA8E /* DatabaseQueueSchemaCacheTests.swift in Sources */, 560FC5781CB00B880014AA8E /* MetalRowTests.swift in Sources */, + 5672DE681CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */, 56193E971CD8A69E00F95862 /* FetchedRecordsControllerTests.swift in Sources */, 560FC5791CB00B880014AA8E /* PrimaryKeyMultipleTests.swift in Sources */, 560FC57A1CB00B880014AA8E /* DatabasePoolFileAttributesTests.swift in Sources */, @@ -2251,6 +2268,7 @@ 560FC57D1CB00B880014AA8E /* RecordInitializersTests.swift in Sources */, 560FC57E1CB00B880014AA8E /* MutablePersistableTests.swift in Sources */, 560FC57F1CB00B880014AA8E /* RowConvertible+FetchRequestTests.swift in Sources */, + 5672DE5A1CDB72520022BA81 /* DatabaseQueueBackupTests.swift in Sources */, 560FC5801CB00B880014AA8E /* DatabasePoolReadOnlyTests.swift in Sources */, 560FC5811CB00B880014AA8E /* RecordCopyTests.swift in Sources */, 560FC5821CB00B880014AA8E /* RawRepresentableTests.swift in Sources */, @@ -2322,6 +2340,7 @@ 567156231CB16729007DC145 /* PrimaryKeyNoneTests.swift in Sources */, 567156241CB16729007DC145 /* MinimalPrimaryKeyRowIDTests.swift in Sources */, 567156251CB16729007DC145 /* DictionaryRowTests.swift in Sources */, + 5672DE691CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */, 567156261CB16729007DC145 /* SelectStatementTests.swift in Sources */, 567156271CB16729007DC145 /* DatabaseCoderTests.swift in Sources */, 567156281CB16729007DC145 /* SQLSupportTests.swift in Sources */, @@ -2333,6 +2352,7 @@ 5671562E1CB16729007DC145 /* DatabasePoolCollationTests.swift in Sources */, 5671562F1CB16729007DC145 /* PrimaryKeySingleTests.swift in Sources */, 567156301CB16729007DC145 /* SQLiteStatementConversionTests.swift in Sources */, + 5672DE5B1CDB72520022BA81 /* DatabaseQueueBackupTests.swift in Sources */, 567156311CB16729007DC145 /* RecordFetchTests.swift in Sources */, 567156321CB16729007DC145 /* DatabasePoolFunctionTests.swift in Sources */, 567156341CB16729007DC145 /* DetachedRowTests.swift in Sources */, @@ -2464,6 +2484,7 @@ 56AFCA461CB1AA9900F48B96 /* PrimaryKeyMultipleTests.swift in Sources */, 56AFCA471CB1AA9900F48B96 /* MetalRowTests.swift in Sources */, 56AFCA491CB1AA9900F48B96 /* StatementArgumentsTests.swift in Sources */, + 5672DE6B1CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */, 56B15D0C1CD4C35100A24C8B /* FetchedRecordsControllerTests.swift in Sources */, 56AFCA4A1CB1AA9900F48B96 /* RecordInitializersTests.swift in Sources */, 56AFCA4B1CB1AA9900F48B96 /* MutablePersistableTests.swift in Sources */, @@ -2471,6 +2492,7 @@ 56AFCA4D1CB1AA9900F48B96 /* DatabasePoolReadOnlyTests.swift in Sources */, 56AFCA4E1CB1AA9900F48B96 /* DatabasePoolFileAttributesTests.swift in Sources */, 56AFCA4F1CB1AA9900F48B96 /* NSDataMemoryTests.swift in Sources */, + 5672DE5D1CDB72520022BA81 /* DatabaseQueueBackupTests.swift in Sources */, 56AFCA501CB1AA9900F48B96 /* RecordCopyTests.swift in Sources */, 56AFCA511CB1AA9900F48B96 /* ManagedDataControllerTests.swift in Sources */, 56AFCA521CB1AA9900F48B96 /* PersistableTests.swift in Sources */, @@ -2526,6 +2548,7 @@ 56AFCA8E1CB1ABC800F48B96 /* DatabaseCoderTests.swift in Sources */, 56AFCA8F1CB1ABC800F48B96 /* CollationTests.swift in Sources */, 56AFCA901CB1ABC800F48B96 /* SQLiteStatementConvertibleFetchTests.swift in Sources */, + 5672DE6C1CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */, 56AFCA911CB1ABC800F48B96 /* SQLSupportTests.swift in Sources */, 56AFCA921CB1ABC800F48B96 /* SelectStatementTests.swift in Sources */, 56AFCA931CB1ABC800F48B96 /* UpdateStatementTests.swift in Sources */, @@ -2537,6 +2560,7 @@ 56AFCA991CB1ABC800F48B96 /* RecordFetchTests.swift in Sources */, 56AFCA9A1CB1ABC800F48B96 /* SQLiteStatementConversionTests.swift in Sources */, 56AFCA9B1CB1ABC800F48B96 /* DatabasePoolFunctionTests.swift in Sources */, + 5672DE5E1CDB72520022BA81 /* DatabaseQueueBackupTests.swift in Sources */, 56AFCA9D1CB1ABC800F48B96 /* DetachedRowTests.swift in Sources */, 56AFCA9E1CB1ABC800F48B96 /* DatabaseQueueSchemaCacheTests.swift in Sources */, 56AFCA9F1CB1ABC800F48B96 /* PrimaryKeyMultipleTests.swift in Sources */, @@ -2668,6 +2692,7 @@ 56A238541B9C74A90082EB20 /* PrimaryKeyMultipleTests.swift in Sources */, 56FDECE31BB32DFD009AD709 /* MetalRowTests.swift in Sources */, 56DE7B121C3D93ED00861EB8 /* StatementArgumentsTests.swift in Sources */, + 5672DE6A1CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */, 56B15D0B1CD4C35100A24C8B /* FetchedRecordsControllerTests.swift in Sources */, 56A238681B9C74A90082EB20 /* RecordInitializersTests.swift in Sources */, 563363B01C933FF8000BE133 /* MutablePersistableTests.swift in Sources */, @@ -2675,6 +2700,7 @@ 56EA869F1C932597002BB4DF /* DatabasePoolReadOnlyTests.swift in Sources */, 563363E01C959295000BE133 /* DatabasePoolFileAttributesTests.swift in Sources */, 56EB0AB31BCD787300A3DC55 /* NSDataMemoryTests.swift in Sources */, + 5672DE5C1CDB72520022BA81 /* DatabaseQueueBackupTests.swift in Sources */, 56A2385E1B9C74A90082EB20 /* RecordCopyTests.swift in Sources */, 56FC78691BBAEB8C00CA1285 /* ManagedDataControllerTests.swift in Sources */, 563363B21C933FF8000BE133 /* PersistableTests.swift in Sources */, @@ -2745,6 +2771,7 @@ 56FDECE21BB32DFD009AD709 /* MetalRowTests.swift in Sources */, 56A238531B9C74A90082EB20 /* PrimaryKeyMultipleTests.swift in Sources */, 563363DF1C959295000BE133 /* DatabasePoolFileAttributesTests.swift in Sources */, + 5672DE671CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */, 56193E991CD8A6A000F95862 /* FetchedRecordsControllerTests.swift in Sources */, 56DE7B111C3D93ED00861EB8 /* StatementArgumentsTests.swift in Sources */, 56A238671B9C74A90082EB20 /* RecordInitializersTests.swift in Sources */, @@ -2752,6 +2779,7 @@ 56300B611C53C42C005A543B /* RowConvertible+FetchRequestTests.swift in Sources */, 56EA869E1C932597002BB4DF /* DatabasePoolReadOnlyTests.swift in Sources */, 56A2385D1B9C74A90082EB20 /* RecordCopyTests.swift in Sources */, + 5672DE591CDB72520022BA81 /* DatabaseQueueBackupTests.swift in Sources */, 56A238431B9C74A90082EB20 /* RawRepresentableTests.swift in Sources */, 56EB0AB21BCD787300A3DC55 /* NSDataMemoryTests.swift in Sources */, 563363B11C933FF8000BE133 /* PersistableTests.swift in Sources */, diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 17265714d..3743532ff 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -73,6 +73,10 @@ public final class Database { /// True if the database connection is currently in a transaction. public private(set) var isInsideTransaction: Bool = false + var lastErrorCode: Int32 { + return sqlite3_errcode(sqliteConnection) + } + var lastErrorMessage: String? { return String.fromCString(sqlite3_errmsg(sqliteConnection)) } diff --git a/GRDB/Core/DatabaseReader.swift b/GRDB/Core/DatabaseReader.swift index d093776c9..7ac52211d 100644 --- a/GRDB/Core/DatabaseReader.swift +++ b/GRDB/Core/DatabaseReader.swift @@ -1,3 +1,15 @@ +#if !SQLITE_HAS_CODEC + #if os(OSX) + import SQLiteMacOSX + #elseif os(iOS) + #if (arch(i386) || arch(x86_64)) + import SQLiteiPhoneSimulator + #else + import SQLiteiPhoneOS + #endif + #endif +#endif + /// The protocol for all types that can fetch values from a database. /// /// It is adopted by DatabaseQueue and DatabasePool. @@ -95,3 +107,70 @@ public protocol DatabaseReader : class { /// Remove a collation. func removeCollation(collation: DatabaseCollation) } + +extension DatabaseReader { + + // MARK: - Backup + + /// Copies the database contents into another database. + /// + /// The `backup` method blocks the current thread until the destination + /// database contains the same contents as the source database. + /// + /// When the source is a DatabasePool, concurrent writes can happen during + /// the backup. Those writes may, or may not, be reflected in the backup, + /// but they won't trigger any error. + /// + /// Conversely, the destination database **must not** be used during the + /// backup. Quoting https://www.sqlite.org/c3ref/backup_finish.html: + /// + /// > SQLite does not currently check to see if the application incorrectly + /// > accesses the destination database connection and so no error code is + /// > reported, but the operations may malfunction nevertheless. Use of the + /// > destination database connection while a backup is in progress might + /// > also also cause a mutex deadlock. + public func backup(to writer: DatabaseWriter) throws { + try backup(to: writer, afterBackupInit: nil, afterBackupStep: nil) + } + + func backup(to writer: DatabaseWriter, afterBackupInit: (() -> ())?, afterBackupStep: (() -> ())?) throws { + try read { dbFrom in + try writer.write { dbDest in + let backup = sqlite3_backup_init(dbDest.sqliteConnection, "main", dbFrom.sqliteConnection, "main") + // TODO: use the future Int <-> Pointer APIs https://github.com/apple/swift-evolution/blob/master/proposals/0016-initializers-for-converting-unsafe-pointers-to-ints.md + guard backup != unsafeBitCast(Int(SQLITE_ERROR), COpaquePointer.self) else { + throw DatabaseError(code: SQLITE_ERROR) + } + guard backup != nil else { + throw DatabaseError(code: dbDest.lastErrorCode, message: dbDest.lastErrorMessage) + } + + afterBackupInit?() + + do { + backupLoop: while true { + switch sqlite3_backup_step(backup, -1) { + case SQLITE_DONE: + afterBackupStep?() + break backupLoop + case SQLITE_OK: + afterBackupStep?() + case let code: + throw DatabaseError(code: code, message: dbDest.lastErrorMessage) + } + } + } catch { + sqlite3_backup_finish(backup) + throw error + } + + switch sqlite3_backup_finish(backup) { + case SQLITE_OK: + break + case let code: + throw DatabaseError(code: code, message: dbDest.lastErrorMessage) + } + } + } + } +} diff --git a/README.md b/README.md index f98302da8..f47f8ec74 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,8 @@ Documentation - [Migrations](#migrations): Transform your database as your application evolves. - [Database Changes Observation](#database-changes-observation): Perform post-commit and post-rollback actions. - [FetchedRecordsController](#fetchedrecordscontroller): Automatic database changes tracking, plus UITableView animations. -- [Encryption](#encryption) +- [Encryption](#encryption): Encrypt your database with SQLCipher. +- [Backup](#backup): Dump the content of a database to another. **Good to know** @@ -1244,6 +1245,7 @@ Before jumping in the low-level wagon, here is a reminder of most SQLite APIs us - Connections & statements, obviously. - Errors (pervasive) - [sqlite3_errmsg](https://www.sqlite.org/c3ref/errcode.html) + - [sqlite3_errcode](https://www.sqlite.org/c3ref/errcode.html) - Inserted Row IDs (`Database.lastInsertedRowID`). - [sqlite3_last_insert_rowid](https://www.sqlite.org/c3ref/last_insert_rowid.html) - Changes count (`Database.changesCount` and `Database.totalChangesCount`). @@ -1260,6 +1262,10 @@ Before jumping in the low-level wagon, here is a reminder of most SQLite APIs us - [sqlite3_update_hook](https://www.sqlite.org/c3ref/update_hook.html) - [sqlite3_commit_hook](https://www.sqlite.org/c3ref/commit_hook.html) - [sqlite3_rollback_hook](https://www.sqlite.org/c3ref/commit_hook.html) +- Backup (see [Backup](#backup)): + - [sqlite3_backup_init](https://www.sqlite.org/c3ref/backup_finish.html) + - [sqlite3_backup_step](https://www.sqlite.org/c3ref/backup_finish.html) + - [sqlite3_backup_finish](https://www.sqlite.org/c3ref/backup_finish.html) - Authorizations callbacks are *reserved* by GRDB: - [sqlite3_set_authorizer](https://www.sqlite.org/c3ref/set_authorizer.html) @@ -1274,7 +1280,8 @@ On top of the SQLite API described above, GRDB provides a toolkit for applicatio - [Migrations](#migrations): Transform your database as your application evolves. - [Database Changes Observation](#database-changes-observation): Perform post-commit and post-rollback actions. - [FetchedRecordsController](#fetchedrecordscontroller): Automatic database changes tracking, plus UITableView animations. -- [Encryption](#encryption) +- [Encryption](#encryption): Encrypt your database with SQLCipher. +- [Backup](#backup): Dump the content of a database to another. ## Records @@ -2554,6 +2561,29 @@ try dbQueue.changePassphrase("newSecret") ``` +## Backup + +**You can backup (copy) a database into another.** + +Backups can for example help you copying an in-memory database to and from a database file when you implement NSDocument subclasses. + +```swift +let source: DatabaseQueue = ... // or DatabasePool +let destination: DatabaseQueue = ... // or DatabasePool +try source.backup(to: destination) +``` + +The `backup` method blocks the current thread until the destination database contains the same contents as the source database. + +When the source is a [database pool](#database-pools), concurrent writes can happen during the backup. Those writes may, or may not, be reflected in the backup, but they won't trigger any error. + +Conversely, the destination database **must not** be used during the backup. Quoting [SQLite documentation](https://www.sqlite.org/c3ref/backup_finish.html): + +> SQLite does not currently check to see if the application incorrectly accesses the destination database connection and so no error code is reported, but the operations may malfunction nevertheless. Use of the destination database connection while a backup is in progress might also also cause a mutex deadlock. + +For a detailed discussion, see https://www.sqlite.org/c3ref/backup_finish.html. + + Good To Know ============ diff --git a/Tests/Public/Core/DatabasePool/DatabasePoolBackupTests.swift b/Tests/Public/Core/DatabasePool/DatabasePoolBackupTests.swift new file mode 100644 index 000000000..87093a345 --- /dev/null +++ b/Tests/Public/Core/DatabasePool/DatabasePoolBackupTests.swift @@ -0,0 +1,81 @@ +import XCTest +#if SQLITE_HAS_CODEC + @testable import GRDBCipher +#else + @testable import GRDB +#endif + +class DatabasePoolBackupTests: GRDBTestCase { + + func testBackup() { + assertNoError { + let source = try makeDatabasePool("source.sqlite") + let destination = try makeDatabasePool("destination.sqlite") + + try source.write { db in + try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") + try db.execute("INSERT INTO items (id) VALUES (NULL)") + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM items")!, 1) + } + + try source.backup(to: destination) + + destination.read { db in + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM items")!, 1) + } + + try source.write { db in + try db.execute("DROP TABLE items") + } + + try source.backup(to: destination) + + destination.read { db in + XCTAssertFalse(db.tableExists("items")) + } + } + } + + func testConcurrentWriteDuringBackup() { + assertNoError { + let source = try makeDatabasePool("source.sqlite") + let destination = try makeDatabasePool("destination.sqlite") + + try source.write { db in + try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") + try db.execute("INSERT INTO items (id) VALUES (NULL)") + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM items")!, 1) + } + + let s1 = dispatch_semaphore_create(0) + let s2 = dispatch_semaphore_create(0) + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { + dispatch_semaphore_wait(s1, DISPATCH_TIME_FOREVER) + try! source.writeInTransaction(.Immediate) { db in + try db.execute("INSERT INTO items (id) VALUES (NULL)") + dispatch_semaphore_signal(s2) + return .Commit + } + } + + try source.backup( + to: destination, + afterBackupInit: { + dispatch_semaphore_signal(s1) + dispatch_semaphore_wait(s2, DISPATCH_TIME_FOREVER) + }, + afterBackupStep: { + try! source.write { db in + try db.execute("INSERT INTO items (id) VALUES (NULL)") + } + }) + + source.read { db in + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM items")!, 3) + } + destination.read { db in + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM items")!, 2) + } + } + } +} diff --git a/Tests/Public/Core/DatabaseQueue/DatabaseQueueBackupTests.swift b/Tests/Public/Core/DatabaseQueue/DatabaseQueueBackupTests.swift new file mode 100644 index 000000000..e1a8be572 --- /dev/null +++ b/Tests/Public/Core/DatabaseQueue/DatabaseQueueBackupTests.swift @@ -0,0 +1,38 @@ +import XCTest +#if SQLITE_HAS_CODEC + import GRDBCipher +#else + import GRDB +#endif + +class DatabaseQueueBackupTests: GRDBTestCase { + + func testBackup() { + assertNoError { + let source = try makeDatabaseQueue("source.sqlite") + let destination = try makeDatabaseQueue("destination.sqlite") + + try source.inDatabase { db in + try db.execute("CREATE TABLE items (id INTEGER PRIMARY KEY)") + try db.execute("INSERT INTO items (id) VALUES (NULL)") + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM items")!, 1) + } + + try source.backup(to: destination) + + destination.inDatabase { db in + XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM items")!, 1) + } + + try source.inDatabase { db in + try db.execute("DROP TABLE items") + } + + try source.backup(to: destination) + + destination.inDatabase { db in + XCTAssertFalse(db.tableExists("items")) + } + } + } +}