Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RAMI-67: Add database build extension to load and evaluate Puse library SQL folder. #126

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/triangulum/build_db.clj
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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)]
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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."]
Expand All @@ -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
Expand All @@ -202,6 +211,7 @@
(or password dbname) ; user-pass
admin-pass
dev-data
pulse-data
verbose)
:functions (load-folder :functions
host
Expand Down