Skip to content

Commit 2fc0dec

Browse files
committed
Fix tests on older version of SQLite
1 parent d7c2635 commit 2fc0dec

File tree

6 files changed

+57
-9
lines changed

6 files changed

+57
-9
lines changed

.swiftlint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ disabled_rules: # rule identifiers to exclude from running
33
- operator_whitespace
44
- large_tuple
55
- closure_parameter_position
6+
- inclusive_language # sqlite_master etc.
67
included: # paths to include during linting. `--path` is ignored if present. takes precendence over `excluded`.
78
- Sources
89
- Tests

SQLite.xcodeproj/project.pbxproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@
203203
DB58B21228FB864300F8EEA4 /* SchemaReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB58B21028FB864300F8EEA4 /* SchemaReader.swift */; };
204204
DB58B21328FB864300F8EEA4 /* SchemaReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB58B21028FB864300F8EEA4 /* SchemaReader.swift */; };
205205
DB58B21428FB864300F8EEA4 /* SchemaReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB58B21028FB864300F8EEA4 /* SchemaReader.swift */; };
206+
DB58B21628FC7C4600F8EEA4 /* SQLiteFeature.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB58B21528FC7C4600F8EEA4 /* SQLiteFeature.swift */; };
207+
DB58B21728FC7C4600F8EEA4 /* SQLiteFeature.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB58B21528FC7C4600F8EEA4 /* SQLiteFeature.swift */; };
208+
DB58B21828FC7C4600F8EEA4 /* SQLiteFeature.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB58B21528FC7C4600F8EEA4 /* SQLiteFeature.swift */; };
209+
DB58B21928FC7C4600F8EEA4 /* SQLiteFeature.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB58B21528FC7C4600F8EEA4 /* SQLiteFeature.swift */; };
206210
DB7C5DA628D7C9B6006395CF /* SQLiteVersion.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB7C5DA528D7C9B6006395CF /* SQLiteVersion.swift */; };
207211
DB7C5DA728D7C9B6006395CF /* SQLiteVersion.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB7C5DA528D7C9B6006395CF /* SQLiteVersion.swift */; };
208212
DB7C5DA828D7C9B6006395CF /* SQLiteVersion.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB7C5DA528D7C9B6006395CF /* SQLiteVersion.swift */; };
@@ -334,6 +338,7 @@
334338
997DF2AD287FC06D00F8DF95 /* Query+with.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Query+with.swift"; sourceTree = "<group>"; };
335339
A121AC451CA35C79005A31D1 /* SQLite.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SQLite.framework; sourceTree = BUILT_PRODUCTS_DIR; };
336340
DB58B21028FB864300F8EEA4 /* SchemaReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SchemaReader.swift; sourceTree = "<group>"; };
341+
DB58B21528FC7C4600F8EEA4 /* SQLiteFeature.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLiteFeature.swift; sourceTree = "<group>"; };
337342
DB7C5DA528D7C9B6006395CF /* SQLiteVersion.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLiteVersion.swift; sourceTree = "<group>"; };
338343
EE247AD31C3F04ED00AE3E12 /* SQLite.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SQLite.framework; sourceTree = BUILT_PRODUCTS_DIR; };
339344
EE247AD61C3F04ED00AE3E12 /* SQLite.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SQLite.h; sourceTree = "<group>"; };
@@ -580,6 +585,7 @@
580585
19A175A9CB446640AE6F2200 /* Connection+Aggregation.swift */,
581586
3DF7B78728842972005DD8CA /* Connection+Attach.swift */,
582587
3DF7B790288449BA005DD8CA /* URIQueryParameter.swift */,
588+
DB58B21528FC7C4600F8EEA4 /* SQLiteFeature.swift */,
583589
DB7C5DA528D7C9B6006395CF /* SQLiteVersion.swift */,
584590
19A17F285B767BFACD96714B /* Connection+Pragmas.swift */,
585591
);
@@ -970,6 +976,7 @@
970976
19A17073552293CA063BEA66 /* Result.swift in Sources */,
971977
997DF2B0287FC06D00F8DF95 /* Query+with.swift in Sources */,
972978
19A179B59450FE7C4811AB8A /* Connection+Aggregation.swift in Sources */,
979+
DB58B21828FC7C4600F8EEA4 /* SQLiteFeature.swift in Sources */,
973980
19A17FC07731779C1B8506FA /* SchemaChanger.swift in Sources */,
974981
19A1740EACD47904AA24B8DC /* SchemaDefinitions.swift in Sources */,
975982
19A1750EF4A5F92954A451FF /* Connection+Schema.swift in Sources */,
@@ -1030,6 +1037,7 @@
10301037
DB7C5DA928D7C9B6006395CF /* SQLiteVersion.swift in Sources */,
10311038
3D67B3EA1DB246D100A4F4C6 /* Value.swift in Sources */,
10321039
3D67B3EB1DB246D100A4F4C6 /* FTS4.swift in Sources */,
1040+
DB58B21928FC7C4600F8EEA4 /* SQLiteFeature.swift in Sources */,
10331041
3D67B3EC1DB246D100A4F4C6 /* RTree.swift in Sources */,
10341042
3D67B3ED1DB246D100A4F4C6 /* FTS5.swift in Sources */,
10351043
3D67B3EE1DB246D100A4F4C6 /* AggregateFunctions.swift in Sources */,
@@ -1089,6 +1097,7 @@
10891097
19A173EFEF0B3BD0B3ED406C /* Result.swift in Sources */,
10901098
997DF2AE287FC06D00F8DF95 /* Query+with.swift in Sources */,
10911099
19A176376CB6A94759F7980A /* Connection+Aggregation.swift in Sources */,
1100+
DB58B21628FC7C4600F8EEA4 /* SQLiteFeature.swift in Sources */,
10921101
19A1773A335CAB9D0AE14E8E /* SchemaChanger.swift in Sources */,
10931102
19A17BA13FD35F058787B7D3 /* SchemaDefinitions.swift in Sources */,
10941103
19A174506543905D71BF0518 /* Connection+Schema.swift in Sources */,
@@ -1168,6 +1177,7 @@
11681177
19A17F1B3F0A3C96B5ED6D64 /* Result.swift in Sources */,
11691178
997DF2AF287FC06D00F8DF95 /* Query+with.swift in Sources */,
11701179
19A170ACC97B19730FB7BA4D /* Connection+Aggregation.swift in Sources */,
1180+
DB58B21728FC7C4600F8EEA4 /* SQLiteFeature.swift in Sources */,
11711181
19A177290558991BCC60E4E3 /* SchemaChanger.swift in Sources */,
11721182
19A17B0DF1DDB6BBC9C95D64 /* SchemaDefinitions.swift in Sources */,
11731183
19A17F0BF02896E1664F4090 /* Connection+Schema.swift in Sources */,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Foundation
2+
3+
enum SQLiteFeature {
4+
case partialIntegrityCheck // PRAGMA integrity_check(table)
5+
case sqliteSchemaTable // sqlite_master => sqlite_schema
6+
7+
func isSupported(by version: SQLiteVersion) -> Bool {
8+
switch self {
9+
case .partialIntegrityCheck, .sqliteSchemaTable:
10+
return version > SQLiteVersion(major: 3, minor: 33)
11+
}
12+
}
13+
}
14+
15+
extension Connection {
16+
func supports(_ feature: SQLiteFeature) -> Bool {
17+
feature.isSupported(by: sqliteVersion)
18+
}
19+
}

