-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathGovernor.swift
More file actions
71 lines (59 loc) · 2.06 KB
/
Copy pathGovernor.swift
File metadata and controls
71 lines (59 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import FluentKit
import Foundation
import NIOCore
import XCTest
public final class Governor: Model, @unchecked Sendable {
public static let schema = "governors"
@ID(key: .id)
public var id: UUID?
@Field(key: "name")
public var name: String
@Parent(key: "planet_id")
public var planet: Planet
public init() {}
public init(id: IDValue? = nil, name: String) {
self.id = id
self.name = name
}
public init(id: IDValue? = nil, name: String, planetId: UUID) {
self.id = id
self.name = name
self.$planet.id = planetId
}
}
public struct GovernorMigration: Migration {
public func prepare(on database: any Database) -> EventLoopFuture<Void> {
database.schema(Governor.schema)
.field(.id, .uuid, .identifier(auto: false), .required)
.field("name", .string, .required)
.field("planet_id", .uuid, .required, .references("planets", "id"))
.unique(on: "planet_id")
.create()
}
public func revert(on database: any Database) -> EventLoopFuture<Void> {
database.schema(Governor.schema).delete()
}
}
public struct GovernorSeed: Migration {
public init() {}
public func prepare(on database: any Database) -> EventLoopFuture<Void> {
Planet.query(on: database).all().flatMap { planets in
.andAllSucceed(
planets.map { planet in
let governor: Governor?
switch planet.name {
case "Mars":
governor = .init(name: "John Doe")
case "Earth":
governor = .init(name: "Jane Doe")
default:
return database.eventLoop.makeSucceededVoidFuture()
}
return planet.$governor.create(governor!, on: database)
}, on: database.eventLoop)
}
}
public func revert(on database: any Database) -> EventLoopFuture<Void> {
Governor.query(on: database).delete()
}
}