Database schema management for lazybones: from chaos to heaven

SCALE 21x

Julien RIOU

March 15, 2024



Speaker


Summary

  1. Basics of schema migrations
  2. Who are we?
  3. Chaos
  4. Schema as code, CI/CD, code reviews
  5. Inventory, automation
  6. Heaven
  7. Known issues
  8. Takeaways

Basics of schema migrations


Schema

https://github.com/devrimgunduz/pagila


Schema changes over time


Schema migration

Data definition language (DDL)

ALTER TABLE customer
    ADD COLUMN archived_at TIMESTAMP WITH TIME ZONE;

Who are we?


Internal databases


Statistics


Cluster example


Chaos


Who can ALTER my database?


Startup mindset


The DBA forgot on which host the database is


The DBA misunderstood what developer really wanted


The DBA executed the query in Europe but forgot Canada


The DBA forgot to create a transaction and one query failed in the middle


The DBA started a transaction but forgot to commit


The DBA created a query that runs for too long and locks production


The DBA forgot to use SET ROLE and permissions are now broken


Blame the DBA!

Link to image


Ticketing

Let’s write migrations in a ticketing system


OTRS


Jira


But

  • Multiple ticketing systems for different teams
  • SQL statement are not well formatted
  • Bad copy pastes
  • Poor reviewing system


Schema as code


Git


sql-migrate

SQL schema migration tool for Go

https://github.com/rubenv/sql-migrate


Developers create code:

v0001.sql

-- +migrate Up
create table x (...);

-- +migrate Down
drop table x;