How to manually use the "templated instance" functionality?
Closed this issue · 1 comments
I am using this from a testing framework that isn't TestNG and the tests are taking a good while to run. Based on the docs it seems like there is some mechanism to just re-use the database between tests but clearing the data and keeping the schema, but it is not clear how to construct that mechanism myself.
I am coding in Clojure, which makes could make my current code hard to parse, but I am going to share just in case it is helpful.
(defn with-test-db [cb]
;; TODO: Use the functionality to create test databases from templates.
(with-open [embedded-postgres (EmbeddedPostgres/start)]
(let [db (-> embedded-postgres (.getPostgresDatabase))]
(-> (Flyway/configure)
(.locations ^{:tag "[Ljava.lang.String;"}
(into-array ["filesystem:migrations/"]))
(.dataSource db)
(.load)
(.migrate))
(cb db))))
The template functionality is part of postgres, and it's very simple.
Do CREATE DATABASE template_app_name
, then run your flyway migrations on that database and load fixture data if you have any. Make sure not to leave any connections open to it. Then when you want to create a fresh test db, us CREATE DATABASE test_app_name TEMPLATE template_app_name
. It's not necessary to run flyway migrations on test_app_name
.