From ba92eca573147ed12d2902bb48940226c0937820 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Wed, 16 Apr 2025 11:11:57 -0700 Subject: [PATCH 1/4] Suppress invalid blank database warnings --- Package.swift | 2 +- .../xcshareddata/swiftpm/Package.resolved | 6 +++--- Sources/SharingGRDB/DefaultDatabase.swift | 4 +++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Package.swift b/Package.swift index 184bc6c2..601e8857 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,7 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/groue/GRDB.swift", from: "7.1.0"), - .package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.6.4"), + .package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.9.0"), .package(url: "https://github.com/pointfreeco/swift-sharing", from: "2.3.0"), ], targets: [ diff --git a/SharingGRDB.xcworkspace/xcshareddata/swiftpm/Package.resolved b/SharingGRDB.xcworkspace/xcshareddata/swiftpm/Package.resolved index a0a493a4..5a387aca 100644 --- a/SharingGRDB.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/SharingGRDB.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "ec46be8deb96ac67ff936228c14004f86c45c5b2212002a00c340bea04ae922e", + "originHash" : "0b0b2ba858f8b04ac444c901bdfa34146f3c9733447c716a345e024788ff20fb", "pins" : [ { "identity" : "combine-schedulers", @@ -69,8 +69,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/pointfreeco/swift-dependencies", "state" : { - "revision" : "52b5e1a09dc016e64ce253e19ab3124b7fae9ac9", - "version" : "1.7.0" + "revision" : "fee6aa29908a75437506ddcbe7434c460605b7e6", + "version" : "1.9.1" } }, { diff --git a/Sources/SharingGRDB/DefaultDatabase.swift b/Sources/SharingGRDB/DefaultDatabase.swift index 1618fea2..d0851a2e 100644 --- a/Sources/SharingGRDB/DefaultDatabase.swift +++ b/Sources/SharingGRDB/DefaultDatabase.swift @@ -81,7 +81,9 @@ extension DependencyValues { """ } } - reportIssue(message) + if shouldReportUnimplemented { + reportIssue(message) + } var configuration = Configuration() #if DEBUG configuration.label = .defaultDatabaseLabel From 04820f78f00cf654551b6620cd211f01b4a75f26 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Wed, 16 Apr 2025 11:25:44 -0700 Subject: [PATCH 2/4] Avoid CI flakiness --- Tests/SharingGRDBTests/IntegrationTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/SharingGRDBTests/IntegrationTests.swift b/Tests/SharingGRDBTests/IntegrationTests.swift index 9159b138..72391829 100644 --- a/Tests/SharingGRDBTests/IntegrationTests.swift +++ b/Tests/SharingGRDBTests/IntegrationTests.swift @@ -5,7 +5,7 @@ import Sharing import SharingGRDB import Testing -@Suite(.dependency(\.defaultDatabase, try .syncUps())) +@MainActor @Suite(.dependency(\.defaultDatabase, try .syncUps())) struct IntegrationTests { @Test func fetchAll_SQLString() async throws { From ae3cdc393d4e53eeb3dbd7a9bc0e786ec8bce7ca Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Wed, 16 Apr 2025 14:39:40 -0700 Subject: [PATCH 3/4] fix --- Tests/SharingGRDBTests/IntegrationTests.swift | 14 +++---- Tests/SharingGRDBTests/SharingGRDBTests.swift | 42 ++++++++++--------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/Tests/SharingGRDBTests/IntegrationTests.swift b/Tests/SharingGRDBTests/IntegrationTests.swift index 72391829..aa6fcf36 100644 --- a/Tests/SharingGRDBTests/IntegrationTests.swift +++ b/Tests/SharingGRDBTests/IntegrationTests.swift @@ -5,7 +5,7 @@ import Sharing import SharingGRDB import Testing -@MainActor @Suite(.dependency(\.defaultDatabase, try .syncUps())) +@Suite(.dependency(\.defaultDatabase, try .syncUps())) struct IntegrationTests { @Test func fetchAll_SQLString() async throws { @@ -18,19 +18,19 @@ struct IntegrationTests { _ = try SyncUp(isActive: true, title: "Engineering") .inserted(db) } - try await Task.sleep(nanoseconds: 10_000_000) + try await Task.sleep(nanoseconds: 100_000_000) #expect(syncUps == [SyncUp(id: 1, isActive: true, title: "Engineering")]) try await database.write { db in _ = try SyncUp(id: 1, isActive: false, title: "Engineering") .saved(db) } - try await Task.sleep(nanoseconds: 10_000_000) + try await Task.sleep(nanoseconds: 100_000_000) #expect(syncUps == []) try await database.write { db in _ = try SyncUp(id: 1, isActive: true, title: "Engineering") .saved(db) } - try await Task.sleep(nanoseconds: 10_000_000) + try await Task.sleep(nanoseconds: 100_000_000) #expect(syncUps == [SyncUp(id: 1, isActive: true, title: "Engineering")]) } @@ -45,19 +45,19 @@ struct IntegrationTests { _ = try SyncUp(isActive: true, title: "Engineering") .inserted(db) } - try await Task.sleep(nanoseconds: 10_000_000) + try await Task.sleep(nanoseconds: 100_000_000) #expect(syncUps == [SyncUp(id: 1, isActive: true, title: "Engineering")]) try await database.write { db in _ = try SyncUp(id: 1, isActive: false, title: "Engineering") .saved(db) } - try await Task.sleep(nanoseconds: 10_000_000) + try await Task.sleep(nanoseconds: 100_000_000) #expect(syncUps == []) try await database.write { db in _ = try SyncUp(id: 1, isActive: true, title: "Engineering") .saved(db) } - try await Task.sleep(nanoseconds: 10_000_000) + try await Task.sleep(nanoseconds: 100_000_000) #expect(syncUps == [SyncUp(id: 1, isActive: true, title: "Engineering")]) } } diff --git a/Tests/SharingGRDBTests/SharingGRDBTests.swift b/Tests/SharingGRDBTests/SharingGRDBTests.swift index 5ee839c7..7ffa9633 100644 --- a/Tests/SharingGRDBTests/SharingGRDBTests.swift +++ b/Tests/SharingGRDBTests/SharingGRDBTests.swift @@ -11,7 +11,7 @@ import Testing $0.defaultDatabase = try DatabaseQueue() } operation: { @SharedReader(.fetchOne(sql: "SELECT 1")) var bool = false - try await Task.sleep(nanoseconds: 10_000_000) + try await Task.sleep(nanoseconds: 100_000_000) #expect(bool) #expect($bool.loadError == nil) } @@ -33,7 +33,7 @@ import Testing } operation: { @SharedReader(.fetchOne(sql: "SELEC 1")) var bool = false #expect(bool == false) - try await Task.sleep(nanoseconds: 10_000_000) + try await Task.sleep(nanoseconds: 100_000_000) #expect($bool.loadError is DatabaseError?) let error = try #require($bool.loadError as? DatabaseError) #expect(error.message == #"near "SELEC": syntax error"#) @@ -41,15 +41,16 @@ import Testing } @Test func fetchWithTwoDatabaseConnections() async throws { + let name = #function try await withDependencies { - $0.defaultDatabase = try .database + $0.defaultDatabase = try .database(named: name) } operation: { @SharedReader(.fetchAll(sql: "SELECT * FROM records")) var records1: [Record] = [] try await Task.sleep(nanoseconds: 100_000_000) #expect(records1.map(\.id) == [1, 2, 3]) try await withDependencies { - $0.defaultDatabase = try .database + $0.defaultDatabase = try .database(named: name) } operation: { @Dependency(\.defaultDatabase) var database2 @SharedReader(.fetchAll(sql: "SELECT * FROM records")) var records2: [Record] = [] @@ -68,14 +69,14 @@ import Testing } } - @Test(.dependency(\.defaultDatabase, try .database)) + @Test(.dependency(\.defaultDatabase, try .database())) func fetchIDHashValue() async throws { let fetchKey1: some SharedReaderKey = .fetch(Fetch1()) let fetchKey2: some SharedReaderKey = .fetch(Fetch2()) #expect(fetchKey1.id.hashValue != fetchKey2.id.hashValue) } - @Test(.dependency(\.defaultDatabase, try .database)) + @Test(.dependency(\.defaultDatabase, try .database())) func fetchAnimationHashValue() async throws { let fetchKey1: some SharedReaderKey = .fetch(Fetch1()) let fetchKey2: some SharedReaderKey = .fetch(Fetch2(), animation: .default) @@ -97,20 +98,23 @@ fileprivate struct Record: Codable, Equatable, FetchableRecord, MutablePersistab let id: Int } extension DatabaseWriter where Self == DatabaseQueue { - fileprivate static var database: DatabaseQueue { - get throws { - let database = try DatabaseQueue(named: "db") - var migrator = DatabaseMigrator() - migrator.registerMigration("Up") { db in - try db.create(table: "records") { table in - table.column("id", .integer).primaryKey(autoincrement: true) - } - for index in 1...3 { - _ = try Record(id: index).inserted(db) - } + fileprivate static func database(named name: String? = nil) throws -> DatabaseQueue { + let database: DatabaseQueue + if let name { + database = try DatabaseQueue(named: name) + } else { + database = try DatabaseQueue() + } + var migrator = DatabaseMigrator() + migrator.registerMigration("Up") { db in + try db.create(table: "records") { table in + table.column("id", .integer).primaryKey(autoincrement: true) + } + for index in 1...3 { + _ = try Record(id: index).inserted(db) } - try migrator.migrate(database) - return database } + try migrator.migrate(database) + return database } } From 5e3e62cfe67ba362a2ed720a3595a5885335b3ed Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Wed, 16 Apr 2025 14:45:19 -0700 Subject: [PATCH 4/4] update messaging --- Sources/SharingGRDB/DefaultDatabase.swift | 41 ++++++++++++++--------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/Sources/SharingGRDB/DefaultDatabase.swift b/Sources/SharingGRDB/DefaultDatabase.swift index d0851a2e..8730c5e8 100644 --- a/Sources/SharingGRDB/DefaultDatabase.swift +++ b/Sources/SharingGRDB/DefaultDatabase.swift @@ -47,21 +47,8 @@ extension DependencyValues { static var testValue: any DatabaseWriter { var message: String { @Dependency(\.context) var context - if context == .preview { - return """ - A blank, in-memory database is being used. To set the database that is used by \ - 'SharingGRDB', use the 'prepareDependencies' tool as early as possible in the lifetime \ - of your preview: - - #Preview { - let _ = prepareDependencies { - $0.defaultDatabase = try! DatabaseQueue(/* ... */) - } - - // ... - } - """ - } else { + switch context { + case .live: return """ A blank, in-memory database is being used. To set the database that is used by \ 'SharingGRDB', use the 'prepareDependencies' tool as early as possible in the lifetime \ @@ -79,6 +66,30 @@ extension DependencyValues { // ... } """ + + case .preview: + return #""" + A blank, in-memory database is being used. To set the database that is used by \ + 'SharingGRDB' in a preview, use a tool like the 'dependency' trait: + + #Preview( + trait: .dependency(\.defaultDatabase, try DatabaseQueue(/* ... */) + ) { + // ... + } + """# + + case .test: + return #""" + A blank, in-memory database is being used. To set the database that is used by \ + 'SharingGRDB' in a test, use a tool like the 'dependency' trait: + + @Suite(.dependency(\.defaultDatabase, try DatabaseQueue(/* ... */) + struct MyTests { + // ... + } + """# + } } if shouldReportUnimplemented {