2024-10-03 16:41:27 +00:00
|
|
|
//
|
2024-11-14 10:02:26 +00:00
|
|
|
// ProfileV2MigrationStrategy.swift
|
2024-10-03 16:41:27 +00:00
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 10/1/24.
|
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout 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
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout 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 Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
2024-11-12 15:42:19 +00:00
|
|
|
import CommonLibrary
|
2024-11-02 09:11:59 +00:00
|
|
|
import CommonUtils
|
2024-10-03 16:41:27 +00:00
|
|
|
import Foundation
|
|
|
|
import PassepartoutKit
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
public final class ProfileV2MigrationStrategy: ProfileMigrationStrategy, Sendable {
|
2024-10-03 16:41:27 +00:00
|
|
|
private let profilesRepository: CDProfileRepositoryV2
|
|
|
|
|
2024-11-12 15:42:19 +00:00
|
|
|
private let cloudKitIdentifier: String?
|
2024-10-03 16:41:27 +00:00
|
|
|
|
|
|
|
public init(
|
2024-11-12 15:42:19 +00:00
|
|
|
coreDataLogger: CoreDataPersistentStoreLogger?,
|
2024-10-03 16:41:27 +00:00
|
|
|
profilesContainerName: String,
|
2024-11-12 15:42:19 +00:00
|
|
|
baseURL: URL? = nil,
|
|
|
|
cloudKitIdentifier: String?
|
2024-10-03 16:41:27 +00:00
|
|
|
) {
|
|
|
|
let store = CoreDataPersistentStore(
|
|
|
|
logger: coreDataLogger,
|
|
|
|
containerName: profilesContainerName,
|
2024-11-12 15:42:19 +00:00
|
|
|
baseURL: baseURL,
|
2024-10-03 16:41:27 +00:00
|
|
|
model: CDProfileRepositoryV2.model,
|
|
|
|
cloudKitIdentifier: cloudKitIdentifier,
|
|
|
|
author: nil
|
|
|
|
)
|
|
|
|
profilesRepository = CDProfileRepositoryV2(context: store.context)
|
|
|
|
self.cloudKitIdentifier = cloudKitIdentifier
|
|
|
|
}
|
2024-11-12 15:42:19 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
// MARK: - ProfileMigrationStrategy
|
2024-11-12 15:42:19 +00:00
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
extension ProfileV2MigrationStrategy {
|
2024-11-12 15:42:19 +00:00
|
|
|
public func fetchMigratableProfiles() async throws -> [MigratableProfile] {
|
|
|
|
try await profilesRepository.migratableProfiles()
|
|
|
|
}
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
public func fetchProfile(withId profileId: UUID) async throws -> Profile? {
|
2024-11-12 15:42:19 +00:00
|
|
|
let mapper = MapperV2()
|
2024-11-14 10:02:26 +00:00
|
|
|
do {
|
|
|
|
guard let profile = try await profilesRepository.profile(withId: profileId) else {
|
|
|
|
return nil
|
2024-11-12 15:42:19 +00:00
|
|
|
}
|
2024-11-14 10:02:26 +00:00
|
|
|
return try mapper.toProfileV3(profile)
|
|
|
|
} catch {
|
|
|
|
pp_log(.App.migration, .error, "Unable to migrate profile \(profileId): \(error)")
|
|
|
|
return nil
|
2024-11-12 15:42:19 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-16 11:29:03 +00:00
|
|
|
|
|
|
|
public func deleteProfiles(withIds profileIds: Set<UUID>) async throws {
|
|
|
|
try await profilesRepository.deleteProfiles(withIds: profileIds)
|
|
|
|
}
|
2024-11-12 15:42:19 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
// MARK: - Internal
|
2024-10-03 16:41:27 +00:00
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
extension ProfileV2MigrationStrategy {
|
2024-11-12 15:42:19 +00:00
|
|
|
func fetchProfilesV2() async throws -> [ProfileV2] {
|
2024-11-14 10:02:26 +00:00
|
|
|
try await profilesRepository.profiles(withIds: nil)
|
2024-10-03 16:41:27 +00:00
|
|
|
}
|
|
|
|
}
|