Content migration from dev/staging to production

A lot of developers who are coming from the more traditional, hacky style of WordPress development will have done this before: deploy a site by copying the database from dev to production. This often seems appealing, because you’ve spent a lot of time getting content looking right on dev, and you don’t want to have to recreate it all over again on live.

Of course, you’d have to change the site URL, and do a db search and replace for URLs, all whilst hoping the client doesn’t look at the site while you’re doing it… I’m sure we all moved to Trellis to get away from this sort of thing.

A better solution, more in line with Trellis principles, would be to use WP-CLI to export the content from dev, and then import it to live. Doing this manually on the command line just before or after a deployment is relatively easy. However, often your new content relies on deployed CSS/code changes, or vice versa. Ideally then, the content migration should happen automatically as part of the deployment. Else you’ll be frantically trying to migrate the content immediately after the deploy, which is never fun.

I’d like to explore how content migrations could be integrated into Trellis deployments.

It seems that hooks ( https://roots.io/trellis/docs/deploys/#hooks ) could be useful here. The ‘deploy_after’ hook could trigger an Ansible task that runs the required WP-CLI commands to import the content. The export file could be checked into the git repo, and would be pulled to the server during the deploy.

One issue I can foresee is that we only want content migrations to run if they haven’t been run already. This could be crudely managed by manually removing tasks/export files from the repo after they have been run. A nicer solution would be to keep some kind of record of what has already been imported. This way, content migrations would become similar to database migrations in frameworks like Rails/Django.

I’m going to see how far I can get with this, but if anyone has any advice or experience of doing content migrations, then I would love to hear from you.

2 Likes

@Twansparant’s existing thread is pretty good.