From ff4d11e6b1757c68996e8e468f4b94dde08d4f09 Mon Sep 17 00:00:00 2001 From: Bear Date: Mon, 6 Nov 2023 23:37:36 -0800 Subject: [PATCH] Add build-db feature to load sql from a folder on the classpath --- src/triangulum/build_db.clj | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/triangulum/build_db.clj b/src/triangulum/build_db.clj index eb7550b..5b72f9b 100644 --- a/src/triangulum/build_db.clj +++ b/src/triangulum/build_db.clj @@ -1,6 +1,7 @@ (ns triangulum.build-db (:import java.io.File) - (:require [clojure.java.io :as io] + (:require [clojure.java.classpath :as cp] + [clojure.java.io :as io] [clojure.java.shell :as sh] [clojure.spec.alpha :as s] [clojure.string :as str] @@ -103,7 +104,12 @@ (def ^:private folders {:tables "./src/sql/tables" :functions "./src/sql/functions" :defaults "./src/sql/default_data" - :dev "./src/sql/dev_data"}) + :dev "./src/sql/dev_data" + ;; Search the classpath for pulse sql folder, i.e. /somepath/pulse/src/sql + :pulse (->> (cp/classpath-directories) + (map #(.toString %)) + (filter #(re-find #"pulse.*sql" %)) + (first))}) (defn- load-folder [sql-type host port database user user-pass verbose] (let [folder (sql-type folders)] @@ -113,7 +119,7 @@ (apply sh-wrapper "./" {:PGPASSWORD user-pass} verbose) (println)))) -(defn- build-everything [host port database user user-pass admin-pass dev-data? verbose] +(defn- build-everything [host port database user user-pass admin-pass dev-data? pulse-data? verbose] (println "Building database...") (let [file (io/file "./src/sql/create_db.sql")] (if (.exists file) @@ -129,7 +135,9 @@ (load-folder :functions host port database user user-pass verbose) (load-folder :defaults host port database user user-pass verbose) (when dev-data? - (load-folder :dev host port database user user-pass verbose))) + (load-folder :dev host port database user user-pass verbose)) + (when pulse-data? + (load-folder :pulse host port database user user-pass verbose))) (println "Error file ./src/sql/create_db.sql is missing.")))) ;; Backup / restore functions @@ -167,6 +175,7 @@ :default 5432] :dbname ["-d" "--dbname DB" "Database name."] :dev-data ["-x" "--dev-data" "Load dev data."] + :pulse-data ["-s" "--pulse-data" "Load pulse data."] :file ["-f" "--file FILE" "File used for backup and restore."] :admin-pass ["-a" "--admin-pass PASSWORD" "Admin password for the postgres account."] :user ["-u" "--user USER" "User for the database. Defaults to the same as the database name."] @@ -193,7 +202,7 @@ cli-actions "build-db" (get-config :database)) - {:keys [host port dbname dev-data file password admin-pass user verbose]} options] + {:keys [host port dbname dev-data pulse-data file password admin-pass user verbose]} options] (case action :build-all (build-everything host port @@ -202,6 +211,7 @@ (or password dbname) ; user-pass admin-pass dev-data + pulse-data verbose) :functions (load-folder :functions host