We are used to the Everything as Code things and we love it. But how about Database as Code? If you have just dropped your DB migrations scripts to your pipeline, then it’s great, but that’s where “Database as Code” is just getting started. In this post, I will share my view on “Database as Code” mantra.
Database as Code. Not only migrations appeared first on MariaDB.org
We are used to the Everything as Code things and we love it. But how about Database as Code? If you have just dropped your DB migrations scripts to your pipeline, then it’s great, but that’s where “Database as Code” is just getting started. In this post, I will share my view on “Database as Code” mantra.
Everything as CodeAnd we will start with “Everything as Code” things. “Everything as Code” is a philosophy, where any IT area might be represented as a plain code. And we can work with it using standard tools and technologies (like code editors, control version systems, static analyzis etc.). And yes, indeed, there are many “Everything as Code” realizations for many areas. For example - Gitlab CI, Ansible, Markdown, etc.
And we can get all the benefits of working with Code:
It was a very exotic combination of words, but Dan North in hist talk “Aren’t we forgetting someone?” proposed four simple rules for treating a database like Code:
It’s been six years, and nowadays, it’s hard to find a project that doesn’t follow these rules (at least the first three). There are a lot of different database migration tools and different ways to integrate it with your Version Control system and your CI/CD pipeline. For example Flyway, Ghost, Sqitch, Skeema and ofcourse Liquibase.
And it’s really great!
DB isn’t only SchemaFirst of all, DB is Data and Queries to Data (DML for data and metadata)
Also databases needs:
SQL is a universal language for data and metadata. And SQL first of all was made by humans for humans, not for machines.
SQL HellBut on the other hand we have some problems with SQL, I call it “SQL Hell” (like “JAR hell” or “DLL hell”). Our SQL-queries scattered everywhere:
And we can’t control, test and trust these tons of SQL.
Keep All SQL as CodeBut how about dead simple idea - keep all your SQL-queries as normal code? Why not? And I have prepared some additional “Database as Code” rules:
The full version of these rules is hosted on GitHub. Please check it out. And I will be very grateful for the stars, PR’s, issues and any other feedback.
Is There It in Wild Life?But are there tools in real life that satisfy this rules? And our answer: “Yes, there are”.
Data Building ToolFirst of all is Data Building Tool, or simply dbt. dbt is a tool for data transformation. And the main idea is very clear and very simple - you just drop sql-file with your select statements to yor repository, while dbt materializes these statements into tables and views. No boilerplate code, only SQL!

dbt provide also:
Another example is Data Maping. Data Maping tools are usually very sophisticated tools for extracting and mapping our Data to our Data structures. It usually results in SQL code generation and performance issues.
But some tools provide to us a Query-first design. You just drop sql-file with your query to yor RepOsitory:
-- :name find_user :one
select *
from users
where user_id = :user_idAnd use it from your application code:
user = queries.find_user(user_id=42)
# -> { 'user_id': 42, 'username': 'mcfunley' }it’s very simple and predictable and no magic!
There are a lot of Query-first libraries and frameworks for different languages (e.g. Yesql, HugSQL, PugSQL, SQLDelight etc).
Project “Malewicz”And finally, I would like to present my small experimental project based on the “Database as Code” ideas - Malewicz.
Malewicz is Yet Another graphical SQL-client (or SQL-manager) for DB schema exploring and performance analysis but with some key features:
Check it out on GitHub and try online demo!
ConclusionIn the database world, we already have a great universal language with marvelous history, and it’s SQL. SQL allows you standardized declarative access not only to relational DB, but to non-relational DB, streaming platforms, files, clouds and so on. However, unfortunately, we often see that the SQL-code is often considered as a kind of bytecode. But we say - SQL-code is a normal code and all SQL-code needs version control, review, testing, CI/CD and all this stuff. ∎
| # | Наименование новости | Тональность | Информативность | Дата публикации |
|---|---|---|---|---|
| 1 | Nurturing, Motivating and Recognizing Non-Code Contributions | 0 | 8.06 | 08-02-2023 |
| 2 | Monthly Percona Developer Meetup | 0 | 6.97 | 26-09-2022 |
| 3 | Open Source for Non-Techs – Find Your Way to Contribute! | 0 | 5.87 | 29-09-2022 |
| 4 | Write for the Percona Community | 0 | 5.8 | 22-05-2026 |
| 5 | Building and Running Percona Everest From Source Code | 0 | 6.1 | 30-10-2023 |
| 6 | How a Database Monitoring Tool Can Help a Developer. The Story of One Mistake. | 0 | 3.71 | 07-04-2023 |
| 7 | Percona Joins Community Over Code 2024 in Bratislava, Slovakia | 0 | 8.4 | 21-06-2024 |
| 8 | Database Schema Management Via Liquibase | 0 | 6.23 | 01-10-2020 |
| 9 | Pre-FOSDEM & FOSDEM 2026, Community, Databases, and Open Source | 0 | 12.63 | 09-02-2026 |
| 10 | Zero downtime schema change with Liquibase & Percona | 0 | 6.73 | 26-10-2020 |