Once we've done writing and testing our migrations, we need to apply them to the primary environment.
The first solution that comes to mind would be to simply promote as primary the sandbox environment we were using to test our migrations, but this can be a very dangerous operation, as the primary environment might have diverged from your sandbox environment. That is, some users or webhooks you've set up might have made changes to either the content or the schema of the primary environment after the fork.
The safe workflow that DatoCMS suggests applying migrations to the primary environment is instead the following:
The first thing to do is turn on Maintenance mode, so that during the process, no one can write new data on the primary environment.
You can do so either from the DatoCMS interface:
Or using the CLI:
$ npx datocms maintenance:onActivating maintenance mode... done
If some users are in the process of editing any record when you launch the command, DatoCMS will warn you and fail the execution of the command. You can force the activation using the
$ npx datocms maintenance:onActivating maintenance mode... !› Error: Cannot activate maintenance mode as some users are currently editing records› Try this: To proceed anyway, use the --force flag
You can now call the
datocms migrations:run command to make a new copy of the primary environment, and run all the pending migrations:
$ npx datocms migrations:run --destination=new-mainMigrations will be run in "new-main" sandbox environmentCreating a fork of "main" environment called "new-main"... doneCreating "schema_migration" model... doneRunning migration "1653061497_createArticleModel.js"... doneRunning migration "1653062813_addAuthors.js"... doneSuccessfully run 2 migration scripts
Before promoting the new sandbox as primary, make sure that your website/app is correctly working. All of our integrations offer a way to point to a sandbox instead of of the primary environment.
As an example, if you're using our GraphQL Content Delivery API, you can explicitly read data from a specific environment using one of the following endpoints instead of the regular ones:
Once everything is ready, you can safely promote the sandbox to be the new primary environment. The old primary environment will be demoted as sandbox, and content editors will immediately see a refresh in the interface. From that moment, they will only be able to see and make changes to the new primary environment.
From the interface:
You can also promote an environment to primary via CLI:
$ npx datocms environments:promote <SANDBOX-ENVIRONMENT-NAME>
When you're ready, you can turn off maintenance mode to allow content editors to return to their regular editorial workflow:
$ npx datocms maintenance:offDeactivating maintenance mode... done
In the unfortunate event you deploy some bad code, you can rollback to a prior, known good version of your project simply re-promoting your old primary environment and re-deploying your frontend/apps to the previous state. The effect is immediate, no re-compilation is required.
Check out this tutorial on how to migrate your content schema using scripts: