summaryrefslogtreecommitdiff
path: root/src/chef/database.clj
diff options
context:
space:
mode:
authorTim <contact@bytim.eu>2025-05-12 22:10:39 +0200
committerTim <contact@bytim.eu>2025-05-12 22:10:39 +0200
commit58906c635ad0ea2028f99997cb673ef40a2ea2bb (patch)
tree1eb8718b6d43ab19b0b91acae0f47bcbcc223faa /src/chef/database.clj
parent06ecbc8c6d52f3e832683e68a52461c5f7c6cb84 (diff)
downloadchef-58906c635ad0ea2028f99997cb673ef40a2ea2bb.tar.xz
chef-58906c635ad0ea2028f99997cb673ef40a2ea2bb.zip
Add sqlite database
Diffstat (limited to 'src/chef/database.clj')
-rw-r--r--src/chef/database.clj23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/chef/database.clj b/src/chef/database.clj
new file mode 100644
index 0000000..ec5d89d
--- /dev/null
+++ b/src/chef/database.clj
@@ -0,0 +1,23 @@
+(ns chef.database
+ (:require [honey.sql :as sql]
+ [next.jdbc :as jdbc]
+
+ [chef.database.init :as cdinit]))
+
+(def db (delay (jdbc/get-datasource {:dbtype "sqlite" :dbname "chef.db"})))
+
+(def ^:private patches {"init" cdinit/exec!})
+(defn run-patches! []
+ (->> (sql/format {:create-table [:applied-patches :if-not-exists]
+ :with-columns [[:name :text]]})
+ (jdbc/execute! @db))
+ (let [applied-patches (->> (sql/format {:select [:*] :from [:applied-patches]})
+ (jdbc/execute! @db)
+ (map :applied_patches/name))]
+ (doseq [[k patch] patches]
+ (when (neg? (.indexOf applied-patches k))
+ (println (str "DB: Running " k " patch..."))
+ (patch @db)
+ (->> (sql/format {:insert-into :applied-patches
+ :values [[k]]})
+ (jdbc/execute! @db))))))