Migrated the Service method in the Persistence package to use PostgreSQL instead.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import FluentMySQLDriver
|
||||
import FluentPostgresDriver
|
||||
import FluentSQLiteDriver
|
||||
import HummingbirdFluent
|
||||
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
|
||||
/// 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.
|
||||
/// - 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.
|
||||
public func callAsFunction() -> Fluent {
|
||||
public func callAsFunction() throws -> Fluent {
|
||||
let fluent = Fluent(
|
||||
logger: logger
|
||||
)
|
||||
|
||||
switch driver {
|
||||
case .mysql(let configuration):
|
||||
case .postgres(let configuration):
|
||||
fluent.databases.use(
|
||||
.mysql(
|
||||
.postgres(
|
||||
configuration: .init(
|
||||
hostname: configuration.host,
|
||||
port: configuration.port,
|
||||
username: configuration.username,
|
||||
password: configuration.password,
|
||||
database: configuration.name,
|
||||
tlsConfiguration: configuration.tls.tlsConfiguration
|
||||
tls: try configuration.tls.postgresTLS()
|
||||
),
|
||||
maxConnectionsPerEventLoop: configuration.maxConnectionsPerEventLoop
|
||||
),
|
||||
as: .mysql,
|
||||
as: .psql,
|
||||
isDefault: true
|
||||
)
|
||||
case .inMemory:
|
||||
|
||||
@@ -20,7 +20,7 @@ struct ServiceTests {
|
||||
logger: Logger(label: "test")
|
||||
)
|
||||
|
||||
let fluent = service()
|
||||
let fluent = try service()
|
||||
let database = fluent.db() as? any SQLDatabase
|
||||
|
||||
try await fluent.shutdown()
|
||||
@@ -31,14 +31,14 @@ struct ServiceTests {
|
||||
}
|
||||
|
||||
@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
|
||||
// needs to be listening on the configured host and port.
|
||||
let service = Service(
|
||||
driver: .mysql(
|
||||
driver: .postgres(
|
||||
.init(
|
||||
host: "127.0.0.1",
|
||||
port: 3306,
|
||||
port: 5432,
|
||||
name: "loud",
|
||||
username: "loud",
|
||||
password: "loud",
|
||||
@@ -49,14 +49,14 @@ struct ServiceTests {
|
||||
logger: Logger(label: "test")
|
||||
)
|
||||
|
||||
let fluent = service()
|
||||
let fluent = try service()
|
||||
let database = fluent.db() as? any SQLDatabase
|
||||
|
||||
try await fluent.shutdown()
|
||||
|
||||
let dialect = try #require(database?.dialect)
|
||||
|
||||
#expect(dialect.name == "mysql")
|
||||
#expect(dialect.name == "postgresql")
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -66,7 +66,7 @@ struct ServiceTests {
|
||||
logger: Logger(label: "test")
|
||||
)
|
||||
|
||||
let fluent = service()
|
||||
let fluent = try service()
|
||||
|
||||
do {
|
||||
let database = try #require(fluent.db() as? any SQLDatabase)
|
||||
@@ -87,12 +87,12 @@ struct ServiceTests {
|
||||
}
|
||||
|
||||
@Test(
|
||||
"mysql: migrate, insert, read back",
|
||||
.enabled(if: mysqlDriver != nil)
|
||||
"postgres: migrate, insert, read back",
|
||||
.enabled(if: postgresDriver != nil)
|
||||
)
|
||||
func mysqlRoundTrip() async throws {
|
||||
func postgresRoundTrip() async throws {
|
||||
try await roundTrip(
|
||||
driver: mysqlDriver!,
|
||||
driver: postgresDriver!,
|
||||
revertAfter: true
|
||||
)
|
||||
}
|
||||
@@ -120,8 +120,8 @@ private extension ServiceTests {
|
||||
driver: driver,
|
||||
logger: Logger(label: "test")
|
||||
)
|
||||
|
||||
let fluent = service()
|
||||
|
||||
let fluent = try service()
|
||||
|
||||
do {
|
||||
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
|
||||
/// variable `MYSQL_TEST_HOST` is unset — in which case the MySQL integration test is skipped, so the suite
|
||||
/// stays runnable with no database available.
|
||||
private let mysqlDriver: Persistence.Driver? = {
|
||||
/// The PostgreSQL driver built from the `POSTGRES_TEST_*` environment variables, or `nil` when the gate
|
||||
/// variable `POSTGRES_TEST_HOST` is unset — in which case the PostgreSQL integration test is skipped, so
|
||||
/// the suite stays runnable with no database available.
|
||||
private let postgresDriver: Persistence.Driver? = {
|
||||
let environment = ProcessInfo.processInfo.environment
|
||||
|
||||
guard let host = environment["MYSQL_TEST_HOST"] else {
|
||||
guard let host = environment["POSTGRES_TEST_HOST"] else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return .mysql(
|
||||
return .postgres(
|
||||
.init(
|
||||
host: host,
|
||||
port: environment["MYSQL_TEST_PORT"].flatMap(Int.init) ?? 3306,
|
||||
name: environment["MYSQL_TEST_NAME"] ?? "loud",
|
||||
username: environment["MYSQL_TEST_USERNAME"] ?? "loud",
|
||||
password: environment["MYSQL_TEST_PASSWORD"] ?? "loud",
|
||||
port: environment["POSTGRES_TEST_PORT"].flatMap(Int.init) ?? 5432,
|
||||
name: environment["POSTGRES_TEST_NAME"] ?? "loud",
|
||||
username: environment["POSTGRES_TEST_USERNAME"] ?? "loud",
|
||||
password: environment["POSTGRES_TEST_PASSWORD"] ?? "loud",
|
||||
tls: .off,
|
||||
maxConnectionsPerEventLoop: 2
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user