Skip to content

Commit 69ca8fe

Browse files
committed
More convenient API for backup
1 parent d10c9da commit 69ca8fe

File tree

5 files changed

+42
-47
lines changed

5 files changed

+42
-47
lines changed

SQLite.xcodeproj/project.pbxproj

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
02A43A9922738CF100FEC494 /* Backup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02A43A9722738CF100FEC494 /* Backup.swift */; };
1212
02A43A9A22738CF100FEC494 /* Backup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02A43A9722738CF100FEC494 /* Backup.swift */; };
1313
02A43A9B22738CF100FEC494 /* Backup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02A43A9722738CF100FEC494 /* Backup.swift */; };
14-
02A43A9D22738E2900FEC494 /* BackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02A43A9C22738E2900FEC494 /* BackupTests.swift */; };
15-
02A43A9E22738E2900FEC494 /* BackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02A43A9C22738E2900FEC494 /* BackupTests.swift */; };
16-
02A43A9F22738E2900FEC494 /* BackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02A43A9C22738E2900FEC494 /* BackupTests.swift */; };
1714
03A65E641C6BB0F60062603F /* SQLite.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03A65E5A1C6BB0F50062603F /* SQLite.framework */; };
1815
03A65E721C6BB2D30062603F /* SQLite.h in Headers */ = {isa = PBXBuildFile; fileRef = EE247AD61C3F04ED00AE3E12 /* SQLite.h */; settings = {ATTRIBUTES = (Public, ); }; };
1916
03A65E731C6BB2D80062603F /* Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE247AF71C3F06E900AE3E12 /* Foundation.swift */; };
@@ -219,7 +216,6 @@
219216

220217
/* Begin PBXFileReference section */
221218
02A43A9722738CF100FEC494 /* Backup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Backup.swift; sourceTree = "<group>"; };
222-
02A43A9C22738E2900FEC494 /* BackupTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackupTests.swift; sourceTree = "<group>"; };
223219
03A65E5A1C6BB0F50062603F /* SQLite.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SQLite.framework; sourceTree = BUILT_PRODUCTS_DIR; };
224220
03A65E631C6BB0F60062603F /* SQLiteTests tvOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "SQLiteTests tvOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
225221
03A65E961C6BB3210062603F /* libsqlite3.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libsqlite3.tbd; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/lib/libsqlite3.tbd; sourceTree = DEVELOPER_DIR; };
@@ -407,7 +403,6 @@
407403
19A17E2695737FAB5D6086E3 /* fixtures */,
408404
EE247B1A1C3F137700AE3E12 /* AggregateFunctionsTests.swift */,
409405
EE247B1B1C3F137700AE3E12 /* BlobTests.swift */,
410-
02A43A9C22738E2900FEC494 /* BackupTests.swift */,
411406
EE247B1D1C3F137700AE3E12 /* ConnectionTests.swift */,
412407
EE247B1E1C3F137700AE3E12 /* CoreFunctionsTests.swift */,
413408
EE247B1F1C3F137700AE3E12 /* CustomFunctionsTests.swift */,
@@ -851,7 +846,6 @@
851846
03A65E911C6BB3030062603F /* SchemaTests.swift in Sources */,
852847
03A65E8D1C6BB3030062603F /* FTS4Tests.swift in Sources */,
853848
03A65E8C1C6BB3030062603F /* ExpressionTests.swift in Sources */,
854-
02A43A9F22738E2900FEC494 /* BackupTests.swift in Sources */,
855849
03A65E8E1C6BB3030062603F /* OperatorsTests.swift in Sources */,
856850
03A65E951C6BB3030062603F /* TestHelpers.swift in Sources */,
857851
19A17254FBA7894891F7297B /* FTS5Tests.swift in Sources */,
@@ -942,7 +936,6 @@
942936
EE247B351C3F142E00AE3E12 /* ValueTests.swift in Sources */,
943937
EE247B2F1C3F141E00AE3E12 /* QueryTests.swift in Sources */,
944938
EE247B221C3F137700AE3E12 /* AggregateFunctionsTests.swift in Sources */,
945-
02A43A9D22738E2900FEC494 /* BackupTests.swift in Sources */,
946939
EE247B2E1C3F141E00AE3E12 /* OperatorsTests.swift in Sources */,
947940
EE247B251C3F137700AE3E12 /* ConnectionTests.swift in Sources */,
948941
19A171E6FA242F72A308C594 /* FTS5Tests.swift in Sources */,
@@ -1002,7 +995,6 @@
1002995
EE247B551C3F3FC700AE3E12 /* ConnectionTests.swift in Sources */,
1003996
EE247B611C3F3FC700AE3E12 /* TestHelpers.swift in Sources */,
1004997
EE247B581C3F3FC700AE3E12 /* ExpressionTests.swift in Sources */,
1005-
02A43A9E22738E2900FEC494 /* BackupTests.swift in Sources */,
1006998
EE247B5E1C3F3FC700AE3E12 /* SetterTests.swift in Sources */,
1007999
EE247B5B1C3F3FC700AE3E12 /* QueryTests.swift in Sources */,
10081000
19A174D78559CD30679BCCCB /* FTS5Tests.swift in Sources */,

