From 97caf75f4efb042157dbd0fe8c057dd23cafec1a Mon Sep 17 00:00:00 2001
From: Cam Saul <cam@geotip.com>
Date: Wed, 4 Feb 2015 13:30:46 -0800
Subject: [PATCH] add `migrate` and `ins` functions to `metabase.db`

---
 .gitignore          |  1 +
 src/metabase/db.clj | 22 +++++++++++++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index 6f7cabca03b..428d9e6d5f4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,3 +12,4 @@ pom.xml.asc
 .idea/
 /docs
 profiles.clj
+/metabase.db.*.db
diff --git a/src/metabase/db.clj b/src/metabase/db.clj
index ba5b3e06947..ba80e99261f 100644
--- a/src/metabase/db.clj
+++ b/src/metabase/db.clj
@@ -1,5 +1,7 @@
 (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))))
-- 
GitLab