How code-based migrations work
masterCode-based migrations allow you to run Clojure functions instead of raw SQL. This is useful for complex logic like data backfilling or dynamic DDL.
Implementation Steps:
- Create an EDN descriptor file in your migrations directory (e.g.,
YYYYMMDDHHMMSS-name.edn). This file maps the migration to a namespace and specific functions. - Implement the functions in a Clojure namespace. The
upanddownfunctions must accept a single argument: the Migratusconfigmap.
EDN Descriptor Format:
:ns: The namespace containing the migration functions.:up-fn: The function to run for the 'up' migration. Can be a symbol or a vector[function extra-arg1 extra-arg2].:down-fn: The function to run for the 'down' migration.:transaction?: Boolean to enable/disable transactions (defaults totrue).
Note: You can mix SQL and code-based migrations in the same directory; they will execute in the order of their timestamps.
;; resources/migrations/20170331141500-import-users.edn
{:ns app.migrations.import-users
:up-fn migrate-up
:down-fn migrate-down
:transaction? true}
;; src/app/migrations/import_users.clj
(ns app.migrations.import-users)
(defn migrate-up [config]
;; logic here)
(defn migrate-down [config]
;; logic here)