Centralise migrations

This commit is contained in:
Balazs Perlaki-Horvath 2024-09-03 23:09:44 +02:00 committed by Kelson
parent 0bca3190fd
commit 94e552a8c6
5 changed files with 32 additions and 10 deletions

View File

@ -31,10 +31,7 @@ struct Kiwix: App {
UNUserNotificationCenter.current().delegate = appDelegate
// MARK: - migrations
if !ProcessInfo.processInfo.arguments.contains("testing") {
let migrations = MigrationService(migrations: [
Migrations.schemeToZIM(using: Database.shared.viewContext)
])
_ = migrations.migrateAll()
_ = MigrationService().migrateAll()
}
}

View File

@ -230,10 +230,7 @@ struct RootView: View {
}
// MARK: - migrations
if !ProcessInfo.processInfo.arguments.contains("testing") {
let migrations = MigrationService(migrations: [
Migrations.schemeToZIM(using: Database.shared.viewContext)
])
_ = migrations.migrateAll()
_ = MigrationService().migrateAll()
}
}
.withHostingWindow { [windowTracker] hostWindow in

View File

@ -16,8 +16,7 @@
import Foundation
import CoreData
enum Migrations {
extension Migrations {
/// Change the bookmarks articleURLs from "kiwix://..." to "zim://..."
/// - Parameter context: DataBase context
/// - Returns: Migration - general struct

View File

@ -39,6 +39,10 @@ struct Migration {
struct MigrationService {
let migrations: [Migration]
init(migrations: [Migration] = Migrations.all) {
self.migrations = migrations
}
func migrateAll(using userDefaults: UserDefaulting = UserDefaults.standard) -> Bool {
var allSucceeded = true
for migration in migrations where migration.migrate(userDefaults) == false {

View File

@ -0,0 +1,25 @@
// This file is part of Kiwix for iOS & macOS.
//
// Kiwix is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// any later version.
//
// Kiwix is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kiwix; If not, see https://www.gnu.org/licenses/.
import Foundation
enum Migrations {
/// A central place for all migrations
/// they will be executed in the order specified here
static let all: [Migration] = [
Self.schemeToZIM(using: Database.shared.viewContext)
]
}