Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Ghy 3359/embedded postgres dev/test driver (#72635)

Ben Grabowa0d8cc309d36
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn
index d1508c765af..5564415583e 100644
--- a/.clj-kondo/config.edn
+++ b/.clj-kondo/config.edn
@@ -492,6 +492,7 @@
metabase.task.impl/defjob clojure.core/defn
metabase.test.data.interface/defdataset clojure.core/def
metabase.test.data.interface/defdataset-edn clojure.core/def
+ metabase.test.embedded-postgres.core/with-system clojure.core/let
metabase.test.gentest/defgentest clojure.test/deftest
metabase.test/defdataset clojure.core/def
metabase.test/let-url clojure.core/let
diff --git a/deps.edn b/deps.edn
index 04af6314283..f0c17831641 100644
--- a/deps.edn
+++ b/deps.edn
@@ -262,8 +262,19 @@
com.gfredericks/test.chuck {:mvn/version "0.2.15"} ; generating strings from regexes (useful with malli)
criterium/criterium {:mvn/version "0.4.6"} ; benchmarking library
djblue/portal {:mvn/version "0.62.0"} ; ui for inspecting values
+ integrant/integrant {:mvn/version "1.0.1"} ; component lifecycle management
io.github.camsaul/humane-are {:mvn/version "1.0.2"} ; cleaner test output
io.github.metabase/hawk {:mvn/version "1.0.13"} ; test runner
+ io.zonky.test/embedded-postgres {:mvn/version "2.2.2"} ; In-process postgres for dev and testing
+ ;; Native postgres binaries for every OS/arch we're likely to run on in
+ ;; dev or CI. Without these Zonky falls back to x86_64 under Rosetta on
+ ;; Apple Silicon and prints a warning. Versions should be pinned to the
+ ;; latest version shipped by Zonky.
+ io.zonky.test.postgres/embedded-postgres-binaries-darwin-amd64 {:mvn/version "18.3.0"} ; macOS Intel
+ io.zonky.test.postgres/embedded-postgres-binaries-darwin-arm64v8 {:mvn/version "18.3.0"} ; macOS Apple Silicon
+ io.zonky.test.postgres/embedded-postgres-binaries-linux-amd64 {:mvn/version "18.3.0"} ; Linux x86_64 (covers ubuntu-latest GH Actions runner)
+ io.zonky.test.postgres/embedded-postgres-binaries-linux-arm64v8 {:mvn/version "18.3.0"} ; Linux arm64
+ io.zonky.test.postgres/embedded-postgres-binaries-windows-amd64 {:mvn/version "18.3.0"} ; Windows x86_64
jonase/eastwood {:mvn/version "1.4.3" ; inspects namespaces and reports possible problems using tools.analyzer
:exclusions
[org.ow2.asm/asm-all]}
diff --git a/test/metabase/test/embedded_postgres/core.clj b/test/metabase/test/embedded_postgres/core.clj
new file mode 100644
index 00000000000..14fdfa80b5a
--- /dev/null
+++ b/test/metabase/test/embedded_postgres/core.clj
@@ -0,0 +1,57 @@
+(ns metabase.test.embedded-postgres.core
+ "Dev/test utility that runs an embedded Postgres via Zonky's embedded-postgres
+ library and manages its lifecycle via Integrant. Owns no global state — clients
+ choose how to hold the running system (local binding, defonce, test fixture,
+ etc.).
+
+ Typical test usage:
+
+ (with-system [system {::embedded-postgres {:port 0}}]
+ (let [{:keys [jdbc-url]} (::embedded-postgres system)]
+ ...))
+
+ Typical REPL usage:
+
+ (require '[metabase.test.embedded-postgres.core :as emb-pg]
+ '[integrant.core :as ig])
+ (def system (ig/init {::emb-pg/embedded-postgres {}}))
+ ;; ... work with the REPL ...
+ (ig/halt! system)"
+ (:require
+ [integrant.core :as ig])
+ (:import
+ (io.zonky.test.db.postgres.embedded EmbeddedPostgres)))
+
+(set! *warn-on-reflection* true)
+
+(defn- start! ^EmbeddedPostgres
+ [port]
+ (.start
+ (doto (EmbeddedPostgres/builder)
+ (cond-> port (.setPort port)))))
+
+(defn- stop!
+ [pg]
+ (when pg (.close ^EmbeddedPostgres pg)))
+
+(defmethod ig/init-key ::db-server
+ [_ {::keys [port]}]
+ (let [pg (start! port)
+ actual-port (.getPort pg)]
+ {::pg pg
+ ::port actual-port
+ ::jdbc-url (format "jdbc:postgresql://localhost:%d/postgres?user=postgres" actual-port)}))
+
+(defmethod ig/halt-key! ::db-server
+ [_ {::keys [pg]}]
+ (stop! pg))
+
+(defmacro with-system
+ "Init an Integrant system from `config`, bind it to `sym`, run `body`, and halt
+ the system in a `finally`."
+ [[sym config] & body]
+ `(let [~sym (ig/init ~config)]
+ (try
+ ~@body
+ (finally
+ (ig/halt! ~sym)))))
diff --git a/test/metabase/test/embedded_postgres/db_server_test.clj b/test/metabase/test/embedded_postgres/db_server_test.clj
new file mode 100644
index 00000000000..b4a26320d03
--- /dev/null
+++ b/test/metabase/test/embedded_postgres/db_server_test.clj
@@ -0,0 +1,12 @@
+(ns metabase.test.embedded-postgres.db-server-test
+ (:require
+ [clojure.test :refer [deftest is testing]]
+ [metabase.test.embedded-postgres.core :as emb-pg]
+ [next.jdbc :as jdbc]))
+
+(deftest db-server-smoke-test
+ (testing "the embedded-postgres db-server component starts, accepts a JDBC connection, and runs a query"
+ (emb-pg/with-system [system {::emb-pg/db-server {}}]
+ (let [{::emb-pg/keys [port jdbc-url]} (::emb-pg/db-server system)]
+ (is (pos-int? port))
+ (is (= {:ok 1} (jdbc/execute-one! {:jdbcUrl jdbc-url} ["select 1 as ok"])))))))