Coast Database Migrations

Database migrations in Coast are unlike migrations you'll find in most clojure libraries.

Usually it's a few sql files that you edit and don't get me wrong there's nothing wrong with sql, it keeps things simple, requires almost no code to apply migrations to your database and you get full access to all of the power of that database.

What if your goal is to develop an app as fast as possible though? Do you really want to keep writing out create table a million times? What about alter table x add column?

Rails has taught a whole generation of software engineers that there can be a different way, a better way to write migrations using the power of ruby. The question is then, why has this not been done in clojure?

I don't have all the answers alright. I do know that coast has this, so you don't have to suffer in silence anymore, here's how they work:

(ns migrations.20190323113040-create-table-post
  (:require [coast.db.migrations :refer :all]))

(defn change []
  (create-table :post
    (references :member)
    (text :title)
    (text :body)
    (integer :published-at)
    (timestamps)))

Slick. Here's the breakdown:

The first part is a namespace, just like every .clj file ever created in the history of clojure's existence. The second part is the :require where you need to require the coast.db.migrations namespace and all of the functions within it.

Now here comes the meat and potatoes, the defn change []. This is essentially a straight up rip off (or inspiration) of rails, there's a function for each of the most common operations you'll want to do, like create-table, add-column, add-index those kinds of things, they are all laid out in the docs under migrations

There are also handy functions for simplifying columns and columns with foreign keys, like references.

Clojure database migrations have been made easy, finally.