Skip to content

Instantly share code, notes, and snippets.

@geraldodev
Last active October 24, 2024 14:06
Show Gist options
  • Save geraldodev/a13968d07430b4a8328db822d6c471c8 to your computer and use it in GitHub Desktop.
Save geraldodev/a13968d07430b4a8328db822d6c471c8 to your computer and use it in GitHub Desktop.
babashka migrate.clj that downloads postgres and mybatis jar and reads username and password from local .env compatible with https://caveman.mccue.dev/tutorial/clojure/15_read_environment_variables
#!/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=${username}
;password=${password}
;
; 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.4.0")
(def deps-edn `{:deps {~artifact {:mvn/version ~version}
org.postgresql/postgresql {:mvn/version "42.7.4"}
}})
(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*))
(def username "postgres")
(def env-file (fs/file ".env"))
(cond
(not (.exists env-file))
(str "env file " env-file " not found")
:else
;; extracted from https://github.com/ivopt/dotenv.clj/blob/master/src/dotenv.clj#L21
(let [env (->> (slurp env-file)
(str/split-lines) ; split input by linebreak
(map str/trim) ; trim heading or tailing spaces
(remove #(-> % empty? ; discard empty lines
(str/starts-with? "#"))) ; discard commented lines
(map #(str/split % #"=")) ; split by equal
(map #(let [[h & t] %]
[(str/replace h #"export *" "") ; handle "exports declarations"
(str/join "=" t) ])) ; join back values that got split
; (map #(vec (->> % (map str/trim) ; trim whitespaces on var and value
; (map unquote-string)))) ; unquote values
(into {})
doall
)
]
(println (:out (apply sh
"java"
"-Xms500m" "-Xmx500m" "-XX:-UseGCOverheadLimit"
"-classpath" (bc/get-classpath)
"-Dapp.name=migrate"
(str "-Dusername=" (get env "POSTGRES_USERNAME") )
(str "-Dpassword=" (get env "POSTGRES_PASSWORD"))
; (str "-Ddatabase.schema=teste" )
"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