-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from hsharghi/async
add Async methods
- Loading branch information
Showing
12 changed files
with
915 additions
and
364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,68 @@ | ||
import Fluent | ||
import SQLKit | ||
|
||
public struct CreateWallet<M:HasWallet>: Migration { | ||
public struct CreateWallet: Migration { | ||
private var idKey: String | ||
public init(foreignKeyColumnName idKey: String = "id") { | ||
self.idKey = idKey | ||
} | ||
|
||
public func prepare(on database: Database) -> EventLoopFuture<Void> { | ||
return database.schema(Wallet.schema) | ||
.id() | ||
.field("name", .string, .required) | ||
.field("owner_id", .uuid, .required, .references(M.schema, .init(stringLiteral: self.idKey), onDelete: .cascade)) | ||
.field("owner_type", .string, .required) | ||
.field("owner_id", .uuid, .required) | ||
.field("min_allowed_balance", .int, .required) | ||
.field("balance", .int, .required) | ||
.field("decimal_places", .uint8, .required) | ||
.field("created_at", .datetime, .required) | ||
.field("updated_at", .datetime, .required) | ||
.field("deleted_at", .datetime) | ||
.create() | ||
.create().flatMap { _ in | ||
let sqlDB = (database as! SQLDatabase) | ||
return sqlDB | ||
.create(index: "type_idx") | ||
.on(Wallet.schema) | ||
.column("owner_type") | ||
.run() | ||
} | ||
} | ||
|
||
public func revert(on database: Database) -> EventLoopFuture<Void> { | ||
return database.schema(Wallet.schema).delete() | ||
} | ||
} | ||
|
||
public struct CreateWalletAsync: AsyncMigration { | ||
private var idKey: String | ||
public init(foreignKeyColumnName idKey: String = "id") { | ||
self.idKey = idKey | ||
} | ||
|
||
public func prepare(on database: Database) async throws { | ||
try await database.schema(Wallet.schema) | ||
.id() | ||
.field("name", .string, .required) | ||
.field("owner_type", .string, .required) | ||
.field("owner_id", .uuid, .required) | ||
.field("min_allowed_balance", .int, .required) | ||
.field("balance", .int, .required) | ||
.field("decimal_places", .uint8, .required) | ||
.field("created_at", .datetime, .required) | ||
.field("updated_at", .datetime, .required) | ||
.field("deleted_at", .datetime) | ||
.create() | ||
let sqlDB = (database as! SQLDatabase) | ||
try await sqlDB | ||
.create(index: "type_idx") | ||
.on(Wallet.schema) | ||
.column("owner_type") | ||
.run() | ||
} | ||
|
||
|
||
public func revert(on database: Database) async throws { | ||
try await database.schema(Wallet.schema).delete() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.