commit 3053b7f8f359f6bbf35a2cde42beff0af4aa0d8f
parent b7b3d947c09568f8876b4c79df6f2ffad1e21c4f
Author: Michael Camilleri <[email protected]>
Date: Mon, 19 Oct 2026 19:11:11 +0900
Drop the narrative attributes from the Core Data model
The narrative, narrativeDraft and narrativeVersion attributes held the
entry the on-device model polished, and have been unused since entries
stopped being polished.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat
| M | Notes/Design.md | | | 7 | ++++++- |
| M | Reportage.xcodeproj/project.pbxproj | | | 4 | ++++ |
| M | Shared/Persistence/ReportageModel.xcdatamodeld/ReportageModel.xcdatamodel/contents | | | 3 | --- |
| A | Tests/Unit/SchemaMigrationTests.swift | | | 88 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 files changed, 98 insertions(+), 4 deletions(-)
diff --git a/Notes/Design.md b/Notes/Design.md
@@ -134,6 +134,12 @@ Persistence is Core Data with raw `CKSyncEngine`, as in Crossmate. It does not use `NSPersistentCloudKitContainer` or SwiftData. The local store is normalised for querying. CloudKit records are coarser (see [Sync](#sync)). +The model has a single version, edited in place. Lightweight migration is on, +and a SQLite store keeps a copy of the model it was made with, so a change +Core Data can infer (adding or removing an attribute or entity, or renaming +one with a renaming ID) migrates an existing store without the old model in +the app. A change that needs data rewritten would need a migration of its own. + ### Question | Field | Notes | @@ -199,7 +205,6 @@ question the user reworded gets the wrong phrasing. | `context` | Encoded `Context` value (below). | | `legacy` | For imported reports: the original Reporter snapshot JSON, compressed. This guarantees a lossless import without modelling every Reporter-specific field. | | `modifiedAt` | Used for last-writer-wins on sync. | -| `narrative`, `narrativeDraft`, `narrativeVersion` | Unused. They held the model-polished entry, which is gone (see [Entries](#entries)). Kept until the Core Data model is next versioned, because removing them without an earlier version to migrate from would stop existing stores opening. | ### Response
diff --git a/Reportage.xcodeproj/project.pbxproj b/Reportage.xcodeproj/project.pbxproj
@@ -18,6 +18,7 @@
22EA393B887791E20B6FE625 /* PlacePickerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1B6C4141A26EED99B1F03FBE /* PlacePickerTests.swift */; };
25BC9F95DC40E31B1E4C6FBA /* Token.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3BE62142CCC4A791F05163A /* Token.swift */; };
28D2CDAAB0777233D4465A83 /* ReporterArchiveTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 487F88C880B5DE69964C6A37 /* ReporterArchiveTests.swift */; };
+ 2D9BE70D44ECC3F3972DA466 /* SchemaMigrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E60426297403B9F8632343D8 /* SchemaMigrationTests.swift */; };
2EE68329C383FA1C5295ACA9 /* PlaceAnswerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9856D1F8123B9CAD7D39071D /* PlaceAnswerView.swift */; };
3175E324135E1CF5A73683D1 /* ReporterTimeZones.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7196D553D769071DB876B606 /* ReporterTimeZones.swift */; };
3244B8439B5E4F79FF84C156 /* JSONSlicer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 643C730A99F0B7BA8C1F8A8E /* JSONSlicer.swift */; };
@@ -170,6 +171,7 @@
D4AFF3D49ED9E8AC69390925 /* JSONSlicerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONSlicerTests.swift; sourceTree = "<group>"; };
DB87A3E922FA7AE98B93DF1E /* PromptSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PromptSettings.swift; sourceTree = "<group>"; };
E21F898FF60FC3A6AFCA06E9 /* Reportage Unit Tests.xctest */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = wrapper.cfbundle; path = "Reportage Unit Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
+ E60426297403B9F8632343D8 /* SchemaMigrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SchemaMigrationTests.swift; sourceTree = "<group>"; };
E7F1ECC4B803EFFD88FFF52B /* ReportageExport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReportageExport.swift; sourceTree = "<group>"; };
EC2D996880DB9C269F0203D5 /* AppleWeather.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppleWeather.swift; sourceTree = "<group>"; };
F498961A14F24AE6CC37453C /* ReportDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReportDetailView.swift; sourceTree = "<group>"; };
@@ -324,6 +326,7 @@
7FD562C0C92C1D59881C771E /* ReporterImportPlanTests.swift */,
84E55C093BA98D8700F406AE /* ReportStoreTests.swift */,
0734316A58943D5F2E26F718 /* ReportSummaryTests.swift */,
+ E60426297403B9F8632343D8 /* SchemaMigrationTests.swift */,
6E382405EE9DBEC8EE616C1D /* SurveyModelTests.swift */,
);
name = Unit;
@@ -548,6 +551,7 @@
28D2CDAAB0777233D4465A83 /* ReporterArchiveTests.swift in Sources */,
C8E02DEA2119E61D31667FFE /* ReporterImportPlanTests.swift in Sources */,
E68EF6274FD2F6D54D49F5BD /* ReporterImporterTests.swift in Sources */,
+ 2D9BE70D44ECC3F3972DA466 /* SchemaMigrationTests.swift in Sources */,
B00AC94E13BC9C602179953E /* SurveyModelTests.swift in Sources */,
E8CC84072697836F87D8A00D /* TestHelpers.swift in Sources */,
);
diff --git a/Shared/Persistence/ReportageModel.xcdatamodeld/ReportageModel.xcdatamodel/contents b/Shared/Persistence/ReportageModel.xcdatamodeld/ReportageModel.xcdatamodel/contents
@@ -24,9 +24,6 @@
<attribute name="kind" attributeType="String"/>
<attribute name="legacy" optional="YES" attributeType="Binary"/>
<attribute name="modifiedAt" attributeType="Date" usesScalarValueType="NO"/>
- <attribute name="narrative" optional="YES" attributeType="String"/>
- <attribute name="narrativeDraft" optional="YES" attributeType="String"/>
- <attribute name="narrativeVersion" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="promptedAt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="state" attributeType="String"/>
<attribute name="timeZone" attributeType="String"/>
diff --git a/Tests/Unit/SchemaMigrationTests.swift b/Tests/Unit/SchemaMigrationTests.swift
@@ -0,0 +1,88 @@
+import CoreData
+import Foundation
+import Testing
+
+@testable import Reportage
+
+/// The model has a single version. Changes that lightweight migration can
+/// infer still open existing stores, because a SQLite store keeps a copy of
+/// the model it was made with, which migration uses when the app no longer
+/// has that model.
+@Suite("Schema migration")
+struct SchemaMigrationTests {
+
+ private static var model: NSManagedObjectModel {
+ PersistenceController(inMemory: true).container.managedObjectModel
+ }
+
+ /// The model as it was while reports had narrative attributes. Its
+ /// entities use plain managed objects, so it doesn't claim the app's
+ /// entity classes.
+ private static func narrativeModel() -> NSManagedObjectModel {
+ let copy = model.copy() as! NSManagedObjectModel
+ for entity in copy.entities {
+ entity.managedObjectClassName = NSStringFromClass(NSManagedObject.self)
+ }
+ func attribute(_ name: String, _ type: NSAttributeDescription.AttributeType, optional: Bool)
+ -> NSAttributeDescription
+ {
+ let attribute = NSAttributeDescription()
+ attribute.name = name
+ attribute.type = type
+ attribute.isOptional = optional
+ return attribute
+ }
+ let version = attribute("narrativeVersion", .integer16, optional: false)
+ version.defaultValue = 0
+ copy.entitiesByName["ReportEntity"]?.properties += [
+ attribute("narrative", .string, optional: true),
+ attribute("narrativeDraft", .string, optional: true),
+ version,
+ ]
+ return copy
+ }
+
+ /// Makes a store with the narrative model holding one report. Everything
+ /// that opened it is released on return, so migration can replace it.
+ private static func makeNarrativeStore(at url: URL, reportID: UUID) throws {
+ let coordinator = NSPersistentStoreCoordinator(managedObjectModel: narrativeModel())
+ let store = try coordinator.addPersistentStore(type: .sqlite, at: url)
+ let context = NSManagedObjectContext(.privateQueue)
+ context.persistentStoreCoordinator = coordinator
+ try context.performAndWait {
+ let entity = try #require(coordinator.managedObjectModel.entitiesByName["ReportEntity"])
+ let report = NSManagedObject(entity: entity, insertInto: context)
+ report.setValuesForKeys([
+ "id": reportID, "date": Date(), "modifiedAt": Date(), "kind": "manual",
+ "state": "complete", "timeZone": "Asia/Tokyo", "trigger": "app",
+ "narrative": "Reading at the library.", "narrativeDraft": "Reading at Library.",
+ ])
+ try context.save()
+ context.reset()
+ }
+ try coordinator.remove(store)
+ }
+
+ @Test("The rebuilt narrative model matches the one stores were made with")
+ func narrativeModelHash() {
+ // ReportEntity's version hash in builds with the narrative attributes.
+ let hash = Data(base64Encoded: "N1hJI1x585Fi2rRj1X/VbR984fbK4ewKwfzcEPOUFKE=")
+ #expect(Self.narrativeModel().entityVersionHashesByName["ReportEntity"] == hash)
+ }
+
+ @Test("A store made with the narrative attributes opens with its reports")
+ func removedAttributes() throws {
+ let directory = URL.temporaryDirectory.appending(path: UUID().uuidString)
+ try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
+ defer { try? FileManager.default.removeItem(at: directory) }
+ let storeURL = directory.appending(path: "Store.sqlite")
+ let id = UUID()
+ try Self.makeNarrativeStore(at: storeURL, reportID: id)
+
+ let persistence = PersistenceController(storeURL: storeURL)
+ let reports = try persistence.viewContext.fetch(ReportEntity.fetchRequest())
+ #expect(reports.map(\.id) == [id])
+ let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStore(type: .sqlite, at: storeURL)
+ #expect(Self.model.isConfiguration(withName: nil, compatibleWithStoreMetadata: metadata))
+ }
+}