/
Migrations.swift
275 lines (253 loc) · 11 KB
/
Migrations.swift
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2024 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Logging
@_spi(ConnectionPool) import PostgresNIO
/// Database migration support
public actor PostgresMigrations {
enum State {
case waiting([CheckedContinuation<Void, Error>])
case completed
case failed(Error)
}
var migrations: [PostgresMigration]
var reverts: [String: PostgresMigration]
var state: State
/// Initialize a PostgresMigrations object
public init() {
self.migrations = []
self.reverts = [:]
self.state = .waiting([])
}
/// Add migration to list of migrations to be be applied
/// - Parameter migration: Migration to be applied
public func add(_ migration: PostgresMigration) {
self.migrations.append(migration)
}
/// Add migration to list of reverts, that can be applied
/// - Parameter migration: Migration to be reverted if necessary
public func revert(_ migration: PostgresMigration) {
self.reverts[migration.name] = migration
}
/// Apply database migrations
///
/// This function compares the list of applied migrations and the list of desired migrations. If there
/// are migrations in the applied list that don't exist in the desired list then every migration after
/// the missing migration is reverted. Then every unapplied migration from the desired list is applied.
///
/// This means removing a single migration from the desired list will revert every migration after the
/// removed migation, changing the order will revert the moved migrations and any migration after.
///
/// As migrating can be a destructive process it is best to run this with `dryRun`` set to true by default
/// and only run it properly if an error is thrown to indicate a migration is required. But check the list
/// of reported migrations and reverts before doing this though.
///
/// - Parameters:
/// - client: Postgres client
/// - logger: Logger to use
/// - dryRun: Should migrations actually be applied, or should we just report what would be applied and reverted
@_spi(ConnectionPool)
public func apply(client: PostgresClient, groups: [MigrationGroup] = [], logger: Logger, dryRun: Bool) async throws {
try await self.migrate(client: client, migrations: self.migrations, groups: groups, logger: logger, completeMigrations: true, dryRun: dryRun)
}
/// Revery database migrations
/// - Parameters:
/// - client: Postgres client
/// - logger: Logger to use
/// - dryRun: Should migrations actually be reverted, or should we just report what would be reverted
@_spi(ConnectionPool)
public func revert(client: PostgresClient, groups: [MigrationGroup] = [], logger: Logger, dryRun: Bool) async throws {
try await self.migrate(client: client, migrations: [], groups: groups, logger: logger, completeMigrations: false, dryRun: dryRun)
}
private func migrate(
client: PostgresClient,
migrations: [PostgresMigration],
groups: [MigrationGroup],
logger: Logger,
completeMigrations: Bool,
dryRun: Bool
) async throws {
switch self.state {
case .completed, .failed:
self.state = .waiting([])
case .waiting:
break
}
let repository = PostgresMigrationRepository(client: client)
do {
_ = try await repository.withContext(logger: logger) { context in
// setup migration repository (create table)
try await repository.setup(context: context)
var requiresChanges = false
// get migrations currently applied in the order they were applied
let appliedMigrations = try await repository.getAll(context: context)
// if groups array passed in is empty then work out list of migration groups by combining
// list of groups from migrations and applied migrations
let groups = groups.count == 0
? (migrations.map(\.group) + appliedMigrations.map(\.group)).uniqueElements
: groups
// for each group apply/revert migrations
for group in groups {
let groupMigrations = migrations.filter { $0.group == group }
let appliedGroupMigrations = appliedMigrations.filter { $0.group == group }
let minMigrationCount = min(groupMigrations.count, appliedGroupMigrations.count)
var i = 0
while i < minMigrationCount, appliedGroupMigrations[i].name == groupMigrations[i].name {
i += 1
}
// Revert deleted migrations, and any migrations after a deleted migration
for j in (i..<appliedGroupMigrations.count).reversed() {
let migrationName = appliedGroupMigrations[j].name
// look for migration to revert in migration list and revert dictionary. NB we are looking in the migration
// array belonging to the type, not the one supplied to the function
guard let migration = self.migrations.first(where: { $0.name == migrationName }) ?? self.reverts[migrationName] else {
throw PostgresMigrationError.cannotRevertMigration
}
logger.info("Reverting \(migration.name) from group \(group.name) \(dryRun ? " (dry run)" : "")")
if !dryRun {
try await migration.revert(connection: context.connection, logger: context.logger)
try await repository.remove(migration, context: context)
} else {
requiresChanges = true
}
}
// Apply migration
for j in i..<groupMigrations.count {
let migration = groupMigrations[j]
logger.info("Migrating \(migration.name) from group \(group.name) \(dryRun ? " (dry run)" : "")")
if !dryRun {
try await migration.apply(connection: context.connection, logger: context.logger)
try await repository.add(migration, context: context)
} else {
requiresChanges = true
}
}
}
// if changes are required
guard requiresChanges == false else {
throw PostgresMigrationError.requiresChanges
}
}
} catch {
self.setFailed(error)
throw error
}
if completeMigrations {
self.setCompleted()
}
}
/// Report if the migration process has completed
public func waitUntilCompleted() async throws {
switch self.state {
case .waiting(var continuations):
return try await withCheckedThrowingContinuation { cont in
continuations.append(cont)
self.state = .waiting(continuations)
}
case .completed:
return
case .failed(let error):
throw error
}
}
func setCompleted() {
switch self.state {
case .waiting(let continuations):
for cont in continuations {
cont.resume()
}
self.state = .completed
case .completed:
break
case .failed:
preconditionFailure("Cannot set it has completed after having set it has failed")
}
}
func setFailed(_ error: Error) {
switch self.state {
case .waiting(let continuations):
for cont in continuations {
cont.resume(throwing: error)
}
self.state = .failed(error)
case .completed:
preconditionFailure("Cannot set it has failed after having set it has completed")
case .failed(let error):
self.state = .failed(error)
}
}
}
/// Create, remove and list migrations
struct PostgresMigrationRepository {
struct Context {
let connection: PostgresConnection
let logger: Logger
}
let client: PostgresClient
func withContext<Value>(logger: Logger, _ process: (Context) async throws -> Value) async throws -> Value {
try await self.client.withConnection { connection in
try await process(.init(connection: connection, logger: logger))
}
}
func setup(context: Context) async throws {
try await self.createMigrationsTable(connection: context.connection, logger: context.logger)
}
func add(_ migration: PostgresMigration, context: Context) async throws {
try await context.connection.query(
"INSERT INTO _hb_migrations (\"name\", \"group\") VALUES (\(migration.name), \(migration.group.name))",
logger: context.logger
)
}
func remove(_ migration: PostgresMigration, context: Context) async throws {
try await context.connection.query(
"DELETE FROM _hb_migrations WHERE name = \(migration.name)",
logger: context.logger
)
}
func getAll(context: Context) async throws -> [(name: String, group: MigrationGroup)] {
let stream = try await context.connection.query(
"SELECT \"name\", \"group\" FROM _hb_migrations ORDER BY \"order\"",
logger: context.logger
)
var result: [(String, MigrationGroup)] = []
for try await (name, group) in stream.decode((String, String).self, context: .default) {
result.append((name, .init(group)))
}
return result
}
private func createMigrationsTable(connection: PostgresConnection, logger: Logger) async throws {
try await connection.query(
"""
CREATE TABLE IF NOT EXISTS _hb_migrations (
"order" SERIAL PRIMARY KEY,
"name" text,
"group" text
)
""",
logger: logger
)
}
}
extension Array where Element: Equatable {
/// The list of unique elements in the list, in the order they are found
var uniqueElements: [Element] {
self.reduce([]) { result, name in
if result.first(where: { $0 == name }) == nil {
var result = result
result.append(name)
return result
}
return result
}
}
}