Skip to content

Commit

Permalink
Remove DatabaseID: @unchecked Sendable now it us Sendable
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Jan 11, 2024
1 parent a6f6454 commit a7df20c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/apple/swift-async-algorithms.git", from: "1.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", branch: "2.x.x"),
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.17.0"),
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.45.1"),
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.0.0"),
// used in tests
.package(url: "https://github.com/vapor/fluent-sqlite-driver.git", from: "4.0.0"),
// .package(url: "https://github.com/vapor/fluent-postgres-driver.git", from: "2.0.0"),
.package(url: "https://github.com/vapor/fluent-postgres-driver.git", from: "2.0.0"),
],
targets: [
.target(name: "HummingbirdFluent", dependencies: [
Expand All @@ -27,7 +27,7 @@ let package = Package(
]),
.testTarget(name: "HummingbirdFluentTests", dependencies: [
.product(name: "FluentSQLiteDriver", package: "fluent-sqlite-driver"),
// .product(name: "FluentPostgresDriver", package: "fluent-postgres-driver"),
.product(name: "FluentPostgresDriver", package: "fluent-postgres-driver"),
.byName(name: "HummingbirdFluent"),
.product(name: "HummingbirdFoundation", package: "hummingbird"),
.product(name: "HummingbirdXCT", package: "hummingbird"),
Expand Down
28 changes: 20 additions & 8 deletions Sources/HummingbirdFluent/Fluent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public struct MainActorBox<Value>: Sendable {
public let value: Value
}

extension DatabaseID: @unchecked Sendable {}

/// Manage fluent databases and migrations
///
/// You can either create this separate from `HBApplication` or add it to your application
Expand Down Expand Up @@ -60,6 +58,10 @@ public struct HBFluent: Sendable, Service {

public func run() async throws {
await GracefulShutdownWaiter().wait()
try await self.shutdown()
}

public func shutdown() async throws {
self._databases.wrappedValue.shutdown()
}

Expand All @@ -74,18 +76,28 @@ public struct HBFluent: Sendable, Service {
)
}

/// Run migration if needed
/// Run migration if needed. If it fails then it will shutdown fluent
@MainActor
public func migrate() async throws {
try await self.migrator.setupIfNeeded().get()
try await self.migrator.prepareBatch().get()
do {
try await self.migrator.setupIfNeeded().get()
try await self.migrator.prepareBatch().get()
} catch {
try await self.shutdown()
throw error
}
}

/// Run revert if needed
/// Run revert if needed. If it fails then it will shutdown fluent
@MainActor
public func revert() async throws {
try await self.migrator.setupIfNeeded().get()
try await self.migrator.revertAllBatches().get()
do {
try await self.migrator.setupIfNeeded().get()
try await self.migrator.revertAllBatches().get()
} catch {
try await self.shutdown()
throw error
}
}

/// Return Database connection
Expand Down
19 changes: 15 additions & 4 deletions Tests/HummingbirdFluentTests/PersistTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
//
//===----------------------------------------------------------------------===//

import FluentPostgresDriver
import FluentSQLiteDriver
// import FluentMySQLDriver
// import FluentPostgresDriver
import Hummingbird
import HummingbirdFluent
import XCTest
Expand All @@ -25,8 +24,20 @@ final class PersistTests: XCTestCase {
logger.logLevel = .trace
let fluent = HBFluent(logger: logger)
// add sqlite database
fluent.databases.use(.sqlite(.memory), as: .sqlite)
// fluent.databases.use(.postgres(hostname: "localhost", username: "postgres", password: "vapor", database: "vapor"), as: .psql)
// fluent.databases.use(.sqlite(.memory), as: .sqlite)
fluent.databases.use(
.postgres(
configuration: .init(
hostname: "localhost",
port: 5432,
username: "hummingbird",
password: "hummingbird",
database: "hummingbird", tls: .disable
),
maxConnectionsPerEventLoop: 32
),
as: .psql
)
let persist = await HBFluentPersistDriver(fluent: fluent)
// run migrations
try await fluent.migrate()
Expand Down

0 comments on commit a7df20c

Please sign in to comment.