|
| 1 | +import protocol SQLKit.SQLDatabase |
| 2 | +import protocol FluentKit.Database |
| 3 | +import protocol FluentKit.AsyncMigration |
| 4 | + |
| 5 | +/// A variant of `AsyncMigration` designed to simplify using SQLKit to write migrations. |
| 6 | +/// |
| 7 | +/// > Warning: Use of ``AsyncSQLMigration`` will cause runtime errors if the migration is added to a Fluent |
| 8 | +/// > database which is not compatible with SQLKit (such as MongoDB). |
| 9 | +public protocol AsyncSQLMigration: AsyncMigration { |
| 10 | + /// Perform the desired migration. |
| 11 | + /// |
| 12 | + /// - Parameter database: The database to migrate. |
| 13 | + func prepare(on database: any SQLDatabase) async throws |
| 14 | + |
| 15 | + /// Reverse, if possible, the migration performed by `prepare(on:)`. |
| 16 | + /// |
| 17 | + /// It is not uncommon for a given migration to be lossy if run in reverse, or to be irreversible in the |
| 18 | + /// entire. While it is recommended that such a migration throw an error (thus stopping any further progression |
| 19 | + /// of the revert operation), there is no requirement that it do so. In practice, most irreversible migrations |
| 20 | + /// choose to simply do nothing at all in this method. Implementors should carefully consider the consequences |
| 21 | + /// of progressively older migrations attempting to revert themselves afterwards before leaving this method blank. |
| 22 | + /// |
| 23 | + /// - Parameter database: The database to revert. |
| 24 | + func revert(on database: any SQLDatabase) async throws |
| 25 | +} |
| 26 | + |
| 27 | +extension AsyncSQLMigration { |
| 28 | + // See `AsyncMigration.prepare(on:)`. |
| 29 | + public func prepare(on database: any Database) async throws { |
| 30 | + try await self.prepare(on: database as! any SQLDatabase) |
| 31 | + } |
| 32 | + |
| 33 | + // See `AsyncMigration.revert(on:)`. |
| 34 | + public func revert(on database: any Database) async throws { |
| 35 | + try await self.revert(on: database as! any SQLDatabase) |
| 36 | + } |
| 37 | +} |
0 commit comments