diff --git a/src/metabase/sample_data.clj b/src/metabase/sample_data.clj index 56abf6989ab3bee6fe1d97f53b680cc9f6f9fd92..a89e97274c7e8e89ec47345dfbac1d1dc2bf02d2 100644 --- a/src/metabase/sample_data.clj +++ b/src/metabase/sample_data.clj @@ -7,9 +7,17 @@ [metabase.sync-database :as sync-database] [metabase.util :as u])) +(def ^:private ^:const ^String sample-dataset-name "Sample Dataset") +(def ^:private ^:const ^String sample-dataset-filename "sample-dataset.db.mv.db") -(def ^:private ^:const sample-dataset-name "Sample Dataset") -(def ^:private ^:const sample-dataset-filename "sample-dataset.db.mv.db") +(defn- db-details [] + (let [resource (io/resource sample-dataset-filename)] + (when-not resource + (throw (Exception. (format "Can't load sample dataset: the DB file '%s' can't be found." sample-dataset-filename)))) + {:db (-> (.getPath resource) + (s/replace #"^file:" "zip:") ; to connect to an H2 DB inside a JAR just replace file: with zip: + (s/replace #"\.mv\.db$" "") ; strip the .mv.db suffix from the path + (str ";USER=GUEST;PASSWORD=guest"))})) ; specify the GUEST user account created for the DB (defn add-sample-dataset! "Add the sample dataset as a Metabase DB if it doesn't already exist." @@ -17,28 +25,17 @@ (when-not (db/exists? Database :is_sample true) (try (log/info "Loading sample dataset...") - (let [resource (io/resource sample-dataset-filename)] - (if-not resource - (log/error (u/format-color 'red "Can't load sample dataset: the DB file '%s' can't be found." sample-dataset-filename)) - (let [h2-file (-> (.getPath resource) - (s/replace #"^file:" "zip:") ; to connect to an H2 DB inside a JAR just replace file: with zip: - (s/replace #"\.mv\.db$" "") ; strip the .mv.db suffix from the path - (str ";USER=GUEST;PASSWORD=guest")) ; specify the GUEST user account created for the DB - db (db/insert! Database - :name sample-dataset-name - :details {:db h2-file} - :engine :h2 - :is_sample true)] - (sync-database/sync-database! db)))) + (sync-database/sync-database! (db/insert! Database + :name sample-dataset-name + :details (db-details) + :engine :h2 + :is_sample true)) (catch Throwable e (log/error (u/format-color 'red "Failed to load sample dataset: %s\n%s" (.getMessage e) (u/pprint-to-str (u/filtered-stacktrace e)))))))) (defn update-sample-dataset-if-needed! - "Re-sync the sample dataset DB if it exists." + "Update the path to the sample dataset DB if it exists in case the JAR has moved." [] - ;; TODO - it would be a bit nicer if we skipped this when the data hasn't changed (when-let [db (Database :is_sample true)] - (try - (sync-database/sync-database! db) - (catch Throwable e - (log/error (u/format-color 'red "Failed to update sample dataset: %s\n%s" (.getMessage e) (u/pprint-to-str (u/filtered-stacktrace e)))))))) + (db/update! Database (:id db) + :details (db-details))))