From 22e8dfb45da89aba69c2c78813d9d521bfad4e15 Mon Sep 17 00:00:00 2001 From: Scott Marchant Date: Fri, 16 Jan 2026 12:59:33 -0700 Subject: [PATCH 1/2] refactor: Resolve new warnings triggered by using .wait in an asynchronous context. The warnings are new as of later versions of swift-nio released in December 2025. --- Tests/SQLiteKitTests/SQLiteKitTests.swift | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Tests/SQLiteKitTests/SQLiteKitTests.swift b/Tests/SQLiteKitTests/SQLiteKitTests.swift index 599a9f0..32d0535 100644 --- a/Tests/SQLiteKitTests/SQLiteKitTests.swift +++ b/Tests/SQLiteKitTests/SQLiteKitTests.swift @@ -105,10 +105,11 @@ final class SQLiteKitTests: XCTestCase { ) let conn2 = try await source.makeConnection(logger: self.connection.logger, on: MultiThreadedEventLoopGroup.singleton.any()).get() - defer { try! conn2.close().wait() } let res2 = try await conn2.query("PRAGMA foreign_keys").get() XCTAssertEqual(res2[0].column("foreign_keys"), .integer(0)) + + try! await conn2.close().get() } func testJSONStringColumn() async throws { @@ -134,18 +135,19 @@ final class SQLiteKitTests: XCTestCase { ) let a1 = try await a.makeConnection(logger: .init(label: "test"), on: MultiThreadedEventLoopGroup.singleton.any()).get() - defer { try! a1.close().wait() } let a2 = try await a.makeConnection(logger: .init(label: "test"), on: MultiThreadedEventLoopGroup.singleton.any()).get() - defer { try! a2.close().wait() } let b1 = try await b.makeConnection(logger: .init(label: "test"), on: MultiThreadedEventLoopGroup.singleton.any()).get() - defer { try! b1.close().wait() } let b2 = try await b.makeConnection(logger: .init(label: "test"), on: MultiThreadedEventLoopGroup.singleton.any()).get() - defer { try! b2.close().wait() } _ = try await a1.query("CREATE TABLE foo (bar INTEGER)").get() _ = try await a2.query("SELECT * FROM foo").get() _ = try await b1.query("CREATE TABLE foo (bar INTEGER)").get() _ = try await b2.query("SELECT * FROM foo").get() + + try! await b2.close().get() + try! await b1.close().get() + try! await a2.close().get() + try! await a1.close().get() } // https://github.com/vapor/sqlite-kit/issues/56 From a4fb999d322b73d01c254b848d3454b5de7252a5 Mon Sep 17 00:00:00 2001 From: Scott Marchant Date: Mon, 30 Jun 2025 16:27:46 -0600 Subject: [PATCH 2/2] feat: Add support for wasm when using in-memory-only sqlite database --- .github/workflows/test.yml | 2 ++ Package@swift-5.9.swift | 16 ++++++++++++---- Sources/SQLiteKit/SQLiteConnectionSource.swift | 9 +++++++++ Tests/SQLiteKitTests/SQLiteKitTests.swift | 6 ++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 23cb57e..9f31477 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -16,6 +16,8 @@ jobs: unit-tests: uses: vapor/ci/.github/workflows/run-unit-tests.yml@main secrets: inherit + with: + with_wasm: true # Make sure downstream dependents still work dependents-check: diff --git a/Package@swift-5.9.swift b/Package@swift-5.9.swift index ae4fbb4..1f90449 100644 --- a/Package@swift-5.9.swift +++ b/Package@swift-5.9.swift @@ -13,16 +13,24 @@ let package = Package( .library(name: "SQLiteKit", targets: ["SQLiteKit"]), ], dependencies: [ - .package(url: "https://github.com/apple/swift-nio.git", from: "2.65.0"), - .package(url: "https://github.com/vapor/sqlite-nio.git", from: "1.9.0"), - .package(url: "https://github.com/vapor/sql-kit.git", from: "3.29.3"), - .package(url: "https://github.com/vapor/async-kit.git", from: "1.19.0"), + // TODO: SM: Update swift-nio version once NIOAsyncRuntime is available from swift-nio + // .package(url: "https://github.com/apple/swift-nio.git", from: "2.89.0"), + .package(url: "https://github.com/PassiveLogic/swift-nio.git", branch: "feat/addNIOAsyncRuntimeForWasm"), + + // TODO: SM: Update below once everything is merged and release to the proper repositories +// .package(url: "https://github.com/vapor/sqlite-nio.git", from: "1.9.0"), + .package(url: "https://github.com/PassiveLogic/sqlite-nio.git", branch: "feat/swift-wasm-support-v2"), + .package(url: "https://github.com/vapor/sql-kit.git", from: "3.33.1"), +// .package(url: "https://github.com/vapor/async-kit.git", from: "1.19.0"), + .package(url: "https://github.com/PassiveLogic/async-kit.git", branch: "feat/swift-wasm-support-v2"), ], targets: [ .target( name: "SQLiteKit", dependencies: [ .product(name: "NIOFoundationCompat", package: "swift-nio"), + .product(name: "NIOAsyncRuntime", package: "swift-nio", condition: .when(platforms: [.wasi])), + .product(name: "NIOPosix", package: "swift-nio"), .product(name: "AsyncKit", package: "async-kit"), .product(name: "SQLiteNIO", package: "sqlite-nio"), .product(name: "SQLKit", package: "sql-kit"), diff --git a/Sources/SQLiteKit/SQLiteConnectionSource.swift b/Sources/SQLiteKit/SQLiteConnectionSource.swift index e327a50..d5c9475 100644 --- a/Sources/SQLiteKit/SQLiteConnectionSource.swift +++ b/Sources/SQLiteKit/SQLiteConnectionSource.swift @@ -5,6 +5,9 @@ import Foundation #endif import Logging import AsyncKit +#if canImport(NIOAsyncRuntime) +import NIOAsyncRuntime +#endif import NIOPosix import SQLiteNIO import NIOCore @@ -16,7 +19,13 @@ public struct SQLiteConnectionSource: ConnectionPoolSource, Sendable { private let threadPool: NIOThreadPool private var connectionStorage: SQLiteConnection.Storage { + #if os(WASI) + // NOTE: For WASI platforms, file urls and paths cause runtime errors currently. Using + // in-memory connection only as a workaround. + .memory + #else .file(path: self.actualURL.absoluteString) + #endif } /// Create a new ``SQLiteConnectionSource``. diff --git a/Tests/SQLiteKitTests/SQLiteKitTests.swift b/Tests/SQLiteKitTests/SQLiteKitTests.swift index 32d0535..76b1acd 100644 --- a/Tests/SQLiteKitTests/SQLiteKitTests.swift +++ b/Tests/SQLiteKitTests/SQLiteKitTests.swift @@ -124,6 +124,11 @@ final class SQLiteKitTests: XCTestCase { XCTAssertEqual(bar.baz, "qux") } + // NOTE: The following test doesn't work in a runtime environment + // due to reliance on temp files. The test is elided for now + // for WASI targets, but could be used in the future once + // a persistence solution is working for WASI platforms. + #if !os(WASI) func testMultipleInMemoryDatabases() async throws { let a = SQLiteConnectionSource( configuration: .init(storage: .memory, enableForeignKeys: true), @@ -149,6 +154,7 @@ final class SQLiteKitTests: XCTestCase { try! await a2.close().get() try! await a1.close().get() } + #endif // !os(WASI) // https://github.com/vapor/sqlite-kit/issues/56 func testDoubleConstraintError() async throws {