Skip to content

Instantly share code, notes, and snippets.

@camsaul
Created November 29, 2022 20:27
Show Gist options
  • Save camsaul/7d2e2518175dc5c2465e085d687e254e to your computer and use it in GitHub Desktop.
Save camsaul/7d2e2518175dc5c2465e085d687e254e to your computer and use it in GitHub Desktop.
Sample Dataset Profiling (ZIP/JAR vs file)
(ns metabase.x
(:require
[metabase.sync :as sync]
[toucan.db :as db]))
(defmulti ^:private info
{:arglists '([location])}
keyword)
(defmethod info :jar
[_location]
{:name "H2 Sample Database (From JAR)"
:url "zip:/home/cam/metabase/target/uberjar/metabase.jar!/sample-database.db;USER=GUEST;PASSWORD=guest;"})
(defmethod info :file
[_location]
{:name "H2 Sample Database (From File)"
:url "file:/home/cam/metabase/resources/sample-database.db;USER=GUEST;PASSWORD=guest"})
(defmethod info :zip
[_location]
{:name "H2 Sample Database (From ZIP)"
:url "zip:/home/cam/metabase/resources/sample-db.zip!/sample-database.db;USER=GUEST;PASSWORD=guest"})
(defn- create-db! [{db-name :name, jdbc-url :url}]
(let [details {:db jdbc-url}]
(db/delete! 'Database :engine "h2", :name db-name, :details details)
(db/insert! 'Database {:engine "h2", :name db-name, :details details})))
(defn- resync-db! [db]
(time (sync/sync-database! db)))
(defn- resync! [location]
(let [db (create-db! (info location))]
(resync-db! db)))
#_(defn compress-sample-database! []
(let [file (str "file:/home/cam/metabase/resources/sample-database.db"
";UNDO_LOG=0;CACHE_SIZE=131072;QUERY_CACHE_SIZE=128;COMPRESS=TRUE;"
"MULTI_THREADED=TRUE;MVCC=TRUE;DEFRAG_ALWAYS=TRUE;MAX_COMPACT_TIME=5000;"
"ANALYZE_AUTO=1;IFEXISTS=TRUE;DB_CLOSE_DELAY=0")
jdbc-url (str "jdbc:h2:" file)]
(with-open [conn (java.sql.DriverManager/getConnection jdbc-url)
stmt (.createStatement conn)]
(.execute stmt "SHUTDOWN COMPACT;"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment