From 94e08164139226f168e319298a2a2964e7d31a55 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Sun, 15 Sep 2024 11:16:19 +0100 Subject: [PATCH 1/4] Split migrations into its own library PostgresMigrations This is in preparation for splitting JobsPostgres into its own repo. Renamed PostgresMigration symbols to DatabaseMigration Created separate test targets for each library --- Package.swift | 27 ++++++++++++++-- Package@swift-6.0.swift | 22 ++++++++++++- Snippets/PSQLSoakTestQueue.swift | 4 +-- .../CreatePersistTable.swift | 7 ++-- .../PostgresPersistDriver.swift | 7 ++-- .../Migrations/CreateJobDelay.swift | 6 ++-- .../Migrations/CreateJobQueue.swift | 6 ++-- .../Migrations/CreateJobQueueMetadata.swift | 6 ++-- .../JobsPostgres/Migrations/CreateJobs.swift | 8 ++--- Sources/JobsPostgres/PostgresJobsQueue.swift | 8 ++--- .../Migration.swift | 20 ++++++------ .../MigrationError.swift | 6 ++-- .../Migrations.swift | 32 +++++++++---------- .../PersistTests.swift | 15 ++++++++- .../HummingbirdPostgresTests/TestUtils.swift | 15 --------- .../JobsTests.swift | 21 +++++++++--- .../MigrationTests.swift | 32 +++++++++++++------ 17 files changed, 153 insertions(+), 89 deletions(-) rename Sources/{HummingbirdPostgres => PostgresMigrations}/Migration.swift (78%) rename Sources/{HummingbirdPostgres => PostgresMigrations}/MigrationError.swift (87%) rename Sources/{HummingbirdPostgres => PostgresMigrations}/Migrations.swift (93%) delete mode 100644 Tests/HummingbirdPostgresTests/TestUtils.swift rename Tests/{HummingbirdPostgresTests => JobsPostgresTests}/JobsTests.swift (96%) rename Tests/{HummingbirdPostgresTests => PostgresMigrationsTests}/MigrationTests.swift (94%) diff --git a/Package.swift b/Package.swift index 1da89dc..e27fce6 100644 --- a/Package.swift +++ b/Package.swift @@ -10,6 +10,7 @@ let package = Package( platforms: [.macOS(.v14), .iOS(.v17), .tvOS(.v17)], products: [ .library(name: "HummingbirdPostgres", targets: ["HummingbirdPostgres"]), + .library(name: "PostgresMigrations", targets: ["PostgresMigrations"]), .library(name: "JobsPostgres", targets: ["JobsPostgres"]), ], dependencies: [ @@ -21,15 +22,23 @@ let package = Package( .target( name: "HummingbirdPostgres", dependencies: [ + "PostgresMigrations", .product(name: "Hummingbird", package: "hummingbird"), .product(name: "PostgresNIO", package: "postgres-nio"), ], swiftSettings: swiftSettings ), + .target( + name: "PostgresMigrations", + dependencies: [ + .product(name: "PostgresNIO", package: "postgres-nio"), + ], + swiftSettings: swiftSettings + ), .target( name: "JobsPostgres", dependencies: [ - "HummingbirdPostgres", + "PostgresMigrations", .product(name: "Jobs", package: "swift-jobs"), .product(name: "PostgresNIO", package: "postgres-nio"), ], @@ -39,10 +48,22 @@ let package = Package( name: "HummingbirdPostgresTests", dependencies: [ "HummingbirdPostgres", + .product(name: "HummingbirdTesting", package: "hummingbird"), + ] + ), + .testTarget( + name: "PostgresMigrationsTests", + dependencies: [ + "PostgresMigrations", + .product(name: "HummingbirdTesting", package: "hummingbird"), + ] + ), + .testTarget( + name: "JobsPostgresTests", + dependencies: [ "JobsPostgres", .product(name: "HummingbirdTesting", package: "hummingbird"), - ], - swiftSettings: swiftSettings + ] ), ] ) diff --git a/Package@swift-6.0.swift b/Package@swift-6.0.swift index fe7a7de..e42a37d 100644 --- a/Package@swift-6.0.swift +++ b/Package@swift-6.0.swift @@ -19,14 +19,21 @@ let package = Package( .target( name: "HummingbirdPostgres", dependencies: [ + "PostgresMigrations", .product(name: "Hummingbird", package: "hummingbird"), .product(name: "PostgresNIO", package: "postgres-nio"), ] ), + .target( + name: "PostgresMigrations", + dependencies: [ + .product(name: "PostgresNIO", package: "postgres-nio"), + ] + ), .target( name: "JobsPostgres", dependencies: [ - "HummingbirdPostgres", + "PostgresMigrations", .product(name: "Jobs", package: "swift-jobs"), .product(name: "PostgresNIO", package: "postgres-nio"), ] @@ -35,6 +42,19 @@ let package = Package( name: "HummingbirdPostgresTests", dependencies: [ "HummingbirdPostgres", + .product(name: "HummingbirdTesting", package: "hummingbird"), + ] + ), + .testTarget( + name: "PostgresMigrationsTests", + dependencies: [ + "PostgresMigrations", + .product(name: "HummingbirdTesting", package: "hummingbird"), + ] + ), + .testTarget( + name: "JobsPostgresTests", + dependencies: [ "JobsPostgres", .product(name: "HummingbirdTesting", package: "hummingbird"), ] diff --git a/Snippets/PSQLSoakTestQueue.swift b/Snippets/PSQLSoakTestQueue.swift index a41ffda..fee119a 100644 --- a/Snippets/PSQLSoakTestQueue.swift +++ b/Snippets/PSQLSoakTestQueue.swift @@ -1,9 +1,9 @@ -import HummingbirdPostgres import Jobs import JobsPostgres import Logging import NIOCore import NIOPosix +import PostgresMigrations import PostgresNIO import ServiceLifecycle @@ -13,7 +13,7 @@ let postgresClient = PostgresClient( configuration: .init(host: "localhost", port: 5432, username: "test_user", password: "test_password", database: "test_db", tls: .disable), backgroundLogger: logger ) -let postgresMigrations = PostgresMigrations() +let postgresMigrations = DatabaseMigrations() let jobQueue = await JobQueue( .postgres( client: postgresClient, diff --git a/Sources/HummingbirdPostgres/CreatePersistTable.swift b/Sources/HummingbirdPostgres/CreatePersistTable.swift index fe52792..e6d9753 100644 --- a/Sources/HummingbirdPostgres/CreatePersistTable.swift +++ b/Sources/HummingbirdPostgres/CreatePersistTable.swift @@ -13,9 +13,10 @@ //===----------------------------------------------------------------------===// import Logging +import PostgresMigrations import PostgresNIO -struct CreatePersistTable: PostgresMigration { +struct CreatePersistTable: DatabaseMigration { func apply(connection: PostgresConnection, logger: Logger) async throws { try await connection.query( """ @@ -37,10 +38,10 @@ struct CreatePersistTable: PostgresMigration { } var name: String { "_Create_Persist_Table_" } - var group: PostgresMigrationGroup { .persist } + var group: DatabaseMigrationGroup { .persist } } -extension PostgresMigrationGroup { +extension DatabaseMigrationGroup { /// Persist driver migration group public static var persist: Self { .init("_hb_pg_persist") } } diff --git a/Sources/HummingbirdPostgres/PostgresPersistDriver.swift b/Sources/HummingbirdPostgres/PostgresPersistDriver.swift index 60b493f..ed70fb2 100644 --- a/Sources/HummingbirdPostgres/PostgresPersistDriver.swift +++ b/Sources/HummingbirdPostgres/PostgresPersistDriver.swift @@ -16,6 +16,7 @@ import AsyncAlgorithms import Foundation import Hummingbird import NIOCore +import PostgresMigrations import PostgresNIO extension PSQLError { @@ -62,15 +63,15 @@ public final class PostgresPersistDriver: PersistDriver { let client: PostgresClient let logger: Logger let tidyUpFrequency: Duration - let migrations: PostgresMigrations + let migrations: DatabaseMigrations /// Initialize PostgresPersistDriver /// - Parameters: /// - client: Postgres client - /// - migrations: Migrations array to add persist migrations + /// - migrations: DatabaseMigrations array to add persist migrations /// - tidyUpFrequency: How frequently cleanup expired database entries should occur /// - logger: Logger used by persist - public init(client: PostgresClient, migrations: PostgresMigrations, tidyUpFrequency: Duration = .seconds(600), logger: Logger) async { + public init(client: PostgresClient, migrations: DatabaseMigrations, tidyUpFrequency: Duration = .seconds(600), logger: Logger) async { self.client = client self.logger = logger self.tidyUpFrequency = tidyUpFrequency diff --git a/Sources/JobsPostgres/Migrations/CreateJobDelay.swift b/Sources/JobsPostgres/Migrations/CreateJobDelay.swift index cda7cfb..aff928c 100644 --- a/Sources/JobsPostgres/Migrations/CreateJobDelay.swift +++ b/Sources/JobsPostgres/Migrations/CreateJobDelay.swift @@ -12,11 +12,11 @@ // //===----------------------------------------------------------------------===// -import HummingbirdPostgres import Logging +import PostgresMigrations import PostgresNIO -struct CreateJobDelay: PostgresMigration { +struct CreateJobDelay: DatabaseMigration { func apply(connection: PostgresConnection, logger: Logger) async throws { try await connection.query( """ @@ -34,5 +34,5 @@ struct CreateJobDelay: PostgresMigration { } var name: String { "_Create_JobQueueDelay_Table_" } - var group: PostgresMigrationGroup { .jobQueue } + var group: DatabaseMigrationGroup { .jobQueue } } diff --git a/Sources/JobsPostgres/Migrations/CreateJobQueue.swift b/Sources/JobsPostgres/Migrations/CreateJobQueue.swift index 83cc077..c64f958 100644 --- a/Sources/JobsPostgres/Migrations/CreateJobQueue.swift +++ b/Sources/JobsPostgres/Migrations/CreateJobQueue.swift @@ -12,11 +12,11 @@ // //===----------------------------------------------------------------------===// -import HummingbirdPostgres import Logging +import PostgresMigrations import PostgresNIO -struct CreateJobQueue: PostgresMigration { +struct CreateJobQueue: DatabaseMigration { func apply(connection: PostgresConnection, logger: Logger) async throws { try await connection.query( """ @@ -44,5 +44,5 @@ struct CreateJobQueue: PostgresMigration { } var name: String { "_Create_JobQueue_Table_" } - var group: PostgresMigrationGroup { .jobQueue } + var group: DatabaseMigrationGroup { .jobQueue } } diff --git a/Sources/JobsPostgres/Migrations/CreateJobQueueMetadata.swift b/Sources/JobsPostgres/Migrations/CreateJobQueueMetadata.swift index 44f1904..7c7c2fe 100644 --- a/Sources/JobsPostgres/Migrations/CreateJobQueueMetadata.swift +++ b/Sources/JobsPostgres/Migrations/CreateJobQueueMetadata.swift @@ -12,11 +12,11 @@ // //===----------------------------------------------------------------------===// -import HummingbirdPostgres import Logging +import PostgresMigrations import PostgresNIO -struct CreateJobQueueMetadata: PostgresMigration { +struct CreateJobQueueMetadata: DatabaseMigration { func apply(connection: PostgresConnection, logger: Logger) async throws { try await connection.query( """ @@ -37,5 +37,5 @@ struct CreateJobQueueMetadata: PostgresMigration { } var name: String { "_Create_JobQueue_Metadata_Table_" } - var group: PostgresMigrationGroup { .jobQueue } + var group: DatabaseMigrationGroup { .jobQueue } } diff --git a/Sources/JobsPostgres/Migrations/CreateJobs.swift b/Sources/JobsPostgres/Migrations/CreateJobs.swift index 1bd4e8c..a900263 100644 --- a/Sources/JobsPostgres/Migrations/CreateJobs.swift +++ b/Sources/JobsPostgres/Migrations/CreateJobs.swift @@ -12,11 +12,11 @@ // //===----------------------------------------------------------------------===// -import HummingbirdPostgres import Logging +import PostgresMigrations import PostgresNIO -struct CreateJobs: PostgresMigration { +struct CreateJobs: DatabaseMigration { func apply(connection: PostgresConnection, logger: Logger) async throws { try await connection.query( """ @@ -46,10 +46,10 @@ struct CreateJobs: PostgresMigration { } var name: String { "_Create_Jobs_Table_" } - var group: PostgresMigrationGroup { .jobQueue } + var group: DatabaseMigrationGroup { .jobQueue } } -extension PostgresMigrationGroup { +extension DatabaseMigrationGroup { /// JobQueue migration group public static var jobQueue: Self { .init("_hb_jobqueue") } } diff --git a/Sources/JobsPostgres/PostgresJobsQueue.swift b/Sources/JobsPostgres/PostgresJobsQueue.swift index 22dd9c6..f537d5b 100644 --- a/Sources/JobsPostgres/PostgresJobsQueue.swift +++ b/Sources/JobsPostgres/PostgresJobsQueue.swift @@ -13,11 +13,11 @@ //===----------------------------------------------------------------------===// import Foundation -import HummingbirdPostgres import Jobs import Logging import NIOConcurrencyHelpers import NIOCore +import PostgresMigrations import PostgresNIO /// Postgres Job queue implementation @@ -98,7 +98,7 @@ public final class PostgresJobQueue: JobQueueDriver { /// Logger used by queue public let logger: Logger - let migrations: PostgresMigrations + let migrations: DatabaseMigrations let isStopped: NIOLockedValueBox /// Initialize a PostgresJobQueue @@ -107,7 +107,7 @@ public final class PostgresJobQueue: JobQueueDriver { /// - migrations: Database migrations to update /// - configuration: Queue configuration /// - logger: Logger used by queue - public init(client: PostgresClient, migrations: PostgresMigrations, configuration: Configuration = .init(), logger: Logger) async { + public init(client: PostgresClient, migrations: DatabaseMigrations, configuration: Configuration = .init(), logger: Logger) async { self.client = client self.configuration = configuration self.logger = logger @@ -358,7 +358,7 @@ extension JobQueueDriver where Self == PostgresJobQueue { /// - migrations: Database migrations to update /// - configuration: Queue configuration /// - logger: Logger used by queue - public static func postgres(client: PostgresClient, migrations: PostgresMigrations, configuration: PostgresJobQueue.Configuration = .init(), logger: Logger) async -> Self { + public static func postgres(client: PostgresClient, migrations: DatabaseMigrations, configuration: PostgresJobQueue.Configuration = .init(), logger: Logger) async -> Self { await Self(client: client, migrations: migrations, configuration: configuration, logger: logger) } } diff --git a/Sources/HummingbirdPostgres/Migration.swift b/Sources/PostgresMigrations/Migration.swift similarity index 78% rename from Sources/HummingbirdPostgres/Migration.swift rename to Sources/PostgresMigrations/Migration.swift index 65c597e..e4df9f3 100644 --- a/Sources/HummingbirdPostgres/Migration.swift +++ b/Sources/PostgresMigrations/Migration.swift @@ -18,43 +18,43 @@ import PostgresNIO /// Protocol for a database migration /// /// Requires two functions one to apply the database migration and one to revert it. -public protocol PostgresMigration: Sendable { +public protocol DatabaseMigration: Sendable { /// Apply database migration func apply(connection: PostgresConnection, logger: Logger) async throws /// Revert database migration func revert(connection: PostgresConnection, logger: Logger) async throws - /// Migration name + /// DatabaseMigration name var name: String { get } /// Group migration belongs to - var group: PostgresMigrationGroup { get } + var group: DatabaseMigrationGroup { get } } -extension PostgresMigration { +extension DatabaseMigration { /// Default implementaion of name public var name: String { String(describing: Self.self) } /// Default group is default - public var group: PostgresMigrationGroup { .default } + public var group: DatabaseMigrationGroup { .default } } /// Group identifier for a group of migrations. /// -/// Migrations in one group are treated independently of migrations in other groups. You can add a +/// DatabaseMigrations in one group are treated independently of migrations in other groups. You can add a /// migration to a group and it will not affect any subsequent migrations not in that group. By default /// all migrations belong to the ``default`` group. /// /// To add a migration to a separate group you first need to define the group by adding a static variable -/// to `MigrationGroup`. +/// to `DatabaseMigrationGroup`. /// ``` -/// extension MigrationGroup { +/// extension DatabaseMigrationGroup { /// public static var `myGroup`: Self { .init("myGroup") } /// } /// ``` -/// After that set `PostgresMigration.group` to `.myGroup`. +/// After that set `DatabaseMigration.group` to `.myGroup`. /// /// Only use a group different from `.default` if you are certain that the database elements you are /// creating within that group will always be independent of everything else in the database. Groups /// are useful for libraries that use migrations to setup their database elements. -public struct PostgresMigrationGroup: Hashable, Equatable, Sendable { +public struct DatabaseMigrationGroup: Hashable, Equatable, Sendable { let name: String public init(_ name: String) { diff --git a/Sources/HummingbirdPostgres/MigrationError.swift b/Sources/PostgresMigrations/MigrationError.swift similarity index 87% rename from Sources/HummingbirdPostgres/MigrationError.swift rename to Sources/PostgresMigrations/MigrationError.swift index 8a7eaa4..5c82fd3 100644 --- a/Sources/HummingbirdPostgres/MigrationError.swift +++ b/Sources/PostgresMigrations/MigrationError.swift @@ -26,17 +26,17 @@ public struct PostgresMigrationError: Error, Equatable { } /// The database requires a migration before the application can run - static var requiresChanges: Self { .init(.requiresChanges) } + public static var requiresChanges: Self { .init(.requiresChanges) } /// Cannot revert a migration as we do not have its details. Add it to the revert list using /// PostgresMigrations.add(revert:) - static var cannotRevertMigration: Self { .init(.cannotRevertMigration) } + public static var cannotRevertMigration: Self { .init(.cannotRevertMigration) } } extension PostgresMigrationError: CustomStringConvertible { public var description: String { switch self.value { case .requiresChanges: "Database requires changes. Run `migrate` with `dryRun` set to false." - case .cannotRevertMigration: "Cannot revert migration because we don't have its details. Use `PostgresMigrations.register` to register the Migration." + case .cannotRevertMigration: "Cannot revert migration because we don't have its details. Use `PostgresMigrations.register` to register the DatabaseMigration." } } } diff --git a/Sources/HummingbirdPostgres/Migrations.swift b/Sources/PostgresMigrations/Migrations.swift similarity index 93% rename from Sources/HummingbirdPostgres/Migrations.swift rename to Sources/PostgresMigrations/Migrations.swift index 3c12157..bd3b592 100644 --- a/Sources/HummingbirdPostgres/Migrations.swift +++ b/Sources/PostgresMigrations/Migrations.swift @@ -16,18 +16,18 @@ import Logging import PostgresNIO /// Database migration support -public actor PostgresMigrations { +public actor DatabaseMigrations { enum State { case waiting([CheckedContinuation]) case completed case failed(Error) } - var migrations: [PostgresMigration] - var reverts: [String: PostgresMigration] + var migrations: [DatabaseMigration] + var reverts: [String: DatabaseMigration] var state: State - /// Initialize a PostgresMigrations object + /// Initialize a DatabaseMigrations object public init() { self.migrations = [] self.reverts = [:] @@ -35,16 +35,16 @@ public actor PostgresMigrations { } /// Add migration to list of migrations to be be applied - /// - Parameter migration: Migration to be applied - public func add(_ migration: PostgresMigration) { + /// - Parameter migration: DatabaseMigration to be applied + public func add(_ migration: DatabaseMigration) { self.migrations.append(migration) } /// Register migration without it being applied /// /// This is useful for migrations you might have to revert. - /// - Parameter migration: Migration to be registerd - public func register(_ migration: PostgresMigration) { + /// - Parameter migration: DatabaseMigration to be registerd + public func register(_ migration: DatabaseMigration) { self.reverts[migration.name] = migration } @@ -68,7 +68,7 @@ public actor PostgresMigrations { /// - dryRun: Should migrations actually be applied, or should we just report what would be applied and reverted public func apply( client: PostgresClient, - groups: [PostgresMigrationGroup] = [], + groups: [DatabaseMigrationGroup] = [], logger: Logger, dryRun: Bool ) async throws { @@ -88,7 +88,7 @@ public actor PostgresMigrations { /// - dryRun: Should migrations actually be reverted, or should we just report what would be reverted public func revert( client: PostgresClient, - groups: [PostgresMigrationGroup] = [], + groups: [DatabaseMigrationGroup] = [], logger: Logger, dryRun: Bool ) async throws { @@ -100,8 +100,8 @@ public actor PostgresMigrations { private func migrate( client: PostgresClient, - migrations: [PostgresMigration], - groups: [PostgresMigrationGroup], + migrations: [DatabaseMigration], + groups: [DatabaseMigrationGroup], logger: Logger, completeMigrations: Bool, dryRun: Bool @@ -260,26 +260,26 @@ struct PostgresMigrationRepository: Sendable { try await self.createMigrationsTable(connection: context.connection, logger: context.logger) } - func add(_ migration: PostgresMigration, context: Context) async throws { + func add(_ migration: DatabaseMigration, context: Context) async throws { try await context.connection.query( "INSERT INTO _hb_pg_migrations (\"name\", \"group\") VALUES (\(migration.name), \(migration.group.name))", logger: context.logger ) } - func remove(_ migration: PostgresMigration, context: Context) async throws { + func remove(_ migration: DatabaseMigration, context: Context) async throws { try await context.connection.query( "DELETE FROM _hb_pg_migrations WHERE name = \(migration.name)", logger: context.logger ) } - func getAll(context: Context) async throws -> [(name: String, group: PostgresMigrationGroup)] { + func getAll(context: Context) async throws -> [(name: String, group: DatabaseMigrationGroup)] { let stream = try await context.connection.query( "SELECT \"name\", \"group\" FROM _hb_pg_migrations ORDER BY \"order\"", logger: context.logger ) - var result: [(String, PostgresMigrationGroup)] = [] + var result: [(String, DatabaseMigrationGroup)] = [] for try await (name, group) in stream.decode((String, String).self, context: .default) { result.append((name, .init(group))) } diff --git a/Tests/HummingbirdPostgresTests/PersistTests.swift b/Tests/HummingbirdPostgresTests/PersistTests.swift index 0aed387..3c47539 100644 --- a/Tests/HummingbirdPostgresTests/PersistTests.swift +++ b/Tests/HummingbirdPostgresTests/PersistTests.swift @@ -16,10 +16,23 @@ import Hummingbird import HummingbirdPostgres import HummingbirdTesting import Logging +import PostgresMigrations import PostgresNIO import ServiceLifecycle import XCTest +func getPostgresConfiguration() async throws -> PostgresClient.Configuration { + let env = try await Environment().merging(with: .dotEnv()) + return .init( + host: env.get("POSTGRES_HOSTNAME") ?? "localhost", + port: env.get("POSTGRES_PORT", as: Int.self) ?? 5432, + username: env.get("POSTGRES_USER") ?? "test_user", + password: env.get("POSTGRES_PASSWORD") ?? "test_password", + database: env.get("POSTGRES_DB") ?? "test_db", + tls: .disable + ) +} + final class PersistTests: XCTestCase { static func createApplication(_ updateRouter: (Router, PersistDriver) -> Void = { _, _ in }) async throws -> some ApplicationProtocol { struct PostgresErrorMiddleware: RouterMiddleware { @@ -43,7 +56,7 @@ final class PersistTests: XCTestCase { configuration: getPostgresConfiguration(), backgroundLogger: logger ) - let postgresMigrations = PostgresMigrations() + let postgresMigrations = DatabaseMigrations() let persist = await PostgresPersistDriver(client: postgresClient, migrations: postgresMigrations, logger: logger) let router = Router() router.middlewares.add(PostgresErrorMiddleware()) diff --git a/Tests/HummingbirdPostgresTests/TestUtils.swift b/Tests/HummingbirdPostgresTests/TestUtils.swift deleted file mode 100644 index 2754481..0000000 --- a/Tests/HummingbirdPostgresTests/TestUtils.swift +++ /dev/null @@ -1,15 +0,0 @@ -import Hummingbird -import PostgresNIO -import ServiceLifecycle - -func getPostgresConfiguration() async throws -> PostgresClient.Configuration { - let env = try await Environment().merging(with: .dotEnv()) - return .init( - host: env.get("POSTGRES_HOSTNAME") ?? "localhost", - port: env.get("POSTGRES_PORT", as: Int.self) ?? 5432, - username: env.get("POSTGRES_USER") ?? "test_user", - password: env.get("POSTGRES_PASSWORD") ?? "test_password", - database: env.get("POSTGRES_DB") ?? "test_db", - tls: .disable - ) -} diff --git a/Tests/HummingbirdPostgresTests/JobsTests.swift b/Tests/JobsPostgresTests/JobsTests.swift similarity index 96% rename from Tests/HummingbirdPostgresTests/JobsTests.swift rename to Tests/JobsPostgresTests/JobsTests.swift index d778b99..9b7b020 100644 --- a/Tests/HummingbirdPostgresTests/JobsTests.swift +++ b/Tests/JobsPostgresTests/JobsTests.swift @@ -14,15 +14,26 @@ import Atomics import Hummingbird -@testable import HummingbirdPostgres import HummingbirdTesting import Jobs @testable import JobsPostgres import NIOConcurrencyHelpers +import PostgresMigrations import PostgresNIO import ServiceLifecycle import XCTest +func getPostgresConfiguration() async throws -> PostgresClient.Configuration { + return .init( + host: ProcessInfo.processInfo.environment["POSTGRES_HOSTNAME"] ?? "localhost", + port: 5432, + username: ProcessInfo.processInfo.environment["POSTGRES_USER"] ?? "test_user", + password: ProcessInfo.processInfo.environment["POSTGRES_PASSWORD"] ?? "test_password", + database: ProcessInfo.processInfo.environment["POSTGRES_DB"] ?? "test_db", + tls: .disable + ) +} + extension XCTestExpectation { convenience init(description: String, expectedFulfillmentCount: Int) { self.init(description: description) @@ -51,7 +62,7 @@ final class JobsTests: XCTestCase { configuration: getPostgresConfiguration(), backgroundLogger: logger ) - let postgresMigrations = PostgresMigrations() + let postgresMigrations = DatabaseMigrations() return await JobQueue( .postgres( client: postgresClient, @@ -393,7 +404,7 @@ final class JobsTests: XCTestCase { configuration: getPostgresConfiguration(), backgroundLogger: logger ) - let postgresMigrations = PostgresMigrations() + let postgresMigrations = DatabaseMigrations() let jobQueue = await JobQueue( .postgres( client: postgresClient, @@ -404,7 +415,7 @@ final class JobsTests: XCTestCase { numWorkers: 2, logger: logger ) - let postgresMigrations2 = PostgresMigrations() + let postgresMigrations2 = DatabaseMigrations() let jobQueue2 = await JobQueue( .postgres( client: postgresClient, @@ -455,7 +466,7 @@ final class JobsTests: XCTestCase { group.addTask { await postgresClient.run() } - let postgresMigrations = PostgresMigrations() + let postgresMigrations = DatabaseMigrations() let jobQueue = await PostgresJobQueue( client: postgresClient, migrations: postgresMigrations, diff --git a/Tests/HummingbirdPostgresTests/MigrationTests.swift b/Tests/PostgresMigrationsTests/MigrationTests.swift similarity index 94% rename from Tests/HummingbirdPostgresTests/MigrationTests.swift rename to Tests/PostgresMigrationsTests/MigrationTests.swift index 0c54a01..b30c7a7 100644 --- a/Tests/HummingbirdPostgresTests/MigrationTests.swift +++ b/Tests/PostgresMigrationsTests/MigrationTests.swift @@ -1,12 +1,24 @@ import Atomics -@testable import HummingbirdPostgres +import Foundation import Logging +@testable import PostgresMigrations import PostgresNIO import XCTest +func getPostgresConfiguration() async throws -> PostgresClient.Configuration { + return .init( + host: ProcessInfo.processInfo.environment["POSTGRES_HOSTNAME"] ?? "localhost", + port: 5432, + username: ProcessInfo.processInfo.environment["POSTGRES_USER"] ?? "test_user", + password: ProcessInfo.processInfo.environment["POSTGRES_PASSWORD"] ?? "test_password", + database: ProcessInfo.processInfo.environment["POSTGRES_DB"] ?? "test_db", + tls: .disable + ) +} + final class MigrationTests: XCTestCase { /// Test migration used to verify order or apply and reverts - struct TestMigration: PostgresMigration { + struct TestMigration: DatabaseMigration { final class Order: Sendable { let value: ManagedAtomic @@ -25,7 +37,7 @@ final class MigrationTests: XCTestCase { order: Order = Order(), applyOrder: Int? = nil, revertOrder: Int? = nil, - group: PostgresMigrationGroup = .default + group: DatabaseMigrationGroup = .default ) { self.order = order self.name = name @@ -47,7 +59,7 @@ final class MigrationTests: XCTestCase { } let name: String - let group: PostgresMigrationGroup + let group: DatabaseMigrationGroup let order: Order let expectedApply: Int? let expectedRevert: Int? @@ -59,9 +71,9 @@ final class MigrationTests: XCTestCase { func testMigrations( revert: Bool = true, - groups: [PostgresMigrationGroup] = [.default], - _ setup: (PostgresMigrations) async throws -> Void, - verify: (PostgresMigrations, PostgresClient) async throws -> Void + groups: [DatabaseMigrationGroup] = [.default], + _ setup: (DatabaseMigrations) async throws -> Void, + verify: (DatabaseMigrations, PostgresClient) async throws -> Void ) async throws { let logger = { var logger = Logger(label: "MigrationTests") @@ -72,7 +84,7 @@ final class MigrationTests: XCTestCase { configuration: getPostgresConfiguration(), backgroundLogger: logger ) - let migrations = PostgresMigrations() + let migrations = DatabaseMigrations() try await setup(migrations) do { try await withThrowingTaskGroup(of: Void.self) { group in @@ -93,7 +105,7 @@ final class MigrationTests: XCTestCase { } } - func getAll(client: PostgresClient, groups: [PostgresMigrationGroup] = [.default]) async throws -> [String] { + func getAll(client: PostgresClient, groups: [DatabaseMigrationGroup] = [.default]) async throws -> [String] { let repository = PostgresMigrationRepository(client: client) return try await repository.withContext(logger: Self.logger) { context in try await repository.getAll(context: context).compactMap { migration in @@ -336,6 +348,6 @@ final class MigrationTests: XCTestCase { } } -extension PostgresMigrationGroup { +extension DatabaseMigrationGroup { static var test: Self { .init("test") } } From bb19e94deb3e4a4c193a9384517015f0b175a7d3 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Sun, 15 Sep 2024 11:38:28 +0100 Subject: [PATCH 2/4] Make sure we remove all the Hummingbird references... ... from jobs and migrations --- Package.swift | 2 -- Package@swift-6.0.swift | 2 -- Tests/JobsPostgresTests/JobsTests.swift | 4 ---- 3 files changed, 8 deletions(-) diff --git a/Package.swift b/Package.swift index e27fce6..e33add4 100644 --- a/Package.swift +++ b/Package.swift @@ -55,14 +55,12 @@ let package = Package( name: "PostgresMigrationsTests", dependencies: [ "PostgresMigrations", - .product(name: "HummingbirdTesting", package: "hummingbird"), ] ), .testTarget( name: "JobsPostgresTests", dependencies: [ "JobsPostgres", - .product(name: "HummingbirdTesting", package: "hummingbird"), ] ), ] diff --git a/Package@swift-6.0.swift b/Package@swift-6.0.swift index e42a37d..dfd8218 100644 --- a/Package@swift-6.0.swift +++ b/Package@swift-6.0.swift @@ -49,14 +49,12 @@ let package = Package( name: "PostgresMigrationsTests", dependencies: [ "PostgresMigrations", - .product(name: "HummingbirdTesting", package: "hummingbird"), ] ), .testTarget( name: "JobsPostgresTests", dependencies: [ "JobsPostgres", - .product(name: "HummingbirdTesting", package: "hummingbird"), ] ), ] diff --git a/Tests/JobsPostgresTests/JobsTests.swift b/Tests/JobsPostgresTests/JobsTests.swift index 9b7b020..f3f9770 100644 --- a/Tests/JobsPostgresTests/JobsTests.swift +++ b/Tests/JobsPostgresTests/JobsTests.swift @@ -13,8 +13,6 @@ //===----------------------------------------------------------------------===// import Atomics -import Hummingbird -import HummingbirdTesting import Jobs @testable import JobsPostgres import NIOConcurrencyHelpers @@ -50,8 +48,6 @@ final class JobsTests: XCTestCase { #endif } - static let env = Environment() - func createJobQueue(numWorkers: Int, configuration: PostgresJobQueue.Configuration, function: String = #function) async throws -> JobQueue { let logger = { var logger = Logger(label: function) From 8fdbf7aa2887a7e8516aa521e0caee6feca09f4e Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Sun, 15 Sep 2024 11:46:17 +0100 Subject: [PATCH 3/4] Add PostgresMigrations product --- Package@swift-6.0.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Package@swift-6.0.swift b/Package@swift-6.0.swift index dfd8218..f903bbb 100644 --- a/Package@swift-6.0.swift +++ b/Package@swift-6.0.swift @@ -8,6 +8,7 @@ let package = Package( platforms: [.macOS(.v14), .iOS(.v17), .tvOS(.v17)], products: [ .library(name: "HummingbirdPostgres", targets: ["HummingbirdPostgres"]), + .library(name: "PostgresMigrations", targets: ["PostgresMigrations"]), .library(name: "JobsPostgres", targets: ["JobsPostgres"]), ], dependencies: [ From aaaab3edd33eaf4bd4005907b809030b3c407a27 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Sun, 15 Sep 2024 11:54:20 +0100 Subject: [PATCH 4/4] Add deprecated symbols --- Sources/PostgresMigrations/Deprecations.swift | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Sources/PostgresMigrations/Deprecations.swift diff --git a/Sources/PostgresMigrations/Deprecations.swift b/Sources/PostgresMigrations/Deprecations.swift new file mode 100644 index 0000000..0f7809b --- /dev/null +++ b/Sources/PostgresMigrations/Deprecations.swift @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Hummingbird server framework project +// +// Copyright (c) 2024 the Hummingbird authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +// Below is a list of unavailable symbols with the "HB" prefix. These are available +// temporarily to ease transition from the old symbols that included the "HB" +// prefix to the new ones. +// +// This file will be removed before we do a 2.0 release + +@_documentation(visibility: internal) @available(*, deprecated, renamed: "DatabaseMigration") +public typealias PostgresMigration = DatabaseMigration +@_documentation(visibility: internal) @available(*, deprecated, renamed: "DatabaseMigrations") +public typealias PostgresMigrations = DatabaseMigrations +@_documentation(visibility: internal) @available(*, deprecated, renamed: "DatabaseMigrationGroup") +public typealias PostgresMigrationGroup = DatabaseMigrationGroup