Environments and migrations > Apply migrations to primary environment

    Apply migrations to primary environment

    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:

    Step 1: Turn on maintenance mode to prevent changes to the primary environment

    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:

    $ datocms maintenance:on
    Activating 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 --force flag:

    $ datocms maintenance:on
    Activating maintenance mode... !
    › Error: Cannot activate maintenance mode as some users are currently editing records
    › Try this: To proceed anyway, use the --force flag

    Step 2: Run the migrations on a newly forked sandbox environment

    You can now call the datocms migrations:run command to make a new copy of the primary environment, and run all the pending migrations:

    $ datocms migrations:run --destination=feature-add-article-model
    Migrations will be run in "feature-add-article-model" sandbox environment
    Creating a fork of "main" environment called "feature-add-article-model"... done
    Creating "schema_migration" model... done
    Running migration "1653061497_createArticleModel.js"... done
    Running migration "1653062813_addAuthors.js"... done
    Successfully run 2 migration scripts

    Step 3: Test your apps pointing them to the new sandbox

    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:

    https://graphql.datocms.com/environments/{ENVIRONMENT-NAME}
    https://graphql.datocms.com/environments/{ENVIRONMENT-NAME}/preview

    Step 4: Promote the sandbox and turn off maintenance mode

    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:

    $ 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:

    $ datocms maintenance:off
    Deactivating maintenance mode... done

    Handling rollbacks

    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.

    Learn more about data migrations

    Check out this tutorial on how to migrate your content schema using scripts: