Moving Craft schema between environments, from Craft 2 to project config
On this page
I wrote last month about content migrations, the kind that change rows. This is about the other kind of change, the schema: sections, fields, entry types, plugin settings. Getting those from a laptop to staging to production has been a different problem in every era of Craft I have worked in, and the repos at the agency have the whole history in them.
Craft 2: nothing moved on its own
The hospital system site started in 2017 on Craft 2. The Craft application itself was committed into the repo under src/craft/app, because that is how Craft 2 was distributed. There was no project config and no migration mechanism for site schema. If you added a field, you added it in the control panel on your machine, then again on staging, then again on production, and you hoped the handles matched.
Two artifacts from that era say it better than I can. A sports nonprofit's repo from October 2017 has a commit titled "add db until I get dev up" whose only content is a zipped MySQL dump. That was the handoff. And a 2018 commit on the hospital site is titled "Use field handle instead of ID in case environment IDs differ":
->join('neoblocktypes nbt', 'nb.typeId = nbt.id')
->where(array('and',
- 'nb.typeId=76',
+ 'nbt.handle="supplementalBlock"',
Hard-coded id 76 was the block type on one environment. On the other it was something else, because someone had created the block types in a different order. Every integer id in Craft 2 was a landmine like that.
Project config, three attempts
Craft 3.1 shipped project config in January 2019. A teammate turned it on for a food brand's site the week it came out, with the commit "Setup project config and only allow site changes through it", meaning useProjectConfigFile => true and allowAdminChanges => false outside local. The next four days of history are nineteen commits named "Add project config", "Remove project config", "Remove project config temporarily", "Test project config", "Remove project config for now". That was the first-release experience.
The hospital site tried in August 2019. The commit added an 11,330-line project.yaml. Five weeks later another commit deleted it and set allowAdminChanges back to true everywhere. In November 2020 the config/project/ directory went into .gitignore, which is the "we use it locally but not for deploys" position. In February 2021 it came out of .gitignore for good, now as the directory of per-component YAML files that Craft 3.5 introduced, and in September 2021 the sandbox deploy gained a line, php craft project-config/apply. The deploy scripts now run php craft up, which runs pending migrations and then applies pending project config.
The lesson I took from those three tries was not "project config is flaky". It was that project config only works once the production database stops being the source of truth, and it took us three tries to mean that.
The ten-pull-request section move
The one I own is from July 2023. The hospital site had facility pages living as an entry type inside the general pages section, and they needed their own section. The change is two things that have to land together: YAML that adds the section and renames the old entry type to facilityPageDeprecated, and a content migration that queries the old entries and moves them.
$oldFacilityPages = Entry::find()
->section('pages')
->status(null)
->type('facilityPageDeprecated')
->all();
collect($oldFacilityPages)->map(fn ($entry) => $this->moveEntry($entry, 'facilityPage'));
The migration file is dated May 16. The pull request merged July 13. By then the entry type handle in the migration did not match the handle in the YAML, so the same afternoon there was a second PR called "fix type". Then a PR that changed one line, dateModified in project.yaml. Then the next day both of those were reverted, then the reverts were reverted, and eleven days later a final PR bumped dateModified again and fixed the handle in safeDown(). Ten pull requests for one section.
Two things were going on. The dateModified bumps are how you tell Craft the YAML is newer than what the database has, so project-config/apply reads it again; when a change has been applied by hand somewhere, or applied and reverted, that timestamp is the only lever you have. And craft up runs migrations before it applies project config, so a migration that depends on a section existing can run before the section does. I cannot reconstruct from git which of those bit on which environment. What I can say is that I now write the migration to tolerate the YAML not having landed yet, I run craft up on a copy of production before the PR goes up, and I never let a migration sit for two months.
What still breaks
This month, on my own Commerce site, I removed a mail plugin and deployed. craft up failed with "No plugin exists with the handle sendgrid". The plugin was gone from Composer, but the project config stored in the database still had a plugins.sendgrid key, and applying the YAML tried to reconcile it against a class that no longer existed. The site served 503 until I ran craft project-config/remove plugins.sendgrid and then craft project-config/apply on the server. Uninstall first, deploy second.
Two weeks later, on an older Craft site, I bumped dateModified to force a small YAML change through. The YAML in that repo had not been touched in a long time, and its schemaVersion values for Craft and a form plugin were behind what production was actually running. Applying it rolled the schema versions backwards, the deploy failed, and the site returned 500 until I bumped them. Before touching project.yaml on a repo that has sat still, compare its schema versions with production.
The rules
Production is not the source of truth. The YAML is, which means allowAdminChanges is false everywhere but local, and a change made by hand on production is a bug.
Deploy runs craft up. Nothing is applied by a person typing on the server, except the repair commands when a deploy has already failed.
A migration and the YAML it depends on ship in the same PR, and the migration assumes it might run first.
Remove plugins before you remove their package.
When you bump dateModified, read the rest of the file.