diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | deps.edn | 13 | ||||
-rw-r--r-- | src/chef/core.clj | 5 | ||||
-rw-r--r-- | src/chef/database.clj | 23 | ||||
-rw-r--r-- | src/chef/database/init.clj | 16 |
5 files changed, 52 insertions, 6 deletions
@@ -3,3 +3,4 @@ target/ .nrepl-port chef.iml +chef.db @@ -1,9 +1,12 @@ {:paths ["src" "resources"] - :deps {http-kit/http-kit {:mvn/version "2.8.0"} - metosin/reitit {:mvn/version "0.8.0"} - hiccup/hiccup {:mvn/version "2.0.0-RC5"} - ring-oauth2/ring-oauth2 {:mvn/version "0.3.0"} - lynxeyes/dotenv {:mvn/version "1.1.0"}} + :deps {http-kit/http-kit {:mvn/version "2.8.0"} + metosin/reitit {:mvn/version "0.8.0"} + hiccup/hiccup {:mvn/version "2.0.0-RC5"} + ring-oauth2/ring-oauth2 {:mvn/version "0.3.0"} + lynxeyes/dotenv {:mvn/version "1.1.0"} + com.github.seancorfield/next.jdbc {:mvn/version "1.3.1002"} + org.xerial/sqlite-jdbc {:mvn/version "3.47.1.0"} + com.github.seancorfield/honeysql {:mvn/version "2.7.1295"}} :aliases {:neil {:project {:name chef/chef :version "0.0.1"}} diff --git a/src/chef/core.clj b/src/chef/core.clj index 638698d..f3422d5 100644 --- a/src/chef/core.clj +++ b/src/chef/core.clj @@ -1,8 +1,11 @@ (ns chef.core (:require [org.httpkit.server :as http-server] - [chef.routes :as croutes]) + [chef.routes :as croutes] + [chef.database :as cdb]) (:gen-class)) (defn -main [& args] + (println "Running db patches...") + (cdb/run-patches!) (println "Starting http server...") (http-server/run-server @croutes/ring-handler {:port 8080})) 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)))))) diff --git a/src/chef/database/init.clj b/src/chef/database/init.clj new file mode 100644 index 0000000..f791e39 --- /dev/null +++ b/src/chef/database/init.clj @@ -0,0 +1,16 @@ +(ns chef.database.init + (:require [next.jdbc :as jdbc] + [honey.sql :as sql])) + +(defn exec! [db] + (jdbc/execute! db (sql/format {:create-table :categories + :with-columns [[:id :integer :auto-increment :primary-key] + [:name :text] + [:question :text] + [:children :text] + [:parent :text]]})) + (jdbc/execute! db (sql/format {:create-table :recipes + :with-columns [[:id :integer :auto-increment :primary-key] + [:category :integer] + [:title :text] + [:description :text]]}))) |