Skip to content

Commit

Permalink
Restore support for pre-Concurrency migrations (#603)
Browse files Browse the repository at this point in the history
Revert async-ifying the MigrationLog and EnumMetadata migrations, spend less time locked when getting the migrators list in Migrator, remove a couple of spurious Sendable annotations.
  • Loading branch information
gwynne authored Apr 27, 2024
1 parent c4a406d commit c4533db
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
10 changes: 5 additions & 5 deletions Sources/FluentKit/Enum/EnumMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ final class EnumMetadata: Model, @unchecked Sendable {
}
}

private struct EnumMetadataMigration: AsyncMigration {
func prepare(on database: any Database) async throws {
try await database.schema(EnumMetadata.schema)
private struct EnumMetadataMigration: Migration {
func prepare(on database: any Database) -> EventLoopFuture<Void> {
database.schema(EnumMetadata.schema)
.id()
.field("name", .string, .required)
.field("case", .string, .required)
Expand All @@ -37,7 +37,7 @@ private struct EnumMetadataMigration: AsyncMigration {
.create()
}

func revert(on database: any Database) async throws {
try await database.schema(EnumMetadata.schema).delete()
func revert(on database: any Database) -> EventLoopFuture<Void> {
database.schema(EnumMetadata.schema).delete()
}
}
12 changes: 6 additions & 6 deletions Sources/FluentKit/Migration/MigrationLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public final class MigrationLog: Model, @unchecked Sendable {
public static let schema = "_fluent_migrations"

public static var migration: any Migration {
return MigrationLogMigration()
MigrationLogMigration()
}

@ID(key: .id)
Expand Down Expand Up @@ -35,9 +35,9 @@ public final class MigrationLog: Model, @unchecked Sendable {
}
}

private struct MigrationLogMigration: AsyncMigration {
func prepare(on database: any Database) async throws {
try await database.schema(MigrationLog.schema)
private struct MigrationLogMigration: Migration {
func prepare(on database: any Database) -> EventLoopFuture<Void> {
database.schema(MigrationLog.schema)
.field(.id, .uuid, .identifier(auto: false))
.field("name", .string, .required)
.field("batch", .int, .required)
Expand All @@ -48,7 +48,7 @@ private struct MigrationLogMigration: AsyncMigration {
.create()
}

func revert(on database: any Database) async throws {
try await database.schema(MigrationLog.schema).delete()
func revert(on database: any Database) -> EventLoopFuture<Void> {
database.schema(MigrationLog.schema).delete()
}
}
12 changes: 6 additions & 6 deletions Sources/FluentKit/Migration/Migrator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,20 @@ public struct Migrator: Sendable {
private func migrators<Result>(
_ handler: (DatabaseMigrator) -> EventLoopFuture<Result>
) -> EventLoopFuture<[Result]> {
self.migrations.storage.withLockedValue { $0.map {
self.migrations.storage.withLockedValue { $0 }.map {
handler(.init(id: $0, database: self.databaseFactory($0), migrations: $1, migrationLogLevel: self.migrationLogLevel))
} }
}
.flatten(on: self.eventLoop)
}
}

private final class DatabaseMigrator: Sendable {
let migrations: [any Migration]
let database: any Database & Sendable
let database: any Database
let id: DatabaseID?
let migrationLogLevel: Logger.Level

init(id: DatabaseID?, database: any Database & Sendable, migrations: [any Migration], migrationLogLevel: Logger.Level) {
init(id: DatabaseID?, database: any Database, migrations: [any Migration], migrationLogLevel: Logger.Level) {
self.migrations = migrations
self.database = database
self.id = id
Expand Down Expand Up @@ -172,11 +172,11 @@ private final class DatabaseMigrator: Sendable {
}

func revertBatch(number: Int) -> EventLoopFuture<Void> {
self.preparedMigrations(batch: number).sequencedFlatMapEach(self.revert)
self.preparedMigrations(batch: number).sequencedFlatMapEach { self.revert($0) }
}

func revertAllBatches() -> EventLoopFuture<Void> {
self.preparedMigrations().sequencedFlatMapEach(self.revert)
self.preparedMigrations().sequencedFlatMapEach { self.revert($0) }
}

// MARK: Preview
Expand Down

0 comments on commit c4533db

Please sign in to comment.