Skip to content

Commit

Permalink
Add schema migration
Browse files Browse the repository at this point in the history
  • Loading branch information
allenhumphreys committed Jul 2, 2023
1 parent ace3adb commit 9faf1e6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
8 changes: 6 additions & 2 deletions Packages/ConfCore/ConfCore/AppleAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import Siesta

// MARK: - Initialization and configuration

public final class AppleAPIClient {
public final class AppleAPIClient: Logging, Signposting {
public static let log = makeLogger()
public static let signposter = makeSignposter()

fileprivate var environment: Environment
fileprivate var service: Service
Expand Down Expand Up @@ -67,7 +69,9 @@ public final class AppleAPIClient {
}

service.configureTransformer(environment.sessionsPath) { (entity: Entity<Data>) throws -> ContentsResponse? in
return try decoder.decode(ContentsResponse.self, from: entity.content)
try Self.signposter.withIntervalSignpost("decode contents", id: Self.signposter.makeSignpostID()) {
try decoder.decode(ContentsResponse.self, from: entity.content)
}
}

service.configureTransformer(environment.liveVideosPath) { (entity: Entity<Data>) throws -> [SessionAsset]? in
Expand Down
5 changes: 4 additions & 1 deletion Packages/ConfCore/ConfCore/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ public final class Storage: Logging, Signposting {
let sessions = backgroundRealm.objects(Session.self).filter("eventIdentifier == %@", event.identifier)

event.sessionInstances.removeAll()
event.sessionInstances.append(objectsIn: instances)
event.sessionInstances.forEach {
$0.session?.eventStartDate = event.startDate
event.sessionInstances.append($0)
}

event.sessions.removeAll()
sessions.forEach {
Expand Down
54 changes: 53 additions & 1 deletion Packages/ConfCore/ConfCore/StorageMigrator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
//

import Foundation
import class RealmSwift.List
import class RealmSwift.Migration
import class RealmSwift.MigrationObject
import OSLog

final class StorageMigrator: Logging {
Expand All @@ -31,7 +33,8 @@ final class StorageMigrator: Logging {
44: removeInvalidLiveAssets,
57: resetFeaturedSections,
59: resetTracks,
60: resetSessionInstances
60: resetSessionInstances,
61: addEventAndTrackInfoToSessionsForFasterSorting
]

init(migration: Migration, oldVersion: UInt64) {
Expand Down Expand Up @@ -191,4 +194,53 @@ final class StorageMigrator: Logging {
migration.deleteData(forType: "SessionInstance")
}

private static func addEventAndTrackInfoToSessionsForFasterSorting(with migration: Migration, oldVersion: SchemaVersion, log: Logger) {
log.info(#function)

// Logic mirrors the values applied in Storage.store(contentResult:)
migration.enumerateObjects(ofType: "Event") { _, event in
guard let event, let startDate = event.startDate as? Date else {
fatalError("Corrupt database during migration: Event.startDate must be a Date")
}

guard let sessions = event.sessions as? List<MigrationObject> else {
fatalError("Corrupt database during migration: Event.sessions must be a List")
}

sessions.forEach { session in
session.eventStartDate = startDate
}

guard let instances = event.sessionInstances as? List<MigrationObject> else {
fatalError("Corrupt database during migration: Event.sessionInstances must be a List")
}

instances.forEach { instance in
(instance.session as? MigrationObject)?.eventStartDate = startDate
}
}

migration.enumerateObjects(ofType: "Track") { _, track in
guard let track, let trackOrder = track.order as? Int else {
fatalError("Corrupt database during migration: Track.order must be an Int")
}

guard let sessions = track.sessions as? List<MigrationObject> else {
fatalError("Corrupt database during migration: Track.sessions must be a List")
}

sessions.forEach { session in
session.trackOrder = trackOrder
}

guard let instances = track.instances as? List<MigrationObject> else {
fatalError("Corrupt database during migration: Track.instances must be a List")
}

instances.forEach { instance in
(instance.session as? MigrationObject)?.trackOrder = trackOrder
}
}
}

}
2 changes: 1 addition & 1 deletion WWDC/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

struct Constants {

static let coreSchemaVersion: UInt64 = 60
static let coreSchemaVersion: UInt64 = 61

static let thumbnailHeight: CGFloat = 150

Expand Down

0 comments on commit 9faf1e6

Please sign in to comment.