Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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"),
],
Expand All @@ -39,10 +48,20 @@ let package = Package(
name: "HummingbirdPostgresTests",
dependencies: [
"HummingbirdPostgres",
"JobsPostgres",
.product(name: "HummingbirdTesting", package: "hummingbird"),
],
swiftSettings: swiftSettings
]
),
.testTarget(
name: "PostgresMigrationsTests",
dependencies: [
"PostgresMigrations",
]
),
.testTarget(
name: "JobsPostgresTests",
dependencies: [
"JobsPostgres",
]
),
]
)
23 changes: 21 additions & 2 deletions Package@swift-6.0.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -19,14 +20,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"),
]
Expand All @@ -35,9 +43,20 @@ let package = Package(
name: "HummingbirdPostgresTests",
dependencies: [
"HummingbirdPostgres",
"JobsPostgres",
.product(name: "HummingbirdTesting", package: "hummingbird"),
]
),
.testTarget(
name: "PostgresMigrationsTests",
dependencies: [
"PostgresMigrations",
]
),
.testTarget(
name: "JobsPostgresTests",
dependencies: [
"JobsPostgres",
]
),
]
)
4 changes: 2 additions & 2 deletions Snippets/PSQLSoakTestQueue.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import HummingbirdPostgres
import Jobs
import JobsPostgres
import Logging
import NIOCore
import NIOPosix
import PostgresMigrations
import PostgresNIO
import ServiceLifecycle

Expand All @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions Sources/HummingbirdPostgres/CreatePersistTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand All @@ -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") }
}
7 changes: 4 additions & 3 deletions Sources/HummingbirdPostgres/PostgresPersistDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import AsyncAlgorithms
import Foundation
import Hummingbird
import NIOCore
import PostgresMigrations
import PostgresNIO

extension PSQLError {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Sources/JobsPostgres/Migrations/CreateJobDelay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand All @@ -34,5 +34,5 @@ struct CreateJobDelay: PostgresMigration {
}

var name: String { "_Create_JobQueueDelay_Table_" }
var group: PostgresMigrationGroup { .jobQueue }
var group: DatabaseMigrationGroup { .jobQueue }
}
6 changes: 3 additions & 3 deletions Sources/JobsPostgres/Migrations/CreateJobQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand Down Expand Up @@ -44,5 +44,5 @@ struct CreateJobQueue: PostgresMigration {
}

var name: String { "_Create_JobQueue_Table_" }
var group: PostgresMigrationGroup { .jobQueue }
var group: DatabaseMigrationGroup { .jobQueue }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand All @@ -37,5 +37,5 @@ struct CreateJobQueueMetadata: PostgresMigration {
}

var name: String { "_Create_JobQueue_Metadata_Table_" }
var group: PostgresMigrationGroup { .jobQueue }
var group: DatabaseMigrationGroup { .jobQueue }
}
8 changes: 4 additions & 4 deletions Sources/JobsPostgres/Migrations/CreateJobs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand Down Expand Up @@ -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") }
}
8 changes: 4 additions & 4 deletions Sources/JobsPostgres/PostgresJobsQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
//===----------------------------------------------------------------------===//

import Foundation
import HummingbirdPostgres
import Jobs
import Logging
import NIOConcurrencyHelpers
import NIOCore
import PostgresMigrations
import PostgresNIO

/// Postgres Job queue implementation
Expand Down Expand Up @@ -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<Bool>

/// Initialize a PostgresJobQueue
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
26 changes: 26 additions & 0 deletions Sources/PostgresMigrations/Deprecations.swift
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading