Lesson
6
Running a content migration
Use field validation to validate if the migration was successful. Back up the dataset for safe measure. Run the content migration and verify that it went well.
Log in to mark your progress for each Lesson and Task
Before diving into the migration, we want to do one more thing: make the Event format field required. The primary reason is that we always want to have information for this with an event, but it will also let us validate that the content migration has been successful later on.
Add the
rule.required()
validation rule to the Event format fieldschemaTypes/eventType.ts
defineField({ name: 'format', type: 'string', title: 'Event format', options: { list: ['in-person', 'virtual'], layout: 'radio', }, validation: (rule) => rule.required(),}),
If you save this change and run the sanity documents validate -y
command again, you should get a lot of errors like this on the event
document type:
Run
npx sanity@latest documents validate -y
Terminal output
ERROR event AUoLUkEDo6CVeRx5svBpXH└─ format ........................ ✖ Required
When you finish the content migration, this validation error should disappear. You are now ready to prepare the content migration to move the values over from the old to the new field in all the existing documents.
You are now ready to do a dry run to check how the migration script will affect your content in the dataset.
Dry run the content migration
npx sanity@latest migration run replace-event-type-with-event-format
If run successfully, this command should output a list of patches for each event document, looking like this:
patch event AUoLUkEDo6CVeRx5svBpBB └─ eventType ..................... unset()
patch event AUoLUkEDo6CVeRx5svBiyh └─ format ........................ setIfMissing("in-person")
It’s a good habit to export the dataset before running a content migration, especially if you do it on production content. This way, you have a backup that you can import if something goes wrong or you make an error (it happens to us all).
Export your production dataset with the Sanity CLI
npx sanity@latest dataset export production
This will export all your documents into a newline-delineated JSON file (.ndjson
) and assets in their folder as a production.tar.gz
file. If you want to “reset” your dataset to its state before the content migration, you can run the following command:
npx sanity@latest dataset import production.tar.gz production --replace
Note that the --replace
will overwrite documents with the same _id
.
With this backup, you can execute the content migration knowing that you have a way to restore the dataset if something goes wrong.
With the dataset backup in place and having verified that the content migration script outputs the changes we want, you are now ready to move a lot of data in one swoop.
Run the content migration with the
--no-dry-run
flagnpx sanity@latest migration run replace-event-type-with-event-format --no-dry-run
If everything went well, then your terminal should have an output like this:
Terminal output
❯ sanity migration run replace-event-type-with-event-format --no-dry-run? This migration will run on the production dataset in your-project-id project. Are you sure? Yes✔ Migration "replace-event-type-with-event-format" completed.
Project id: uxqfcg2d Dataset: production
179 documents processed. 179 mutations generated. 1 transactions committed.
This tells you that you have migrated 179 documents with 179 mutations, and it was all done with one transaction (that is one API request). In other words, it happened almost instantly for all these documents and anyone who had been in the Studio while you ran this.
Run the validation command again to see if there is content for the Event format field.
Run
npx sanity@latest documents validate -y
to confirm that the migration was successfulTerminal output
❯ sanity documents validate -y ✔ Loaded workspace 'default' using project 'uxqfcg2d' and dataset 'production' (1.8s)✔ Downloaded 457 documents (0.3s)✔ Checked all references (0.0s)✔ Validated 444 documents (3.4s)
Validation results:✔ Valid: 444 documents✖ Errors: 0 documents, 0 errors⚠ Warnings: 0 documents, 0 warnings
If you have zero errors, then the migration was successful!
You have 6 uncompleted tasks in this lesson
0 of 6