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

Remove "HB" prefix from all symbols #11

Merged
merged 3 commits into from Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.library(name: "HummingbirdPostgres", targets: ["HummingbirdPostgres"]),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", branch: "2.x.x"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", branch: "main"),
.package(url: "https://github.com/vapor/postgres-nio", from: "1.20.0"),
],
targets: [
Expand Down
Expand Up @@ -16,7 +16,7 @@ import HummingbirdPostgres
import Logging
@_spi(ConnectionPool) import PostgresNIO

struct CreateJobQueue: HBPostgresMigration {
struct CreateJobQueue: PostgresMigration {
func apply(connection: PostgresConnection, logger: Logger) async throws {
try await connection.query(
"""
Expand Down Expand Up @@ -44,5 +44,5 @@ struct CreateJobQueue: HBPostgresMigration {
}

var name: String { "_Create_JobQueue_Table_" }
var group: HBMigrationGroup { .jobQueue }
var group: MigrationGroup { .jobQueue }
}
6 changes: 3 additions & 3 deletions Sources/HummingbirdJobsPostgres/Migrations/CreateJobs.swift
Expand Up @@ -16,7 +16,7 @@ import HummingbirdPostgres
import Logging
@_spi(ConnectionPool) import PostgresNIO

struct CreateJobs: HBPostgresMigration {
struct CreateJobs: PostgresMigration {
func apply(connection: PostgresConnection, logger: Logger) async throws {
try await connection.query(
"""
Expand All @@ -39,10 +39,10 @@ struct CreateJobs: HBPostgresMigration {
}

var name: String { "_Create_Jobs_Table_" }
var group: HBMigrationGroup { .jobQueue }
var group: MigrationGroup { .jobQueue }
}

extension HBMigrationGroup {
extension MigrationGroup {
/// JobQueue migration group
public static var jobQueue: Self { .init("_hb_jobqueue") }
}
30 changes: 15 additions & 15 deletions Sources/HummingbirdJobsPostgres/PostgresJobsQueue.swift
Expand Up @@ -21,7 +21,7 @@ import NIOCore
@_spi(ConnectionPool) import PostgresNIO

@_spi(ConnectionPool)
public final class HBPostgresQueue: HBJobQueueDriver {
public final class PostgresQueue: JobQueueDriver {
public typealias JobID = UUID

/// what to do with failed/processing jobs from last time queue was handled
Expand All @@ -31,7 +31,7 @@ public final class HBPostgresQueue: HBJobQueueDriver {
case remove
}

/// Errors thrown by HBPostgresJobQueue
/// Errors thrown by PostgresJobQueue
public enum PostgresQueueError: Error, CustomStringConvertible {
case failedToAdd

Expand Down Expand Up @@ -77,11 +77,11 @@ public final class HBPostgresQueue: HBJobQueueDriver {
/// Logger used by queue
public let logger: Logger

let migrations: HBPostgresMigrations
let migrations: PostgresMigrations
let isStopped: NIOLockedValueBox<Bool>

/// Initialize a HBPostgresJobQueue
public init(client: PostgresClient, migrations: HBPostgresMigrations, configuration: Configuration = .init(), logger: Logger) async {
/// Initialize a PostgresJobQueue
public init(client: PostgresClient, migrations: PostgresMigrations, configuration: Configuration = .init(), logger: Logger) async {
self.client = client
self.configuration = configuration
self.logger = logger
Expand Down Expand Up @@ -112,7 +112,7 @@ public final class HBPostgresQueue: HBJobQueueDriver {
/// - Returns: Identifier of queued job
@discardableResult public func push(_ buffer: ByteBuffer) async throws -> JobID {
try await self.client.withConnection { connection in
let queuedJob = HBQueuedJob<JobID>(id: .init(), jobBuffer: buffer)
let queuedJob = QueuedJob<JobID>(id: .init(), jobBuffer: buffer)
try await add(queuedJob, connection: connection)
try await addToQueue(jobId: queuedJob.id, connection: connection)
return queuedJob.id
Expand Down Expand Up @@ -141,9 +141,9 @@ public final class HBPostgresQueue: HBJobQueueDriver {
/// shutdown queue once all active jobs have been processed
public func shutdownGracefully() async {}

func popFirst() async throws -> HBQueuedJob<JobID>? {
func popFirst() async throws -> QueuedJob<JobID>? {
do {
return try await self.client.withConnection { connection -> HBQueuedJob? in
return try await self.client.withConnection { connection -> QueuedJob? in
while true {
try Task.checkCancellation()
let stream = try await connection.query(
Expand Down Expand Up @@ -176,7 +176,7 @@ public final class HBPostgresQueue: HBJobQueueDriver {
guard let buffer = try await stream2.decode(ByteBuffer.self, context: .default).first(where: { _ in true }) else {
continue
}
return HBQueuedJob(id: jobId, jobBuffer: buffer)
return QueuedJob(id: jobId, jobBuffer: buffer)
} catch {
try await self.setStatus(jobId: jobId, status: .failed, connection: connection)
throw JobQueueError.decodeJobFailed
Expand All @@ -189,7 +189,7 @@ public final class HBPostgresQueue: HBJobQueueDriver {
}
}

func add(_ job: HBQueuedJob<JobID>, connection: PostgresConnection) async throws {
func add(_ job: QueuedJob<JobID>, connection: PostgresConnection) async throws {
try await connection.query(
"""
INSERT INTO _hb_pg_jobs (id, job, status)
Expand Down Expand Up @@ -256,10 +256,10 @@ public final class HBPostgresQueue: HBJobQueueDriver {
}
}

/// extend HBPostgresJobQueue to conform to AsyncSequence
extension HBPostgresQueue {
/// extend PostgresJobQueue to conform to AsyncSequence
extension PostgresQueue {
public struct AsyncIterator: AsyncIteratorProtocol {
let queue: HBPostgresQueue
let queue: PostgresQueue

public func next() async throws -> Element? {
while true {
Expand All @@ -281,13 +281,13 @@ extension HBPostgresQueue {
}

@_spi(ConnectionPool)
extension HBJobQueueDriver where Self == HBPostgresQueue {
extension JobQueueDriver where Self == PostgresQueue {
/// Return Postgres driver for Job Queue
/// - Parameters:
/// - client: Postgres client
/// - configuration: Queue configuration
/// - logger: Logger used by queue
public static func postgres(client: PostgresClient, migrations: HBPostgresMigrations, configuration: HBPostgresQueue.Configuration = .init(), logger: Logger) async -> Self {
public static func postgres(client: PostgresClient, migrations: PostgresMigrations, configuration: PostgresQueue.Configuration = .init(), logger: Logger) async -> Self {
await Self(client: client, migrations: migrations, configuration: configuration, logger: logger)
}
}
6 changes: 3 additions & 3 deletions Sources/HummingbirdPostgres/CreatePersistTable.swift
Expand Up @@ -15,7 +15,7 @@
import Logging
@_spi(ConnectionPool) import PostgresNIO

struct CreatePersistTable: HBPostgresMigration {
struct CreatePersistTable: PostgresMigration {
func apply(connection: PostgresConnection, logger: Logger) async throws {
try await connection.query(
"""
Expand All @@ -37,10 +37,10 @@ struct CreatePersistTable: HBPostgresMigration {
}

var name: String { "_Create_Persist_Table_" }
var group: HBMigrationGroup { .persist }
var group: MigrationGroup { .persist }
}

extension HBMigrationGroup {
extension MigrationGroup {
/// Persist driver migration group
public static var persist: Self { .init("_hb_pg_persist") }
}
18 changes: 9 additions & 9 deletions Sources/HummingbirdPostgres/Migration.swift
Expand Up @@ -18,43 +18,43 @@ import Logging
/// Protocol for a database migration
///
/// Requires two functions one to apply the database migration and one to revert it.
public protocol HBPostgresMigration {
public protocol PostgresMigration {
/// Apply database migration
func apply(connection: PostgresConnection, logger: Logger) async throws
/// Revert database migration
func revert(connection: PostgresConnection, logger: Logger) async throws
/// Migration name
var name: String { get }
/// Group migration belongs to
var group: HBMigrationGroup { get }
var group: MigrationGroup { get }
}

extension HBPostgresMigration {
extension PostgresMigration {
/// Default implementaion of name
public var name: String { String(describing: Self.self) }
/// Default group is default
public var group: HBMigrationGroup { .default }
public var group: MigrationGroup { .default }
}

/// Group identifier for a group of migrations.
///
/// Migrations 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 ``HBMigrationGroup.default`` group.
/// all migrations belong to the ``MigrationGroup.default`` group.
///
/// To add a migration to a separate group you first need to define the group by adding a static variable
/// to `HBMigrationGroup`.
/// to `MigrationGroup`.
/// ```
/// extension HBMigrationGroup {
/// extension MigrationGroup {
/// public static var `myGroup`: Self { .init("myGroup") }
/// }
/// ```
/// After that use to ``HBPostgresMigration.group`` set the group for a migration.
/// After that use to ``PostgresMigration.group`` set the group for a migration.
///
/// 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 HBMigrationGroup: Hashable, Equatable {
public struct MigrationGroup: Hashable, Equatable {
let name: String

public init(_ name: String) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/HummingbirdPostgres/MigrationError.swift
Expand Up @@ -13,7 +13,7 @@
//===----------------------------------------------------------------------===//

/// Error thrown by migration code
public struct HBPostgresMigrationError: Error, Equatable {
public struct PostgresMigrationError: Error, Equatable {
enum _Internal {
case requiresChanges
case cannotRevertMigration
Expand All @@ -28,6 +28,6 @@ public struct HBPostgresMigrationError: Error, Equatable {
/// The database requires a migration before the application can run
static var requiresChanges: Self { .init(.requiresChanges) }
/// Cannot revert a migration as we do not have its details. Add it to the revert list using
/// HBPostgresMigrations.add(revert:)
/// PostgresMigrations.add(revert:)
static var cannotRevertMigration: Self { .init(.cannotRevertMigration) }
}
36 changes: 18 additions & 18 deletions Sources/HummingbirdPostgres/Migrations.swift
Expand Up @@ -16,18 +16,18 @@ import Logging
@_spi(ConnectionPool) import PostgresNIO

/// Database migration support
public actor HBPostgresMigrations {
public actor PostgresMigrations {
enum State {
case waiting([CheckedContinuation<Void, Error>])
case completed
case failed(Error)
}

var migrations: [HBPostgresMigration]
var reverts: [String: HBPostgresMigration]
var migrations: [PostgresMigration]
var reverts: [String: PostgresMigration]
var state: State

/// Initialize a HBPostgresMigrations object
/// Initialize a PostgresMigrations object
public init() {
self.migrations = []
self.reverts = [:]
Expand All @@ -36,13 +36,13 @@ public actor HBPostgresMigrations {

/// Add migration to list of migrations to be be applied
/// - Parameter migration: Migration to be applied
public func add(_ migration: HBPostgresMigration) {
public func add(_ migration: PostgresMigration) {
self.migrations.append(migration)
}

/// Add migration to list of reverts, that can be applied
/// - Parameter migration: Migration to be reverted if necessary
public func revert(_ migration: HBPostgresMigration) {
public func revert(_ migration: PostgresMigration) {
self.reverts[migration.name] = migration
}

Expand All @@ -64,7 +64,7 @@ public actor HBPostgresMigrations {
/// - logger: Logger to use
/// - dryRun: Should migrations actually be applied, or should we just report what would be applied and reverted
@_spi(ConnectionPool)
public func apply(client: PostgresClient, groups: [HBMigrationGroup] = [], logger: Logger, dryRun: Bool) async throws {
public func apply(client: PostgresClient, groups: [MigrationGroup] = [], logger: Logger, dryRun: Bool) async throws {
try await self.migrate(client: client, migrations: self.migrations, groups: groups, logger: logger, completeMigrations: true, dryRun: dryRun)
}

Expand All @@ -74,14 +74,14 @@ public actor HBPostgresMigrations {
/// - logger: Logger to use
/// - dryRun: Should migrations actually be reverted, or should we just report what would be reverted
@_spi(ConnectionPool)
public func revert(client: PostgresClient, groups: [HBMigrationGroup] = [], logger: Logger, dryRun: Bool) async throws {
public func revert(client: PostgresClient, groups: [MigrationGroup] = [], logger: Logger, dryRun: Bool) async throws {
try await self.migrate(client: client, migrations: [], groups: groups, logger: logger, completeMigrations: false, dryRun: dryRun)
}

private func migrate(
client: PostgresClient,
migrations: [HBPostgresMigration],
groups: [HBMigrationGroup],
migrations: [PostgresMigration],
groups: [MigrationGroup],
logger: Logger,
completeMigrations: Bool,
dryRun: Bool
Expand All @@ -92,7 +92,7 @@ public actor HBPostgresMigrations {
case .waiting:
break
}
let repository = HBPostgresMigrationRepository(client: client)
let repository = PostgresMigrationRepository(client: client)
do {
_ = try await repository.withContext(logger: logger) { context in
// setup migration repository (create table)
Expand Down Expand Up @@ -121,7 +121,7 @@ public actor HBPostgresMigrations {
// look for migration to revert in migration list and revert dictionary. NB we are looking in the migration
// array belonging to the type, not the one supplied to the function
guard let migration = self.migrations.first(where: { $0.name == migrationName }) ?? self.reverts[migrationName] else {
throw HBPostgresMigrationError.cannotRevertMigration
throw PostgresMigrationError.cannotRevertMigration
}
logger.info("Reverting \(migration.name) from group \(group.name) \(dryRun ? " (dry run)" : "")")
if !dryRun {
Expand All @@ -145,7 +145,7 @@ public actor HBPostgresMigrations {
}
// if changes are required
guard requiresChanges == false else {
throw HBPostgresMigrationError.requiresChanges
throw PostgresMigrationError.requiresChanges
}
}
} catch {
Expand Down Expand Up @@ -202,7 +202,7 @@ public actor HBPostgresMigrations {
}

/// Create, remove and list migrations
struct HBPostgresMigrationRepository {
struct PostgresMigrationRepository {
struct Context {
let connection: PostgresConnection
let logger: Logger
Expand All @@ -220,26 +220,26 @@ struct HBPostgresMigrationRepository {
try await self.createMigrationsTable(connection: context.connection, logger: context.logger)
}

func add(_ migration: HBPostgresMigration, context: Context) async throws {
func add(_ migration: PostgresMigration, context: Context) async throws {
try await context.connection.query(
"INSERT INTO _hb_migrations (\"name\", \"group\") VALUES (\(migration.name), \(migration.group.name))",
logger: context.logger
)
}

func remove(_ migration: HBPostgresMigration, context: Context) async throws {
func remove(_ migration: PostgresMigration, context: Context) async throws {
try await context.connection.query(
"DELETE FROM _hb_migrations WHERE name = \(migration.name)",
logger: context.logger
)
}

func getAll(context: Context) async throws -> [(name: String, group: HBMigrationGroup)] {
func getAll(context: Context) async throws -> [(name: String, group: MigrationGroup)] {
let stream = try await context.connection.query(
"SELECT \"name\", \"group\" FROM _hb_migrations ORDER BY \"order\"",
logger: context.logger
)
var result: [(String, HBMigrationGroup)] = []
var result: [(String, MigrationGroup)] = []
for try await (name, group) in stream.decode((String, String).self, context: .default) {
result.append((name, .init(group)))
}
Expand Down