CI-friendly migration
bsgreenb opened this issue · 4 comments
I was wondering what's the best way to set up something like
https://www.contentful.com/developers/docs/tutorials/general/continuous-integration-with-circleci/
Approach they use there is a singleton entry on Contentful which stores the current schema migration version. Then when you push code to an environment, it would know which migrations it would need to run.
This repo could potentially add dev-friendly commands like:
npm run migrate
npm run migrate:down
etc
https://edgeguides.rubyonrails.org/active_record_migrations.html#running-migrations <-- Rails had a really nice setup
How do you approach CI? What's your migration process like?
I feel like if I get this missing piece, I'll have an amazing setup between Contentful and your tool.
Howdy Ben!
We described our approach in this post on the Contentful Community boards:
https://www.contentfulcommunity.com/t/contentful-schema-diff-tool/1423
Basically, we forked Contentful's migration tool to make it work just like Rails. It scans a directory and only runs migrations that haven't been run yet. It uses a content type called "Migration History" to store info about what has already been run.
We also built a shell script wrapper around Contentful tooling that makes it easy to work with direnv and Heroku's environments. It runs the migrations like this:
[[ ! -z "$CONTENTFUL_ENVIRONMENT" ]] && ENV="--environment-id $CONTENTFUL_ENVIRONMENT"
node_modules/.bin/ts-node --skip-project node_modules/.bin/contentful-migration \
-s $CONTENTFUL_SPACE_ID $ENV -a $CONTENTFUL_MANAGEMENT_TOKEN \
-y -p batch db/migrate || return 1
Then in our Heroku release script, we call into our shell wrapper to execute this.
We looked into using CircleCI to do this for us, but we wanted to make sure we tied it together with actually releasing our new code in case we made changes that the previously-running code couldn't handle.
Bumped my old PR just in case the Contentful guys want to take another look:
contentful/contentful-migration#66 (comment)
Wow @contentful should really adopt your toolkit, as it's a first-class migration setup.. I'll ping a friend who's in contact with them @themarecelolewin
Thanks! I'll go ahead and close this issue.