parent
de5d39cc90
commit
11fb813734
@ -0,0 +1,85 @@ |
|||||||
|
// |
||||||
|
// Migration.swift |
||||||
|
// LeStorage |
||||||
|
// |
||||||
|
// Created by Laurent Morvillier on 07/02/2024. |
||||||
|
// |
||||||
|
|
||||||
|
import Foundation |
||||||
|
|
||||||
|
public protocol MigrationSource : Storable { |
||||||
|
associatedtype Destination : Storable |
||||||
|
func migrate() -> Destination |
||||||
|
} |
||||||
|
|
||||||
|
public protocol SomeMigration { |
||||||
|
func migrate(synchronized: Bool) throws |
||||||
|
var version: UInt { get } |
||||||
|
var resourceName: String { get } |
||||||
|
} |
||||||
|
|
||||||
|
public class Migration<S : MigrationSource, D : Storable> : SomeMigration where S.Destination == D { |
||||||
|
|
||||||
|
public var version: UInt |
||||||
|
|
||||||
|
public var resourceName: String { |
||||||
|
return S.resourceName() |
||||||
|
} |
||||||
|
|
||||||
|
public init(version: UInt) { |
||||||
|
self.version = version |
||||||
|
} |
||||||
|
|
||||||
|
public func migrate(synchronized: Bool) throws { |
||||||
|
try self._migrateMainCollection() |
||||||
|
if synchronized { |
||||||
|
try self._migrateApiCallsCollection() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fileprivate func _migrateMainCollection() throws { |
||||||
|
let jsonString = try FileUtils.readDocumentFile(fileName: S.fileName()) |
||||||
|
if let decoded: [S] = try jsonString.decodeArray() { |
||||||
|
|
||||||
|
let migratedObjects: [D] = decoded.map { $0.migrate() } |
||||||
|
|
||||||
|
let jsonString: String = try migratedObjects.jsonString() |
||||||
|
let _ = try FileUtils.writeToDocumentDirectory(content: jsonString, fileName: D.fileName()) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fileprivate func _migrateApiCallsCollection() throws { |
||||||
|
let jsonString = try FileUtils.readDocumentFile(fileName: ApiCall<S>.fileName()) |
||||||
|
if let apiCalls: [ApiCall<S>] = try jsonString.decodeArray() { |
||||||
|
|
||||||
|
let migratedCalls = try apiCalls.map { apiCall in |
||||||
|
if let source: S = try apiCall.body.decode() { |
||||||
|
let migrated: D = source.migrate() |
||||||
|
apiCall.body = try migrated.jsonString() |
||||||
|
} |
||||||
|
return apiCall |
||||||
|
} |
||||||
|
|
||||||
|
let jsonString = try migratedCalls.jsonString() |
||||||
|
let _ = try FileUtils.writeToDocumentDirectory(content: jsonString, fileName: ApiCall<D>.fileName()) |
||||||
|
|
||||||
|
Logger.log("Ended _migrateApiCallsCollection: \(jsonString)") |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
class MigrationHistory: ModelObject, Storable { |
||||||
|
|
||||||
|
public static func resourceName() -> String { "migration_history" } |
||||||
|
|
||||||
|
public var id: String = Store.randomId() |
||||||
|
public var version: UInt |
||||||
|
public var resourceName: String |
||||||
|
|
||||||
|
init(version: UInt, resourceName: String) { |
||||||
|
self.version = version |
||||||
|
self.resourceName = resourceName |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue