Ship & operate · Lesson 13 of 15 · 20 min
Change the model safely with environments and migrations
Add a field to Lesson with a script you can review in a pull request, test it in a dev environment, then apply it to master.
So far you’ve changed the content model by clicking. That works alone. On a team it breaks down fast: nobody knows who changed what, and one wrong click breaks the live site.
The fix is the same one you use for code. Make changes in a copy, write them down as a script, review the script, then run it on the real thing.
1. Install the CLI and log in
npm install -g contentful-cli
contentful login
login opens your browser and creates a Content Management token. This is the token that can write and delete. It’s stored on your computer, not in the project.
Point the CLI at your space:
contentful space use
2. Back up first
contentful space export --environment-id master
That writes one JSON file with your whole model and all content. Keep it out of git if your content is private.
3. Create a dev environment
contentful space environment create --name dev --environment-id dev --source master
dev is a full copy of master: same model, same entries. Changes in one don’t touch the other.
Give your API key access to it. In Settings → API keys, open your Learn Hub key, find the environments list, and tick dev.
4. Write a migration
You’ll add a list of learning objectives to every lesson.
migrations/01-add-objectives.cjs
module.exports = function (migration) {
const lesson = migration.editContentType('lesson');
lesson
.createField('objectives')
.name('Learning objectives')
.type('Array')
.items({ type: 'Symbol' });
lesson.moveField('objectives').afterField('summary');
};
The file ends in .cjs because your Astro project uses ES modules and the CLI loads migrations the older way.
Run it against dev:
contentful space migration --environment-id dev migrations/01-add-objectives.cjs
The CLI prints its plan and asks before doing anything. Say yes.
In the Contentful web app, switch to the dev environment using the environment menu. Lesson now has a Learning objectives field. Add two or three objectives to a lesson and publish it.
5. Update the code
Three small changes. The mapper:
src/lib/contentful-loader.ts
lesson: ({ fields }) => ({
title: fields.title,
summary: fields.summary,
minutes: fields.minutes,
objectives: fields.objectives,
author: fields.author?.sys.id,
}),
The schema:
src/content.config.ts
objectives: z.array(z.string()).default([]),
And the lesson page, above <Content />:
src/pages/lessons/[id].astro
{lesson.data.objectives.length > 0 && (
<section>
<h2>You'll learn</h2>
<ul>
{lesson.data.objectives.map((objective) => <li>{objective}</li>)}
</ul>
</section>
)}
Point your local site at dev:
.env
CONTENTFUL_ENVIRONMENT=dev
Restart npm run dev. The lesson shows its objectives. Lessons without any still work, because the schema defaults to an empty list.
6. Ship it in the right order
- Commit the migration and the code together, and open a pull request
- After review, run the same script against
master:
contentful space migration --environment-id master migrations/01-add-objectives.cjs
- Merge and deploy the code
- Remove
CONTENTFUL_ENVIRONMENTfrom.env
The order works because both halves are tolerant. The new field is optional, so old code ignores it. The new code defaults to an empty list, so it works before any lesson has objectives.
Making a field required later
Don’t add a required field in one step. Every existing entry would be missing it, and editors couldn’t republish anything until they filled it in. Do it in three steps:
| Step | What you do |
|---|---|
| Expand | Add the field as optional (what you just did) |
| Backfill | Fill it in on every existing entry |
| Contract | A second migration: lesson.editField('objectives').required(true) |
This “expand, backfill, contract” pattern is how database teams change a live schema without downtime. It’s the same problem.