Sources/SQLite/Schema/Connection+Schema.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ public extension Connection {
2525

2626
// This pragma does a low-level formatting and consistency check of the database.
2727
// https://sqlite.org/pragma.html#pragma_integrity_check
28-
func integrityCheck(table: String? = nil, maxErrors: Int? = nil) throws -> [String] {
29-
try run("PRAGMA integrity_check" + (table.map { "(\($0.quote()))" } ?? ""))
28+
func integrityCheck(table: String? = nil) throws -> [String] {
29+
precondition(table == nil || supports(.partialIntegrityCheck), "partial integrity check not supported")
30+
31+
return try run("PRAGMA integrity_check" + (table.map { "(\($0.quote()))" } ?? ""))
3032
.compactMap { $0[0] as? String }
3133
.filter { $0 != "ok" }
3234
}

Sources/SQLite/Schema/SchemaReader.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class SchemaReader {
3838
public func objectDefinitions(name: String? = nil,
3939
type: ObjectDefinition.ObjectType? = nil,
4040
temp: Bool = false) throws -> [ObjectDefinition] {
41-
var query: QueryType = temp ? SchemaTable.tempName : SchemaTable.name
41+
var query: QueryType = connection.schemaTable(temp: temp)
4242
if let name = name {
4343
query = query.where(SchemaTable.nameColumn == name)
4444
}
@@ -99,9 +99,9 @@ public class SchemaReader {
9999
column: row[ForeignKeyListTable.fromColumn],
100100
primaryKey: row[ForeignKeyListTable.toColumn],
101101
onUpdate: row[ForeignKeyListTable.onUpdateColumn] == TableBuilder.Dependency.noAction.rawValue
102-
? nil : row[ForeignKeyListTable.onUpdateColumn],
102+
? nil : row[ForeignKeyListTable.onUpdateColumn],
103103
onDelete: row[ForeignKeyListTable.onDeleteColumn] == TableBuilder.Dependency.noAction.rawValue
104-
? nil : row[ForeignKeyListTable.onDeleteColumn]
104+
? nil : row[ForeignKeyListTable.onDeleteColumn]
105105
)
106106
}
107107
}
@@ -110,9 +110,9 @@ public class SchemaReader {
110110
try objectDefinitions(type: .table)
111111
.map { table in
112112
TableDefinition(
113-
name: table.name,
114-
columns: try columnDefinitions(table: table.name),
115-
indexes: try indexDefinitions(table: table.name)
113+
name: table.name,
114+
columns: try columnDefinitions(table: table.name),
115+
indexes: try indexDefinitions(table: table.name)
116116
)
117117
}
118118
}
@@ -129,6 +129,10 @@ private class SchemaTable {
129129
internal static let name = Table("sqlite_schema", database: "main")
130130
internal static let tempName = Table("sqlite_schema", database: "temp")
131131

132+
// legacy table names
133+
internal static let masterName = Table("sqlite_master")
134+
internal static let tempMasterName = Table("sqlite_temp_master")
135+
132136
static let typeColumn = Expression<String>("type")
133137
static let nameColumn = Expression<String>("name")
134138
static let tableNameColumn = Expression<String>("tbl_name")
@@ -180,3 +184,13 @@ private class ForeignKeyListTable {
180184
static let onDeleteColumn = Expression<String>("on_delete")
181185
static let matchColumn = Expression<String>("match")
182186
}
187+
188+
private extension Connection {
189+
func schemaTable(temp: Bool = false) -> Table {
190+
if supports(.sqliteSchemaTable) {
191+
return temp ? SchemaTable.tempName : SchemaTable.name
192+
} else {
193+
return temp ? SchemaTable.tempMasterName : SchemaTable.masterName
194+
}
195+
}
196+
}

Tests/SQLiteTests/Schema/Connection+SchemaTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ class ConnectionSchemaTests: SQLiteTestCase {
3333
XCTAssert(results.isEmpty)
3434
}
3535

36-
func test_integrityCheck_table() throws {
36+
func test_partial_integrityCheck_table() throws {
37+
guard db.supports(.partialIntegrityCheck) else { return }
3738
let results = try db.integrityCheck(table: "users")
3839
XCTAssert(results.isEmpty)
3940
}
4041

4142
func test_integrityCheck_table_not_found() throws {
43+
guard db.supports(.partialIntegrityCheck) else { return }
4244
XCTAssertThrowsError(try db.integrityCheck(table: "xxx")) { error in
4345
guard case Result.error(let message, _, _) = error else {
4446
assertionFailure("invalid error type")

0 commit comments

Comments
 (0)