Guides · updated
Deploying a CloudKit schema to Production without breaking sync
A CloudKit container has two schemas, Development and Production, and Production only grows. You can add record types and fields to it. You can never delete, rename, or retype one. TestFlight and App Store builds talk to Production, and Production won't create a type or field when a record arrives with one it doesn't know. So before you ship a build that saves anything new, deploy the schema from Development in CloudKit Console. Skip that, and every save of the new record fails on the server with Cannot create new type ... in production schema, while it works on the phone you built it on.
Pikos, a notes, tasks, and calendar app that syncs through CKSyncEngine, lives with this rule. What follows is the workflow it uses and the check it runs before a release.
What breaks if you don't deploy
A debug build run from Xcode uses the Development environment. There, saving a record with a type or field the schema doesn't have adds it for you, which Apple calls a just-in-time schema. Production doesn't do that. When Xcode uploads a build for TestFlight or the App Store, it configures the app to use Production, and an app uploaded that way can use only Production.
So the build that worked on your desk reaches testers, saves a record with a type Production has never seen, and the server refuses it. Developers report the error from a TestFlight build on Apple's forums as:
"Partial Failure" "Failed to modify some records"
"Invalid Arguments"
"Cannot create new type XXX in production schema"
A new field on a type Production already has fails the same way. In another thread, a developer posted:
"Invalid Arguments" (12/2006); server message =
"Cannot create or modify field 'CD_nameFirstChar'
in record 'CD_Charts' in production schema"
An Apple engineer replied: "Typically this means you never updated the production schema with CD_nameFirstChar."
Code 12 is CKError.Code.invalidArguments. With CKSyncEngine it reaches your delegate in the sentRecordZoneChanges event, as one entry per record in failedRecordSaves. The engine retries a short list of transient errors by itself: network failures, rate limits, a busy zone, an account that's briefly unavailable. Invalid arguments isn't on that list. Apple's advice for a failed save is to fix the cause and save the record again, and nothing you do on the device fixes a missing type. Until someone deploys the schema, that record never reaches iCloud, and the user's other devices never see it.
It's a partial failure, so records whose types Production already knows keep saving. Most of sync looks fine, and only the new thing goes missing.
What you can and can't change in Production
| Change | In Production |
|---|---|
| Add a record type | Yes, by deploying from Development |
| Add a field to a record type | Yes, by deploying from Development |
| Delete a record type | No |
| Delete a field | No |
| Change a field's type | No |
| Rename a record type or field | No. There's no rename, and the old name can't be removed |
| Create a type or field by saving a record | No, that only happens in Development |
| Copy records over from Development | No. A deploy copies record types, fields, and indexes, never records |
Under every row is one rule from Apple's schema documentation: a change that could lose data against the current Production schema, like removing a type or field that already exists, is invalid, and CloudKit makes no change at all.
That makes every name in Production permanent, so the cheapest time to get the shape right is before the first deploy. Pikos is changing its own shape for exactly this reason. Its sync uses thirteen record types, one per kind of thing it stores: pages, folders, tags, reminders, and so on. Before 1.0 it decided to send every encrypted record under a single record type, with the kind inside the encrypted body, so Apple can't count someone's reminders by looking at record types. After 1.0 that change would mean uploading everyone's data again under the new type. Before the real container's first deploy, it costs nothing.
After your first deploy, plan each change as an addition: a new field beside the old one, or a new record type. Older builds that are still installed keep writing the old shape, so keep reading it.
The safe workflow
1. Keep the schema in a file in your repo
CloudKit has a text schema language, and Apple's cktool command-line tool reads and writes it. Apple recommends downloading an existing schema rather than writing it by hand, since a mistake can make existing Development data inaccessible:
xcrun cktool export-schema \
--team-id "$TEAM_ID" \
--container-id iCloud.com.example.notes \
--environment development \
--output-file schema.ckdb
Schema commands need a management token, which you create in CloudKit Console's settings and store with xcrun cktool save-token --type management.
Pikos goes one step further. Its schema file is generated from the code that turns its data into CloudKit records, and a test fails when the checked-in file no longer matches what the code generates. A new field can't reach the sync code without reaching the file. One record type from that file, trimmed:
DEFINE SCHEMA
RECORD TYPE "asset" (
"___recordID" REFERENCE QUERYABLE,
"fmt" ENCRYPTED INT64,
"record" ENCRYPTED STRING,
"file" ASSET,
GRANT WRITE TO "_creator",
GRANT CREATE TO "_icloud",
GRANT READ TO "_world"
);
Watch the system fields, the ones that start with three underscores. Every record type has them whether the file lists them or not. A line like "___recordID" REFERENCE QUERYABLE is only there to keep an index on one. If a later version of the file stops mentioning it, CloudKit keeps the field and drops the index, and queries that relied on it slow down or fail.
2. Change the schema in Development, from the file
Edit the file, then apply it to Development:
xcrun cktool import-schema \
--team-id "$TEAM_ID" \
--container-id iCloud.com.example.notes \
--environment development \
--file schema.ckdb
import-schema only works against Development, and cktool has no command that deploys to Production. That step stays in the Console.
3. Start Development from Production before you deploy
Every debug build that saves a record can add a type or field to Development. If one of those strays is still there when you deploy, it goes into Production with everything else, permanently. Clear them first. xcrun cktool reset-schema reverts Development to the current Production definition and deletes every record in Development, the same as Reset Environment in the Console. Then import the file again. Do it on a container whose Development data you don't need.
4. Deploy in CloudKit Console, before the build goes to TestFlight
- Sign in to CloudKit Console and open CloudKit Database.
- Choose your container.
- Select Deploy Schema Changes.
- Read the list of pending changes, then click Deploy.
Read that list properly. Anything on it you didn't expect becomes part of Production for good once you click. Deploying needs permission to edit Production, which a team's administrator has.
Deploy before the TestFlight upload, not before the App Store release. TestFlight builds use Production too.
5. Run a build against Production yourself
A development build can use either environment. The com.apple.developer.icloud-container-environment entitlement says which, and takes Development or Production. When you export a build from Xcode for testing, you can choose which one it targets. Point a build at Production, sign in with your own Apple Account, and save one record of each new type and with each new field before any tester does.
Pikos adds one more layer. Its dogfood builds use a separate staging container. That container's Production is add-only as well, but a fresh staging container is cheap if it gets messy. The real container gets its first schema at 1.0, in its final shape.
Check before every release
Deploying is a click someone has to remember. Make the release fail when the build saves something Production doesn't have:
- Export Production's schema with
cktool export-schema --environment production, to a file in the build's working folder. - Compare it with the file in your repo. Every record type and field in your file has to exist in Production with the same type. Extra fields in Production are fine, since old ones never go away.
- Fail the release on anything missing, and name it:
Note.archivedAt (TIMESTAMP) is missing in Production.
Compare names and types, not text. CloudKit doesn't keep comments in an uploaded schema, and an exported schema can list its elements in a different order from yours, so a plain diff reports changes that aren't there.
This is the step Pikos is adding to its own release: export Production with cktool, compare it with the schema file its test keeps current, and stop the release on anything missing. In CI, the management token goes in as a secret.
Sync Bench's free schema check is that comparison as one command and a CI step, and it ships before the app. Join the waitlist to hear when it's ready.
Sources
- Apple, Deploying an iCloud Container's Schema
- Apple, Designing and Creating a CloudKit Database
- Apple, Integrating a Text-Based Schema into Your Workflow
- Apple, Inspecting and Editing an iCloud Container's Schema
- Apple, cktool
- Apple, Designing for CloudKit (archived)
- Apple, CKSyncEngine and CKSyncEngine.Event.SentRecordZoneChanges
- Apple, com.apple.developer.icloud-container-environment
- Apple Developer Forums, threads 110743 and 749729