Migrated the Service method in the Persistence package to use PostgreSQL instead.

This commit is contained in:
2026-08-04 17:44:02 +02:00
parent d7d2709f88
commit 1fe7ef2049
2 changed files with 30 additions and 29 deletions
@@ -1,4 +1,4 @@
import FluentMySQLDriver import FluentPostgresDriver
import FluentSQLiteDriver import FluentSQLiteDriver
import HummingbirdFluent import HummingbirdFluent
import Logging import Logging
@@ -37,27 +37,28 @@ public struct Service: Sendable {
/// The selected backend is registered as the *default* database, so repositories resolve it with a plain `fluent.db()` and stay agnostic of which /// The selected backend is registered as the *default* database, so repositories resolve it with a plain `fluent.db()` and stay agnostic of which
/// driver is in use. The returned service is not yet running; add it to the application's service group (`app.addServices(_:)`) so it starts and shuts /// driver is in use. The returned service is not yet running; add it to the application's service group (`app.addServices(_:)`) so it starts and shuts
/// its connection pool down alongside the server. /// its connection pool down alongside the server.
/// - Throws: an error when the TLS context for the PostgreSQL backend cannot be built.
/// - Returns: the configured `Fluent` service, ready to be added to the service group. /// - Returns: the configured `Fluent` service, ready to be added to the service group.
public func callAsFunction() -> Fluent { public func callAsFunction() throws -> Fluent {
let fluent = Fluent( let fluent = Fluent(
logger: logger logger: logger
) )
switch driver { switch driver {
case .mysql(let configuration): case .postgres(let configuration):
fluent.databases.use( fluent.databases.use(
.mysql( .postgres(
configuration: .init( configuration: .init(
hostname: configuration.host, hostname: configuration.host,
port: configuration.port, port: configuration.port,
username: configuration.username, username: configuration.username,
password: configuration.password, password: configuration.password,
database: configuration.name, database: configuration.name,
tlsConfiguration: configuration.tls.tlsConfiguration tls: try configuration.tls.postgresTLS()
), ),
maxConnectionsPerEventLoop: configuration.maxConnectionsPerEventLoop maxConnectionsPerEventLoop: configuration.maxConnectionsPerEventLoop
), ),
as: .mysql, as: .psql,
isDefault: true isDefault: true
) )
case .inMemory: case .inMemory:
@@ -20,7 +20,7 @@ struct ServiceTests {
logger: Logger(label: "test") logger: Logger(label: "test")
) )
let fluent = service() let fluent = try service()
let database = fluent.db() as? any SQLDatabase let database = fluent.db() as? any SQLDatabase
try await fluent.shutdown() try await fluent.shutdown()
@@ -31,14 +31,14 @@ struct ServiceTests {
} }
@Test @Test
func `registers a MySQL database as the default for the mysql driver`() async throws { func `registers a PostgreSQL database as the default for the postgres driver`() async throws {
// Resolving the default database opens no connection pooling is lazy so no server // Resolving the default database opens no connection pooling is lazy so no server
// needs to be listening on the configured host and port. // needs to be listening on the configured host and port.
let service = Service( let service = Service(
driver: .mysql( driver: .postgres(
.init( .init(
host: "127.0.0.1", host: "127.0.0.1",
port: 3306, port: 5432,
name: "loud", name: "loud",
username: "loud", username: "loud",
password: "loud", password: "loud",
@@ -49,14 +49,14 @@ struct ServiceTests {
logger: Logger(label: "test") logger: Logger(label: "test")
) )
let fluent = service() let fluent = try service()
let database = fluent.db() as? any SQLDatabase let database = fluent.db() as? any SQLDatabase
try await fluent.shutdown() try await fluent.shutdown()
let dialect = try #require(database?.dialect) let dialect = try #require(database?.dialect)
#expect(dialect.name == "mysql") #expect(dialect.name == "postgresql")
} }
@Test @Test
@@ -66,7 +66,7 @@ struct ServiceTests {
logger: Logger(label: "test") logger: Logger(label: "test")
) )
let fluent = service() let fluent = try service()
do { do {
let database = try #require(fluent.db() as? any SQLDatabase) let database = try #require(fluent.db() as? any SQLDatabase)
@@ -87,12 +87,12 @@ struct ServiceTests {
} }
@Test( @Test(
"mysql: migrate, insert, read back", "postgres: migrate, insert, read back",
.enabled(if: mysqlDriver != nil) .enabled(if: postgresDriver != nil)
) )
func mysqlRoundTrip() async throws { func postgresRoundTrip() async throws {
try await roundTrip( try await roundTrip(
driver: mysqlDriver!, driver: postgresDriver!,
revertAfter: true revertAfter: true
) )
} }
@@ -121,7 +121,7 @@ private extension ServiceTests {
logger: Logger(label: "test") logger: Logger(label: "test")
) )
let fluent = service() let fluent = try service()
do { do {
await prepareDB(for: fluent) await prepareDB(for: fluent)
@@ -147,23 +147,23 @@ private extension ServiceTests {
} }
/// The MySQL/MariaDB driver built from the `MYSQL_TEST_*` environment variables, or `nil` when the gate /// The PostgreSQL driver built from the `POSTGRES_TEST_*` environment variables, or `nil` when the gate
/// variable `MYSQL_TEST_HOST` is unset in which case the MySQL integration test is skipped, so the suite /// variable `POSTGRES_TEST_HOST` is unset in which case the PostgreSQL integration test is skipped, so
/// stays runnable with no database available. /// the suite stays runnable with no database available.
private let mysqlDriver: Persistence.Driver? = { private let postgresDriver: Persistence.Driver? = {
let environment = ProcessInfo.processInfo.environment let environment = ProcessInfo.processInfo.environment
guard let host = environment["MYSQL_TEST_HOST"] else { guard let host = environment["POSTGRES_TEST_HOST"] else {
return nil return nil
} }
return .mysql( return .postgres(
.init( .init(
host: host, host: host,
port: environment["MYSQL_TEST_PORT"].flatMap(Int.init) ?? 3306, port: environment["POSTGRES_TEST_PORT"].flatMap(Int.init) ?? 5432,
name: environment["MYSQL_TEST_NAME"] ?? "loud", name: environment["POSTGRES_TEST_NAME"] ?? "loud",
username: environment["MYSQL_TEST_USERNAME"] ?? "loud", username: environment["POSTGRES_TEST_USERNAME"] ?? "loud",
password: environment["MYSQL_TEST_PASSWORD"] ?? "loud", password: environment["POSTGRES_TEST_PASSWORD"] ?? "loud",
tls: .off, tls: .off,
maxConnectionsPerEventLoop: 2 maxConnectionsPerEventLoop: 2
) )