Continous Integration: Make Sure Database Changes are Included using GitLab CI/CD

Fanny Huang
Tales of Libeo
Published in
1 min readJan 25, 2021

Because of that one time we broke our prod.

Photo by Kelly Sikkema on Unsplash

At Libeo, we use TypeORM’s migration command to generate all our migrations scripts. Great tool to generate and keep a record of our database changes. But there is that one time, when these scripts were inadvertently not pushed to master and, eventually the new changes have been deployed to our production environment breaking a part of our app.

To prevent it from happening again, we introduced a new pipeline in our continuous delivery process.

First, install GitLab CI/CD on your GitLab project. Here’s a quick start on how to install it.

👉 https://docs.gitlab.com/ee/ci/quick_start/README.html

As mentioned before, our project works with TypeORM. It integrates a lot of CLI commands and one of them is the ability to compare database tables and database schemas defined by your code.

First, create a configuration file for your database.

TypeORM databas configuration in ormconfig-ci.ts

Next, in package.json, add commands to run migrations and compare schemas.

Scripts to run migrations in package.json

Finally, in gitlab-ci.yml, create a new job for your pipeline.

Pipeline definition in .gitlab-ci.yml

Conclusion

Easy right ? 🥳 Thanks to this new pipeline, mistakes like not pushing migrations scripts won’t affect master : your database tables and entities will never be out of sync again !

--

--