Nomad is a SQL schema migration library. It uses pure Clojure functions to implement forward migrations (rollback migrations are a planned feature).
Add below git coordinates in deps.edn
omnyway-labs/nomad
{:git/url "https://github.com/omnyway-labs/nomad.git",
:sha "8af037d2cafeb3bf48cca804493ad8d43b74da7e"}
Define migration handlers as follows:
(require '[nomad.core :as nomad :refer [defmigration]])
(require '[clojure.java.jdbc :as jdbc])
(defmigration initial-schema
:up (fn []
(jdbc/do-commands
"CREATE TABLE test(name VARCHAR(32))")))
(defmigration add-age-column
:up (fn []
(jdbc/do-commands
"ALTER TABLE test ADD COLUMN age INTEGER")))
The name of the migration is used as a unique identifier and is used to track whether the migration has already been applied.
To apply a set of migrations Nomad requires a migrator
to be provided - see nomad-migrators for a list of currently implemented migrators (H2 and Postgres). Migrators provide concrete implementations of the IMigrator
protocol, which specifies function entry points for loading and saving migration state.
(require '[nomad.migrator.h2 :as h2])
;; create a migrator
(def migrator (h2/connect {:db "mem:test;DB_CLOSE_DELAY=-1;IGNORECASE=TRUE"}))
;; apply migrations
(nomad/migrate! migrator)
16-08-06 19:11:19 luna INFO [nomad.core:89] - Applying migration init-schema
16-08-06 19:11:19 luna INFO [nomad.core:89] - Applying migration add-test1-age
See nomad-migrators for examples of how to create migrators for different databases.