How do you do database migration/evolution?

A minimalist approach is to store the migrations as .sql files in a folder, then run them via a command line client like psql or from your Clojure code via JDBC.

For Postgres, I’ve come up with a minimalist framework. It’s so simple that it doesn’t really deserve the name “framework”. In a nutshell the idempotent function ensures that a particular migration (say, CREATE TABLE) is run only once. Subsequent runs are NOOPs. Completed migrations are stored in the migrations table.

Advantages over “real” migration tools like flyway:

  • it’s fast if there’s nothing to do so you can easily run all migrations whenever your JVM process starts
  • you know exactly what’s going on
  • it’s all just SQL so the sql snippets can include data migrations (UPDATE foo ...) as well as schema migrations (ALTER TABLE foo ...)

Limitations include lack of support for rolling back migrations. But if your use case is like mine, you may not need that functionality (and appreciate the fact that the code fits on a postcard).

3 Likes