Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Fixes #345.

Fixes a few migration reverts.

---------

Co-authored-by: Chall Fry <[email protected]>
  • Loading branch information
challfry and challf authored Jan 20, 2025
1 parent a20089c commit fc6f51a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
16 changes: 11 additions & 5 deletions Sources/swiftarr/Migrations/Test Data/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ struct CreateTestData: AsyncMigration {

func revert(on database: Database) async throws {
try await Twarrt.query(on: database).delete()
guard let category = try await Category.query(on: database).filter(\.$title == "Test 1").first() else {
guard let category = try await Category.query(on: database).filter(\.$title == "General").first() else {
throw Abort(.internalServerError, reason: "No category found.")
}
try await Forum.query(on: database).filter(\.$category.$id == category.requireID()).delete()
if let forum = try await Forum.query(on: database).filter(\.$category.$id == category.requireID()).filter(\.$title == "Say Hello Here")
.with(\.$posts).with(\.$readers.$pivots).with(\.$edits).first() {
try await forum.posts.delete(on: database)
try await forum.$readers.pivots.delete(on: database)
try await forum.edits.delete(on: database)
try await forum.delete(on: database)
}
}

// Makes a single tweet by admin.
Expand All @@ -29,13 +35,13 @@ struct CreateTestData: AsyncMigration {
try await twarrt.save(on: database)
}

// Creates a forum in Test 1, adds 6 posts to it
// Creates a forum in General, adds 6 posts to it
func createTestForumPosts(on database: Database) async throws {
guard let admin = try await User.query(on: database).filter(\.$username == "admin").first() else {
throw Abort(.internalServerError, reason: "Could not find admin user.")
}
guard let category = try await Category.query(on: database).filter(\.$title == "Egype").first() else {
throw Abort(.internalServerError, reason: "Test category doesn't exist;, can't make test posts.")
guard let category = try await Category.query(on: database).filter(\.$title == "General").first() else {
throw Abort(.internalServerError, reason: "General category doesn't exist;, can't make test posts.")
}
let thread = try Forum(title: "Say Hello Here", category: category, creatorID: admin.requireID())
try await thread.save(on: database)
Expand Down
2 changes: 1 addition & 1 deletion Sources/swiftarr/Models/ScheduleLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ struct CreateScheduleLogSchema: AsyncMigration {
}

func revert(on database: Database) async throws {
try await database.schema("profileedit").delete()
try await database.schema("schedulelog").delete()
}
}
6 changes: 6 additions & 0 deletions Sources/swiftarr/Models/StreamPhoto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,18 @@ struct CreateStreamPhotoSchema: AsyncMigration {
// Changes atEvent to be set to null when the associated event is deleted.
struct StreamPhotoSchemaV2: AsyncMigration {
func prepare(on database: Database) async throws {
try? await database.schema("streamphoto")
.deleteConstraint(name: "streamphoto_at_event_fkey")
.update()
try await database.schema("streamphoto")
.foreignKey("at_event", references: "event", "id", onDelete: .setNull)
.update()
}

func revert(on database: Database) async throws {
try? await database.schema("streamphoto")
.deleteConstraint(name: "fk:streamphoto.at_event+streamphoto.id")
.update()
try await database.schema("streamphoto")
.foreignKey("at_event", references: "event", "id", onDelete: .noAction)
.update()
Expand Down

0 comments on commit fc6f51a

Please sign in to comment.