Sources/SQLite/Core/Backup.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ public final class Backup {
107107
///
108108
/// - targetConnection: The connection to the database to save backup into.
109109
///
110-
/// - targetConnection: The name of the database to save backup into.
110+
/// - targetName: The name of the database to save backup into.
111111
///
112112
/// Default: `.main`.
113113
///
114114
/// - sourceConnection: The connection to the database to backup.
115115
///
116-
/// - targetConnection: The name of the database to backup.
116+
/// - sourceName: The name of the database to backup.
117117
///
118118
/// Default: `.main`.
119119
///

Sources/SQLite/Core/Connection.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,34 @@ public final class Connection {
627627
}
628628
fileprivate typealias Collation = @convention(block) (UnsafeRawPointer, UnsafeRawPointer) -> Int32
629629
fileprivate var collations = [String: Collation]()
630-
630+
631+
// MARK: - Backup
632+
633+
/// Prepares a new backup for current connection.
634+
///
635+
/// - Parameters:
636+
///
637+
/// - databaseName: The name of the database to backup.
638+
///
639+
/// Default: `.main`
640+
///
641+
/// - targetConnection: The name of the database to save backup into.
642+
///
643+
/// - targetDatabaseName: The name of the database to save backup into.
644+
///
645+
/// Default: `.main`.
646+
///
647+
/// - Returns: A new database backup.
648+
649+
public func backup(databaseName: Backup.DatabaseName = .main,
650+
usingConnection targetConnection: Connection,
651+
andDatabaseName targetDatabaseName: Backup.DatabaseName = .main) throws -> Backup {
652+
return try Backup(targetConnection: targetConnection,
653+
targetName: targetDatabaseName,
654+
sourceConnection: self,
655+
sourceName: databaseName)
656+
}
657+
631658
// MARK: - Error Handling
632659

633660
func sync<T>(_ block: () throws -> T) rethrows -> T {

Tests/SQLiteTests/BackupTests.swift

Lines changed: 0 additions & 36 deletions
This file was deleted.

Tests/SQLiteTests/ConnectionTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ class ConnectionTests : SQLiteTestCase {
141141

142142
AssertSQL("BEGIN EXCLUSIVE TRANSACTION")
143143
}
144+
145+
func test_backup_copiesDatabase() throws {
146+
let target = try Connection()
147+
148+
try InsertUsers("alice", "betsy")
149+
150+
let backup = try db.backup(usingConnection: target)
151+
try backup.step()
152+
153+
let users = try target.prepare("SELECT email FROM users ORDER BY email")
154+
XCTAssertEqual(users.map { $0[0] as? String }, ["alice@example.com", "betsy@example.com"])
155+
}
144156

145157
func test_transaction_beginsAndCommitsTransactions() {
146158
let stmt = try! db.prepare("INSERT INTO users (email) VALUES (?)", "alice@example.com")

0 commit comments

Comments
 (0)