Skip to content
Snippets Groups Projects
Commit 97caf75f authored by Cam Saul's avatar Cam Saul
Browse files

add `migrate` and `ins` functions to `metabase.db`

parent 11317c45
No related branches found
No related tags found
No related merge requests found
......@@ -12,3 +12,4 @@ pom.xml.asc
.idea/
/docs
profiles.clj
/metabase.db.*.db
(ns metabase.db
(:require [clojure.tools.logging :as log]
"Korma database definition and helper functions for interacting with the database."
(:require [clojure.java.jdbc :as jdbc]
[clojure.tools.logging :as log]
[clojure.string :as str]
[environ.core :refer [env]]
[korma.core :refer :all]
......@@ -18,3 +20,21 @@
(h2 {:db (get-db-file)
:naming {:keys str/lower-case
:fields str/upper-case}})))
(defn migrate
"Migrate the database :up or :down."
[direction]
(let [conn (jdbc/get-connection {:subprotocol "h2"
:subname (get-db-file)})]
(case direction
:up (com.metabase.corvus.migrations.LiquibaseMigrations/setupDatabase conn)
:down (com.metabase.corvus.migrations.LiquibaseMigrations/teardownDatabase conn))))
;;; UTILITY FUNCTIONS
(defn ins
"Wrapper around `korma.core/insert` that renames the `:scope_identity()` keyword in output to `:id`
and automatically passes &rest KWARGS to `korma.core/values`."
[entity & kwargs]
(-> (insert entity (values (apply assoc {} kwargs)))
(clojure.set/rename-keys ({keyword "scope_identity()"} :id))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment