CONTENTS03 +
Recently I was tasked with a relatively big database refactor (43 tables and counting) with a lot of interconnections between them, and, as usual, everyone wanted to add their two cents.
The problem
The first problem was that the company didn’t have any tools or best practices for this occasion. I usually rely on Vemto or Laravel Blueprint to create the database for my Laravel projects, but for this project it wasn’t really an option (the company doesn’t have any PHP-based apps, nor do they want any). So off I went to find something that can deal with the issue at hand: single-page apps hosted on Firebase, with a PostgreSQL database and a GraphQL layer somewhere in the cloud…
DBML
The tool I’ve found is DBML, short for Database Markup Language. This is a really simple and universal way of describing relational databases, and to top it off, it allows for a lot of nifty things! The company who developed the language also offers quite a number of things, like DBML visualisation, conversion into SQL statements for both MySQL and PostgreSQL, a documentation tool… there’s a lot to take in! Most of these services are available for free, which is also a testament to their open-source commitment!
To give an idea of how little it takes, here are two tables and the relationship between them:
The same file goes into dbdiagram.io to draw the diagram and into dbdocs.io for browsable documentation, and the @dbml/cli package turns it into SQL:
$npx -p @dbml/cli dbml2sql schema.dbml --postgres -o schema.sql
That’s what made it work for a refactor everyone had an opinion on: the schema is a text file, so changes can be proposed, reviewed and diffed like code, and the diagram is never out of date.
Wrapping up
In short, it’s Figma, but for databases! It’s simply an amazing tool that really propelled the planned database structure, and we had a blast using it! It’s a must-have tool in your arsenal!