2026-09-21 13:52:52 +02:00
|
|
|
import FluentKit
|
|
|
|
|
|
|
|
|
|
/// Creates the `events`, `event_shops`, and `owned_issues` tables, seeded with the demo cons and the shops trading at them.
|
|
|
|
|
struct CreateCommunity: AsyncMigration {
|
|
|
|
|
|
|
|
|
|
// MARK: Methods
|
|
|
|
|
|
|
|
|
|
func prepare(on database: Database) async throws {
|
|
|
|
|
try await database.schema(EventRecord.schema)
|
|
|
|
|
.id()
|
|
|
|
|
.field("slug", .string, .required)
|
|
|
|
|
.field("name", .string, .required)
|
|
|
|
|
.field("venue", .string, .required)
|
|
|
|
|
.field("starts_on", .datetime, .required)
|
|
|
|
|
.field("ends_on", .datetime, .required)
|
|
|
|
|
.unique(on: "slug")
|
|
|
|
|
.create()
|
|
|
|
|
try await database.schema(EventShopRecord.schema)
|
|
|
|
|
.id()
|
|
|
|
|
.field("event_id", .uuid, .required, .references(EventRecord.schema, "id", onDelete: .cascade))
|
|
|
|
|
.field("shop_id", .uuid, .required, .references(ShopRecord.schema, "id", onDelete: .cascade))
|
|
|
|
|
.unique(on: "event_id", "shop_id")
|
|
|
|
|
.create()
|
|
|
|
|
try await database.schema(OwnedIssueRecord.schema)
|
|
|
|
|
.id()
|
|
|
|
|
.field("user_id", .uuid, .required, .references(UserRecord.schema, "id", onDelete: .cascade))
|
|
|
|
|
.field("series_slug", .string, .required)
|
|
|
|
|
.field("issue", .int, .required)
|
|
|
|
|
.unique(on: "user_id", "series_slug", "issue")
|
|
|
|
|
.create()
|
|
|
|
|
|
2026-10-25 12:47:23 +01:00
|
|
|
for (event, shops) in Seed.events(after: .init()) {
|
2026-09-21 13:52:52 +02:00
|
|
|
let record = EventRecord(event)
|
|
|
|
|
|
|
|
|
|
try await record.save(on: database)
|
|
|
|
|
|
|
|
|
|
for shop in try await ShopRecord.query(on: database).filter(\.$slug ~~ shops).all() {
|
|
|
|
|
try await record.$shops.attach(shop, on: database)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func revert(on database: Database) async throws {
|
|
|
|
|
for schema in [OwnedIssueRecord.schema, EventShopRecord.schema, EventRecord.schema] {
|
|
|
|
|
try await database.schema(schema).delete()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|