2022-03-31 02:47:09 +02:00
|
|
|
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import GRDB
|
|
|
|
|
|
|
|
public protocol Migration {
|
2022-05-31 09:41:02 +02:00
|
|
|
static var target: TargetMigrations.Identifier { get }
|
2022-03-31 02:47:09 +02:00
|
|
|
static var identifier: String { get }
|
2022-05-30 05:04:26 +02:00
|
|
|
static var needsConfigSync: Bool { get }
|
|
|
|
static var minExpectedRunDuration: TimeInterval { get }
|
2022-03-31 02:47:09 +02:00
|
|
|
|
|
|
|
static func migrate(_ db: Database) throws
|
|
|
|
}
|
2022-05-31 09:41:02 +02:00
|
|
|
|
|
|
|
public extension Migration {
|
2023-06-19 10:19:47 +02:00
|
|
|
static func loggedMigrate(
|
|
|
|
_ storage: Storage?,
|
|
|
|
targetIdentifier: TargetMigrations.Identifier
|
|
|
|
) -> ((_ db: Database) throws -> ()) {
|
2022-05-31 09:41:02 +02:00
|
|
|
return { (db: Database) in
|
2022-12-06 06:59:14 +01:00
|
|
|
SNLogNotTests("[Migration Info] Starting \(targetIdentifier.key(with: self))")
|
2023-06-19 10:19:47 +02:00
|
|
|
storage?.internalCurrentlyRunningMigration.mutate { $0 = (targetIdentifier, self) }
|
|
|
|
defer { storage?.internalCurrentlyRunningMigration.mutate { $0 = nil } }
|
|
|
|
|
|
|
|
try migrate(db)
|
2022-12-06 06:59:14 +01:00
|
|
|
SNLogNotTests("[Migration Info] Completed \(targetIdentifier.key(with: self))")
|
2022-05-31 09:41:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|