From 3baa4cc1b5b354775401ac1c73871dbe76c37f63 Mon Sep 17 00:00:00 2001
From: Cam Saul <cam@geotip.com>
Date: Mon, 16 Mar 2015 15:59:16 -0700
Subject: [PATCH] add `lein migration-summary` command that will list all
 Tables + fields in Metabase DB

---
 lein_tasks/leiningen/migration_summary.clj | 21 +++++++++++++++++++++
 src/metabase/db.clj                        |  9 +++++++++
 2 files changed, 30 insertions(+)
 create mode 100644 lein_tasks/leiningen/migration_summary.clj

diff --git a/lein_tasks/leiningen/migration_summary.clj b/lein_tasks/leiningen/migration_summary.clj
new file mode 100644
index 00000000000..84b2d1e7fe6
--- /dev/null
+++ b/lein_tasks/leiningen/migration_summary.clj
@@ -0,0 +1,21 @@
+(ns leiningen.migration-summary
+  (:require [leiningen.core.eval :as lein]))
+
+(defn migration-summary [project & args]
+  (lein/eval-in-project
+   project
+   `(do
+      (require '[metabase.config :as ~'config]
+               '[metabase.db :as ~'db]
+               '[metabase.driver.generic-sql.sync :as ~'sync])
+      (let [db# {:engine (name (config/config-kw :mb-db-type))
+                 :details {:conn_str (db/metabase-db-conn-str)}}]
+        (->> (sync/table-names db#)
+             (map (fn [table-name#]
+                    (println (format "\n* %s" table-name#))
+                    (->> (sync/jdbc-columns db# table-name#)
+                         (sort-by :column_name)
+                         (map (fn [{:keys [~'type_name ~'column_name]}]
+                                (println (format "  * %s %s" ~'column_name ~'type_name))))
+                         dorun)))
+             dorun)))))
diff --git a/src/metabase/db.clj b/src/metabase/db.clj
index 476868b2d2c..62a2d250ef7 100644
--- a/src/metabase/db.clj
+++ b/src/metabase/db.clj
@@ -55,6 +55,15 @@
                          :password (config/config-str :mb-db-pass)
                          :host (config/config-str :mb-db-host)})))
 
+(defn metabase-db-conn-str []
+  (case (config/config-kw :mb-db-type)
+    :h2 (db-file)
+    :postgres (format "host=%s port=%d dbname=%s user=%s password=%s"
+                      (config/config-str :mb-db-host)
+                      (config/config-int :mb-db-port)
+                      (config/config-str :mb-db-dbname)
+                      (config/config-str :mb-db-user)
+                      (config/config-str :mb-db-pass))))
 
 (defn test-db-conn
   "Simple test of a JDBC connection."
-- 
GitLab