BLACKFYRE
MG—01
LANG
←
ALL POSTS
05 · Writing / 2022
27 JUN 2022 · 2 MIN READ

Figma for Databases

Notes from refactoring a 43-table database: describing it in DBML, then diagramming, documenting and turning it into SQL from one file.

CONTENTS
03 +

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:

schema.dbml
DBML

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:

SHELL
$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!

TAGS
Databases
DBML
PostgreSQL
06 · CONTACT

Have a system that needs building?

GET IN TOUCH → PROJECTS →
PRODUCT DATA SHEET
MG—01
BLACKFYRE
S/N MG-1985-1027
Miklós Galicz — Golang Advocate · Solution Architect
MODEL
MG—01 "Miklós Galicz"
SERIES
1985
ORIGIN
Nagykovácsi, Hungary
FUNCTION
Senior Full Stack Engineer · Solution Architect
CORE LANGUAGES
Go · PHP · JavaScript
SPOKEN
Hungarian · English · German
SERVICE LIFE
~20 years in software, ongoing
POWER SUPPLY
Coffee, 2–4 cups / day
DIMENSIONS
1 × human, standard size
OPERATING TEMP.
Calm under production incidents
CONNECTIVITY
[email protected] · github.com/blackfyre · linkedin.com/in/galiczmiklos
Less, but better. Specifications subject to continuous improvement.
● ● ●
MG—01 · SERIES 1985
№ MG-1985-1027
CERTIFICATE OF OPERATION
Certified Operator
Has located every documented feature of the MG—01 without reading the manual. Probably.
TIME
—
FEATURES
—
DATE
—
SIGNED
Miklós Galicz