Skip to content

Instantly share code, notes, and snippets.

@geraldodev
Last active January 22, 2023 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geraldodev/f4cc25376331ebd37d2eb8a4d3ccf8a9 to your computer and use it in GitHub Desktop.
Save geraldodev/f4cc25376331ebd37d2eb8a4d3ccf8a9 to your computer and use it in GitHub Desktop.
Babashka script to download and invoke the awesome MyBatis Migrations https://mybatis.org/migrations/index.html
#!/usr/bin/env bb
; migrate command, invoker of org.apache.ibatis.migration.Migrator
; Downloads maven artifact
; it adds --path=migrations if not specified as command-line-args
; this script is meant to be on root of the project, and when executed
; like ./migrate.clj it applies the migrations on the migrations subdirectory
; to initialize migrations path use:
; ./migrate.clj init
;
; After initializing the migrations subdirectory configure the credentials
; of the development database on migrations/enviroments/development.properties
; Example:
;## JDBC connection properties.
;driver=org.postgresql.Driver
;url=jdbc:postgresql://localhost:5432/mydatabasename
;username=postgres
;password=mypassword
;
; To create new migration file use: './migrate.clj new migration_name'
; To see the status of applied ones use: './migrate.clj status'
; use './migrate.clj up' to advance pending migrations
; use './migrate.clj down' to retreat the migration one file
;
; Mybatis uses the same file for up and down
; Everything below -- //@UNDO line reverts the changes
; Everything above undo line is supposed be migrations commands
; Migration without undo line is considered just up
;
; Mybatis migration Documentation https://mybatis.org/migrations/index.html
; Github https://github.com/mybatis/migrations
(require
'[babashka.classpath :as bc]
'[babashka.fs :as fs]
'[clojure.java.io :as io]
'[clojure.java.shell :refer [sh]]
'[clojure.string :as str]
)
(def artifact 'org.mybatis/mybatis-migrations)
(def version "3.3.11")
(def deps-edn `{:deps {~artifact {:mvn/version ~version}
org.postgresql/postgresql {:mvn/version "42.5.1"}
}})
(def migrations-subdir "migrations")
(println (str artifact " " version))
(require '[babashka.deps :as deps])
(deps/add-deps deps-edn)
(def patched-args
(if-not (some #(str/starts-with? % "--path=") *command-line-args*)
(let [migrations-dir (-> (File. ".")
fs/canonicalize
fs/path
str ;; i'm transforming to str to get away of of no Coercion of sun.nio.fs.UnixPath
(io/file migrations-subdir))]
(println (str "Defaulting to path " migrations-dir))
(conj (into [] *command-line-args*)
(str "--path=" migrations-dir )))
*command-line-args*))
(println (:out (apply sh
"java"
"-Xms500m" "-Xmx500m" "-XX:-UseGCOverheadLimit"
"-classpath" (bc/get-classpath)
"-Dapp.name=\"migrate\""
"org.apache.ibatis.migration.Migrator"
patched-args)))
; this was original migrate shell command that we are mimicking
; exec "$JAVACMD" $JAVA_OPTS -Xms500m -Xmx500m -XX:-UseGCOverheadLimit \
; -classpath "$CLASSPATH" \
; -Dapp.name="migrate" \
; -Dapp.pid="$$" \
; -Dapp.repo="$REPO" \
; -Dapp.home="$BASEDIR" \
; -Dbasedir="$BASEDIR" \
; org.apache.ibatis.migration.Migrator \
; "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment