Skip to content

Commit 6b16790

Browse files
Merge pull request #3325 from SwiftPackageIndex/convert-to-async-await
Convert migrations to async/await
2 parents 620b773 + c0125c0 commit 6b16790

File tree

63 files changed

+451
-491
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+451
-491
lines changed

Sources/App/Migrations/001/CreatePackage.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
import Fluent
1616

17-
struct CreatePackage: Migration {
18-
func prepare(on database: Database) -> EventLoopFuture<Void> {
19-
return database.schema("packages")
17+
struct CreatePackage: AsyncMigration {
18+
func prepare(on database: Database) async throws {
19+
try await database.schema("packages")
2020
// managed fields
2121
.id()
2222
.field("created_at", .datetime)
@@ -31,7 +31,7 @@ struct CreatePackage: Migration {
3131
.create()
3232
}
3333

34-
func revert(on database: Database) -> EventLoopFuture<Void> {
35-
return database.schema("packages").delete()
34+
func revert(on database: Database) async throws {
35+
try await database.schema("packages").delete()
3636
}
3737
}

Sources/App/Migrations/001/CreateProduct.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
import Fluent
1616

17-
struct CreateProduct: Migration {
18-
func prepare(on database: Database) -> EventLoopFuture<Void> {
19-
return database.schema("products")
17+
struct CreateProduct: AsyncMigration {
18+
func prepare(on database: Database) async throws {
19+
try await database.schema("products")
2020
// managed fields
2121
.id()
2222
.field("created_at", .datetime)
@@ -33,7 +33,7 @@ struct CreateProduct: Migration {
3333
.create()
3434
}
3535

36-
func revert(on database: Database) -> EventLoopFuture<Void> {
37-
return database.schema("products").delete()
36+
func revert(on database: Database) async throws {
37+
try await database.schema("products").delete()
3838
}
3939
}

Sources/App/Migrations/001/CreateRecentPackages.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import Fluent
1616
import SQLKit
1717

1818

19-
struct CreateRecentPackages: Migration {
20-
func prepare(on database: Database) -> EventLoopFuture<Void> {
19+
struct CreateRecentPackages: AsyncMigration {
20+
func prepare(on database: Database) async throws {
2121
guard let db = database as? SQLDatabase else {
2222
fatalError("Database must be an SQLDatabase ('as? SQLDatabase' must succeed)")
2323
}
24-
return db.raw(
24+
try await db.raw(
2525
"""
2626
-- v0
2727
CREATE MATERIALIZED VIEW recent_packages AS
@@ -39,10 +39,10 @@ struct CreateRecentPackages: Migration {
3939
).run()
4040
}
4141

42-
func revert(on database: Database) -> EventLoopFuture<Void> {
42+
func revert(on database: Database) async throws {
4343
guard let db = database as? SQLDatabase else {
4444
fatalError("Database must be an SQLDatabase ('as? SQLDatabase' must succeed)")
4545
}
46-
return db.raw("DROP MATERIALIZED VIEW recent_packages").run()
46+
try await db.raw("DROP MATERIALIZED VIEW recent_packages").run()
4747
}
4848
}

Sources/App/Migrations/001/CreateRecentReleases.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import Fluent
1616
import SQLKit
1717

1818

19-
struct CreateRecentReleases: Migration {
20-
func prepare(on database: Database) -> EventLoopFuture<Void> {
19+
struct CreateRecentReleases: AsyncMigration {
20+
func prepare(on database: Database) async throws {
2121
guard let db = database as? SQLDatabase else {
2222
fatalError("Database must be an SQLDatabase ('as? SQLDatabase' must succeed)")
2323
}
24-
return db.raw(
24+
try await db.raw(
2525
"""
2626
-- v0
2727
CREATE MATERIALIZED VIEW recent_releases AS
@@ -40,10 +40,10 @@ struct CreateRecentReleases: Migration {
4040
).run()
4141
}
4242

43-
func revert(on database: Database) -> EventLoopFuture<Void> {
43+
func revert(on database: Database) async throws {
4444
guard let db = database as? SQLDatabase else {
4545
fatalError("Database must be an SQLDatabase ('as? SQLDatabase' must succeed)")
4646
}
47-
return db.raw("DROP MATERIALIZED VIEW recent_releases").run()
47+
try await db.raw("DROP MATERIALIZED VIEW recent_releases").run()
4848
}
4949
}

Sources/App/Migrations/001/CreateRepository.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
import Fluent
1616

17-
struct CreateRepository: Migration {
18-
func prepare(on database: Database) -> EventLoopFuture<Void> {
19-
return database.schema("repositories")
17+
struct CreateRepository: AsyncMigration {
18+
func prepare(on database: Database) async throws {
19+
try await database.schema("repositories")
2020
// managed fields
2121
.id()
2222
.field("created_at", .datetime)
@@ -48,7 +48,7 @@ struct CreateRepository: Migration {
4848
.create()
4949
}
5050

51-
func revert(on database: Database) -> EventLoopFuture<Void> {
52-
return database.schema("repositories").delete()
51+
func revert(on database: Database) async throws {
52+
try await database.schema("repositories").delete()
5353
}
5454
}

Sources/App/Migrations/001/CreateSearch.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import Fluent
1616
import SQLKit
1717

1818

19-
struct CreateSearch: Migration {
20-
func prepare(on database: Database) -> EventLoopFuture<Void> {
19+
struct CreateSearch: AsyncMigration {
20+
func prepare(on database: Database) async throws {
2121
guard let db = database as? SQLDatabase else {
2222
fatalError("Database must be an SQLDatabase ('as? SQLDatabase' must succeed)")
2323
}
24-
return db.raw(
24+
try await db.raw(
2525
"""
2626
-- v1
2727
CREATE MATERIALIZED VIEW search AS
@@ -40,10 +40,10 @@ struct CreateSearch: Migration {
4040
).run()
4141
}
4242

43-
func revert(on database: Database) -> EventLoopFuture<Void> {
43+
func revert(on database: Database) async throws {
4444
guard let db = database as? SQLDatabase else {
4545
fatalError("Database must be an SQLDatabase ('as? SQLDatabase' must succeed)")
4646
}
47-
return db.raw("DROP MATERIALIZED VIEW search").run()
47+
try await db.raw("DROP MATERIALIZED VIEW search").run()
4848
}
4949
}

Sources/App/Migrations/001/CreateVersion.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
import Fluent
1616

17-
struct CreateVersion: Migration {
18-
func prepare(on database: Database) -> EventLoopFuture<Void> {
19-
return database.schema("versions")
17+
struct CreateVersion: AsyncMigration {
18+
func prepare(on database: Database) async throws {
19+
try await database.schema("versions")
2020
// managed fields
2121
.id()
2222
.field("created_at", .datetime)
@@ -37,7 +37,7 @@ struct CreateVersion: Migration {
3737
.create()
3838
}
3939

40-
func revert(on database: Database) -> EventLoopFuture<Void> {
41-
return database.schema("versions").delete()
40+
func revert(on database: Database) async throws {
41+
try await database.schema("versions").delete()
4242
}
4343
}

Sources/App/Migrations/002/CreateOwnerRepositoryIndex.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ import Fluent
1616
import SQLKit
1717

1818

19-
struct CreateOwnerRepositoryIndex: Migration {
20-
func prepare(on database: Database) -> EventLoopFuture<Void> {
19+
struct CreateOwnerRepositoryIndex: AsyncMigration {
20+
func prepare(on database: Database) async throws {
2121
guard let db = database as? SQLDatabase else {
2222
fatalError("Database must be an SQLDatabase ('as? SQLDatabase' must succeed)")
2323
}
24-
return db.raw(
24+
try await db.raw(
2525
"""
2626
CREATE UNIQUE INDEX idx_repositories_owner_name
2727
ON repositories (LOWER(owner), LOWER(name))
2828
"""
2929
).run()
3030
}
3131

32-
func revert(on database: Database) -> EventLoopFuture<Void> {
32+
func revert(on database: Database) async throws {
3333
guard let db = database as? SQLDatabase else {
3434
fatalError("Database must be an SQLDatabase ('as? SQLDatabase' must succeed)")
3535
}
36-
return db.raw("DROP INDEX idx_repositories_owner_name").run()
36+
try await db.raw("DROP INDEX idx_repositories_owner_name").run()
3737
}
3838
}

Sources/App/Migrations/002/CreateRepositoriesNameIndex.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,23 @@ import Fluent
1616
import SQLKit
1717

1818

19-
struct CreateRepositoriesNameIndex: Migration {
20-
func prepare(on database: Database) -> EventLoopFuture<Void> {
19+
struct CreateRepositoriesNameIndex: AsyncMigration {
20+
func prepare(on database: Database) async throws {
2121
guard let db = database as? SQLDatabase else {
2222
fatalError("Database must be an SQLDatabase ('as? SQLDatabase' must succeed)")
2323
}
2424

2525
// See https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/176#issuecomment-637710906
2626
// for details about this index
27-
return db.raw("CREATE EXTENSION IF NOT EXISTS pg_trgm").run()
28-
.flatMap {
29-
db.raw("CREATE INDEX idx_repositories_name ON repositories USING gin (name gin_trgm_ops)").run() }
27+
try await db.raw("CREATE EXTENSION IF NOT EXISTS pg_trgm").run()
28+
try await db.raw("CREATE INDEX idx_repositories_name ON repositories USING gin (name gin_trgm_ops)").run()
3029
}
3130

32-
func revert(on database: Database) -> EventLoopFuture<Void> {
31+
func revert(on database: Database) async throws {
3332
guard let db = database as? SQLDatabase else {
3433
fatalError("Database must be an SQLDatabase ('as? SQLDatabase' must succeed)")
3534
}
36-
return
37-
db.raw("DROP INDEX idx_repositories_name").run()
38-
.flatMap { db.raw("DROP EXTENSION pg_trgm").run() }
35+
try await db.raw("DROP INDEX idx_repositories_name").run()
36+
try await db.raw("DROP EXTENSION pg_trgm").run()
3937
}
4038
}

Sources/App/Migrations/003/UpdateRecentPackages1.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import Fluent
1616
import SQLKit
1717

1818

19-
struct UpdateRecentPackages1: Migration {
19+
struct UpdateRecentPackages1: AsyncMigration {
2020
let dropSQL: SQLQueryString = "DROP MATERIALIZED VIEW recent_packages"
2121

22-
func prepare(on database: Database) -> EventLoopFuture<Void> {
22+
func prepare(on database: Database) async throws {
2323
guard let db = database as? SQLDatabase else {
2424
fatalError("Database must be an SQLDatabase ('as? SQLDatabase' must succeed)")
2525
}
@@ -43,11 +43,11 @@ struct UpdateRecentPackages1: Migration {
4343
ORDER BY MAX(p.created_at) DESC
4444
LIMIT 100
4545
"""
46-
return db.raw(dropSQL).run()
47-
.flatMap { db.raw(updatedViewSQL).run() }
46+
try await db.raw(dropSQL).run()
47+
try await db.raw(updatedViewSQL).run()
4848
}
4949

50-
func revert(on database: Database) -> EventLoopFuture<Void> {
50+
func revert(on database: Database) async throws {
5151
guard let db = database as? SQLDatabase else {
5252
fatalError("Database must be an SQLDatabase ('as? SQLDatabase' must succeed)")
5353
}
@@ -66,7 +66,7 @@ struct UpdateRecentPackages1: Migration {
6666
ORDER BY MAX(p.created_at) DESC
6767
LIMIT 100
6868
"""
69-
return db.raw(dropSQL).run()
70-
.flatMap { db.raw(oldViewSQL).run() }
69+
try await db.raw(dropSQL).run()
70+
try await db.raw(oldViewSQL).run()
7171
}
7272
}

0 commit comments

Comments
 (0)