Skip to content

Commit 9e162a8

Browse files
committed
Refactoring userVersion + adding tests
1 parent b9489a7 commit 9e162a8

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

Sources/SQLite/Core/Connection.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,22 @@ public final class Connection {
151151
public var totalChanges: Int {
152152
Int(sqlite3_total_changes(handle))
153153
}
154-
155-
/// The user version of the database.
154+
155+
/// Gets the user version of the database.
156+
/// See SQLite [PRAGMA user_version](https://sqlite.org/pragma.html#pragma_user_version)
157+
/// - Returns: the user version of the database
158+
public func getUserVersion() throws -> Int32? {
159+
guard let userVersion = try scalar("PRAGMA user_version") as? Int64 else {
160+
return nil
161+
}
162+
return Int32(userVersion)
163+
}
164+
165+
/// Sets the user version of the database.
156166
/// See SQLite [PRAGMA user_version](https://sqlite.org/pragma.html#pragma_user_version)
157-
public var userVersion: Int32 {
158-
get { return Int32(try! scalar("PRAGMA user_version") as! Int64)}
159-
set { try! run("PRAGMA user_version = \(newValue)") }
167+
/// - Parameter userVersion: the new user version of the database
168+
public func setUserVersion(to userVersion: Int32) throws {
169+
try run("PRAGMA user_version = \(userVersion)")
160170
}
161171

162172
// MARK: - Execute

Tests/SQLiteTests/ConnectionTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ class ConnectionTests: SQLiteTestCase {
9696
XCTAssertEqual(2, db.totalChanges)
9797
}
9898

99+
func test_userVersion() {
100+
try! db.setUserVersion(to: 2)
101+
XCTAssertEqual(2, try! db.getUserVersion()!)
102+
}
103+
99104
func test_prepare_preparesAndReturnsStatements() {
100105
_ = try! db.prepare("SELECT * FROM users WHERE admin = 0")
101106
_ = try! db.prepare("SELECT * FROM users WHERE admin = ?", 0)

0 commit comments

Comments
 (0)