Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Database ID as Identifier for In-Memory Database Connection #79

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Sources/SQLiteKit/SQLiteConnectionSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import Logging
public struct SQLiteConnectionSource: ConnectionPoolSource {
private let configuration: SQLiteConfiguration
private let threadPool: NIOThreadPool
private let database: String?

private var connectionStorage: SQLiteConnection.Storage {
switch self.configuration.storage {
case .memory:
let identifier = self.database ?? String(describing: ObjectIdentifier(threadPool).unique)
return .file(
path: "file:\(ObjectIdentifier(threadPool).unique)?mode=memory&cache=shared"
path: "file:\(identifier)?mode=memory&cache=shared"
)
case .file(let path):
return .file(path: path)
Expand All @@ -19,10 +21,12 @@ public struct SQLiteConnectionSource: ConnectionPoolSource {

public init(
configuration: SQLiteConfiguration,
threadPool: NIOThreadPool
threadPool: NIOThreadPool,
database: String?
) {
self.configuration = configuration
self.threadPool = threadPool
self.database = database
}

public func makeConnection(
Expand Down
3 changes: 2 additions & 1 deletion Tests/SQLiteKitTests/SQLKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ class SQLiteTests: XCTestCase {
self.threadPool.start()
self.connection = try! SQLiteConnectionSource(
configuration: .init(storage: .memory, enableForeignKeys: true),
threadPool: self.threadPool
threadPool: self.threadPool,
database: "memory"
).makeConnection(logger: .init(label: "test"), on: self.eventLoopGroup.next()).wait()
}

Expand Down