Skip to content

Commit

Permalink
Use @mainactor for accessing migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Dec 25, 2023
1 parent 568da4b commit 30ef912
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
20 changes: 17 additions & 3 deletions Sources/HummingbirdFluent/Fluent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,31 @@ import FluentKit
import Hummingbird
import ServiceLifecycle

@MainActor
public struct MainActorBox<Value>: Sendable {
let value: Value
}

extension Databases: @unchecked Sendable {}
extension DatabaseID: @unchecked Sendable {}

/// Manage fluent databases and migrations
///
/// You can either create this separate from `HBApplication` or add it to your application
/// using `HBApplication.addFluent`.
public struct HBFluent: @unchecked Sendable, Service {
public struct HBFluent: Sendable, Service {
/// Databases attached
public let databases: Databases
/// List of migrations
public let migrations: Migrations
let _migrations: MainActorBox<Migrations>
/// Event loop group
public let eventLoopGroup: EventLoopGroup
/// Logger
public let logger: Logger

@MainActor
public var migrations: Migrations { self._migrations.value }

/// Initialize HBFluent
/// - Parameters:
/// - eventLoopGroup: EventLoopGroup used by databases
Expand All @@ -42,7 +53,7 @@ public struct HBFluent: @unchecked Sendable, Service {
) {
let eventLoopGroup = eventLoopGroupProvider.eventLoopGroup
self.databases = Databases(threadPool: threadPool, on: eventLoopGroup)
self.migrations = .init()
self._migrations = .init(value: .init())
self.eventLoopGroup = eventLoopGroup
self.logger = logger
}
Expand All @@ -53,6 +64,7 @@ public struct HBFluent: @unchecked Sendable, Service {
}

/// fluent migrator
@MainActor
public var migrator: Migrator {
Migrator(
databases: self.databases,
Expand All @@ -63,12 +75,14 @@ public struct HBFluent: @unchecked Sendable, Service {
}

/// Run migration if needed
@MainActor
public func migrate() async throws {
try await self.migrator.setupIfNeeded().get()
try await self.migrator.prepareBatch().get()
}

/// Run revert if needed
@MainActor
public func revert() async throws {
try await self.migrator.setupIfNeeded().get()
try await self.migrator.revertAllBatches().get()
Expand Down
4 changes: 2 additions & 2 deletions Sources/HummingbirdFluent/Persist+fluent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public final class HBFluentPersistDriver: HBPersistDriver {
/// - Parameters:
/// - fluent: Fluent setup
/// - databaseID: ID of database to use
public init(fluent: HBFluent, databaseID: DatabaseID? = nil) {
public init(fluent: HBFluent, databaseID: DatabaseID? = nil) async {
self.fluent = fluent
self.databaseID = databaseID
self.fluent.migrations.add(CreatePersistModel())
await self.fluent.migrations.add(CreatePersistModel())
self.tidy()
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/HummingbirdFluentTests/FluentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ final class FluentTests: XCTestCase {
fluent.databases.use(.sqlite(.memory), as: .sqlite)
// fluent.databases.use(.postgres(hostname: "localhost", username: "postgres", password: "vapor", database: "vapor"), as: .psql)
// add migration
fluent.migrations.add(CreatePlanet())
await fluent.migrations.add(CreatePlanet())
// run migrations
try await fluent.migrate()

Expand Down
2 changes: 1 addition & 1 deletion Tests/HummingbirdFluentTests/PersistTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class PersistTests: XCTestCase {
// add sqlite database
fluent.databases.use(.sqlite(.memory), as: .sqlite)
// fluent.databases.use(.postgres(hostname: "localhost", username: "postgres", password: "vapor", database: "vapor"), as: .psql)
let persist = HBFluentPersistDriver(fluent: fluent)
let persist = await HBFluentPersistDriver(fluent: fluent)
// run migrations
try await fluent.migrate()

Expand Down

0 comments on commit 30ef912

Please sign in to comment.