summaryrefslogtreecommitdiff
path: root/src/chef/database.clj
blob: af34b72edcf5b9edb7fef4e9eea02831686fe0de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
(ns chef.database
  (:require [honey.sql :as sql]
            [next.jdbc :as jdbc]

            [chef.database.init :as cdinit]
            [chef.database.root-category :as cdroot-category]))

(def db (delay (jdbc/get-datasource {:dbtype "sqlite" :dbname "chef.db"})))

(def ^:private patches {"init"          cdinit/exec!
                        "root-category" cdroot-category/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))))))