Skip to content
Jonas Collberg edited this page Feb 8, 2019 · 20 revisions

First, add the Ragtime dependency to your project:

[ragtime "0.8.0"]

Next, create a directory to hold your migrations:

mkdir -p resources/migrations

A migration may be specified in edn or SQL format. An edn migration is contained in one file, and looks like this:

;; resources/migrations/001-foo.edn
{:up   ["CREATE TABLE foo (id int);"]
 :down ["DROP TABLE foo;"]}

A SQL migration is held in at least two files. One for the "up" SQL:

-- resources/migrations/001-foo.up.sql
CREATE TABLE foo (id int);

And one for the "down" SQL:

-- resources/migrations/001-foo.down.sql
DROP TABLE foo;

It's up to you which format you choose. Once you have at least one migration, you can set up Ragtime. You'll need to build a configuration map that will tell Ragtime how to connect to your database, and where the migrations are. In the example below, we'll put the configuration in the user namespace:

(ns user
  (:require [ragtime.jdbc :as jdbc]))

(def config
  {:datastore  (jdbc/sql-database {:connection-uri "..."})
   :migrations (jdbc/load-resources "migrations")})

You can now migrate and rollback your database using that configuration at the REPL:

user=> (require '[ragtime.repl :as repl])
nil
user=> (repl/migrate config)
Applying 001-foo
nil
user=> (repl/rollback config)
Rolling back 001-foo
nil
Clone this wiki locally