Skip to content

Commit db094c5

Browse files
committed
Make sure db is configured for the correct host
1 parent a4f69b6 commit db094c5

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

Sources/App/configure.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Vapor
1919

2020

2121
@discardableResult
22-
public func configure(_ app: Application, databasePort: Int? = nil) async throws -> String {
22+
public func configure(_ app: Application, databaseHost: String? = nil, databasePort: Int? = nil) async throws -> String {
2323
#if DEBUG && os(macOS)
2424
// The bundle is only loaded if /Applications/InjectionIII.app exists on the local development machine.
2525
// Requires InjectionIII 4.7.3 or higher to be loaded for compatibility with Package.swift files.
@@ -52,7 +52,7 @@ public func configure(_ app: Application, databasePort: Int? = nil) async throws
5252

5353
// Setup database connection
5454
guard
55-
let host = Environment.get("DATABASE_HOST"),
55+
let host = databaseHost ?? Environment.get("DATABASE_HOST"),
5656
let port = databasePort ?? Environment.get("DATABASE_PORT").flatMap(Int.init),
5757
let username = Environment.get("DATABASE_USERNAME"),
5858
let password = Environment.get("DATABASE_PASSWORD"),

Tests/AppTests/Helpers/DatabasePool.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ actor DatabasePool {
3030
self.connectionDetails = .init(index: index)
3131
}
3232

33+
var host: String { connectionDetails.host }
3334
var port: Int { connectionDetails.port }
3435
}
3536

@@ -125,7 +126,7 @@ actor DatabasePool {
125126
// We don't have docker available in CI to probe for running dbs.
126127
// Instead, we have a hard-coded list of dbs we launch in the GH workflow
127128
// file and correspondingly, we hard-code their ports here.
128-
return (0..<8).map(Database.init(index:))
129+
return (0..<Environment.databasePoolSize).map(Database.init(index:))
129130
} else {
130131
let stdout = try await ShellOut.shellOut(to: .getContainerNames).stdout
131132
return stdout
@@ -228,7 +229,7 @@ extension DatabasePool.Database {
228229
do { // Use autoMigrate to spin up the schema
229230
let app = try await Application.make(environment)
230231
app.logger = .init(label: "noop") { _ in SwiftLogNoOpLogHandler() }
231-
try await configure(app, databasePort: connectionDetails.port)
232+
try await configure(app, databaseHost: connectionDetails.host, databasePort: connectionDetails.port)
232233
try await app.autoMigrate()
233234
try await app.asyncShutdown()
234235
}
@@ -282,7 +283,6 @@ private func connect(to databaseName: String, details: DatabasePool.Database.Con
282283
database: databaseName,
283284
tls: .disable
284285
)
285-
286286
return .init(configuration: config)
287287
}
288288

@@ -293,12 +293,11 @@ private func _withDatabase(_ databaseName: String,
293293
_ query: @Sendable @escaping (PostgresClient) async throws -> Void) async throws {
294294
let client = connect(to: databaseName, details: details)
295295
try await run(timeout: timeout) {
296-
#warning("This looks weird, review it")
297296
try await withThrowingTaskGroup { taskGroup in
298297
taskGroup.addTask { await client.run() }
299-
taskGroup.addTask {
300-
try await query(client)
301-
}
298+
299+
taskGroup.addTask { try await query(client) }
300+
302301
try await taskGroup.next()
303302
taskGroup.cancelAll()
304303
}

Tests/AppTests/Helpers/TestSupport.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func withApp(
3232
try await DatabasePool.shared.withDatabase { database in
3333
try await database.restoreSnapshot(details: database.connectionDetails)
3434
let app = try await Application.make(environment)
35-
try await configure(app, databasePort: database.port)
35+
try await configure(app, databaseHost: database.host, databasePort: database.port)
3636

3737
return try await run {
3838
try await setup(app)

0 commit comments

Comments
 (0)