Commit 564324ea authored by Johannes Knödtel's avatar Johannes Knödtel 🎶
Browse files

switch from yesql to conman/hugsql

parent 03939d85
......@@ -23,7 +23,7 @@
[migratus "0.8.32"]
[luminus-migrations "0.2.6"]
[luminus-immutant "0.2.2"]
[yesql "0.5.3"]
[conman "0.6.2"]
[to-jdbc-uri "0.3.0"]
[com.h2database/h2 "1.4.193"]
[org.immutant/web "2.0.2"]
......@@ -36,6 +36,7 @@
:min-lein-version "2.0.0"
:uberjar-name "GMS.jar"
:jvm-opts ["-server"]
:javac-options ["-target" "1.8" "-source" "1.8" "-Xlint:-options"]
:main gms.core
:migratus {:store :database}
......
-- name: add-food!
-- add food-request
-- :name add-food! :!
-- :doc add food-request
INSERT INTO food
(session, food_name, comment, customer, quantity)
VALUES (:session, :food_name, :comment, :customer, :quantity)
ON DUPLICATE KEY UPDATE quantity = :quantity;
-- name: add-price!
-- add prices
-- :name add-price! :!
-- :doc add prices
INSERT INTO prices
(session, food_name, comment, ammount_per_package, number_of_packages, package_price)
VALUES (:session, :food_name, :comment, :ammount_per_package, :number_of_packages, :package_price);
-- name: delete-order!
-- deletes an order
-- :name delete-order! :!
-- :doc deletes an order
DELETE FROM food
WHERE
session = :session
......@@ -23,8 +23,8 @@ AND
AND
customer = :customer;
-- name: edit-order!
-- edits an order
-- :name edit-order! :!
-- :doc edits an order
UPDATE food
SET
food_name = :food_name,
......@@ -37,8 +37,8 @@ AND
AND
customer = :old_customer;
-- name: get-food
-- retrive all food orders
-- :name get-food :? :*
-- :doc retrive all food orders
SELECT
food_name, comment, customer, quantity
FROM
......@@ -49,8 +49,8 @@ ORDER BY
food_name ASC,
comment ASC;
-- name: get-sessions
-- retrives all sessions with names and ids
-- :name get-sessions :? :*
-- :doc retrives all sessions with names and ids
SELECT
id, name
FROM
......@@ -58,24 +58,24 @@ FROM
ORDER BY
id DESC;
-- name: create-session<!
-- generate new session
-- :name create-session<! :i!
-- :doc generate new session
INSERT INTO sessions
(name)
VALUES (:name);
-- name: truncate-food!
-- cleans the food table
-- :name truncate-food! :!
-- :doc cleans the food table
DELETE FROM TABLE food
WHERE session = :session;
-- name: truncate-prices!
-- cleans the prices table
-- :name truncate-prices! :!
-- :doc cleans the prices table
DELETE FROM TABLE prices
WHERE session = :session;
-- name: get-already-paid
-- gets the names of all customers that already paid their bill
-- :name get-already-paid :? :*
-- :doc gets the names of all customers that already paid their bill
SELECT
customer
FROM
......@@ -83,16 +83,16 @@ FROM
WHERE
session = :session;
-- name: pay!
-- gets the names of all customers that already paid their bill
-- :name pay! :!
-- :doc gets the names of all customers that already paid their bill
INSERT INTO payments
(session, customer)
VALUES (:session, :customer)
-- H2 doesn't know IGNORE, so we will work around that like this.
ON DUPLICATE KEY UPDATE customer = :customer;
-- name: get-summed-food-numbers
-- cleans the list
-- :name get-summed-food-numbers :? :*
-- :doc cleans the list
SELECT
food.food_name,
food.comment,
......@@ -130,8 +130,8 @@ ORDER BY
food_name ASC,
comment ASC;
-- name: get-food-numbers
-- Returns all foods and prices
-- :name get-food-numbers :? :*
-- :doc Returns all foods and prices
SELECT
food_name,
comment,
......@@ -145,8 +145,8 @@ FROM
WHERE
session = :session;
-- name: get-bills
-- Gets the aggregated price each person has to pay
-- :name get-bills :? :*
-- :doc Gets the aggregated price each person has to pay
SELECT
food.food_name,
food.comment,
......
(ns gms.db.core
(:require
[clojure.java.jdbc :as jdbc]
[yesql.core :refer [defqueries]]
[conman.core :as conman]
[taoensso.timbre :as timbre]
[environ.core :refer [env]])
(:import java.sql.BatchUpdateException))
(defonce conn (atom nil))
(defqueries "sql/queries.sql")
(conman/bind-connection conn "sql/queries.sql")
(defn connect! []
(try
(reset!
conn
{:classname "org.h2.Driver"
:connection-uri (:database-url env)
:make-pool? true
:naming {:keys clojure.string/lower-case
:fields clojure.string/upper-case}})
(catch Exception e
(timbre/error "Error occured while connecting to the database!" e))))
(reset! conn (conman/connect! {:jdbc-url (:database-url env)})))
(defn disconnect! [])
(defn disconnect! []
(swap! conn conman/disconnect!))
(defn run
"executes a Yesql query using the given database connection and parameter map
the parameter map defaults to an empty map and the database conection defaults
to the conn atom"
([query-fn] (run query-fn {}))
([query-fn query-map] (run query-fn query-map @conn))
([query-fn query-map db]
(try
(query-fn query-map {:connection db})
(query-fn db query-map)
(catch BatchUpdateException e
(throw (or (.getNextException e) e))))))
......@@ -5,7 +5,8 @@
[migratus.core :as migratus]
[migratus.database :as migratus-db]
[migratus.protocols :as migratus-proto]
[clojure.java.io :as io]))
[clojure.java.io :as io]
[conman.core :as conman]))
(def db-file "./gms_test.db")
(def db-uri (to-jdbc-uri (format "jdbc:h2:%s;MODE=MYSQL" db-file)))
......@@ -15,13 +16,27 @@
(def migrations (sort-by :id (.migrations (migratus-proto/make-store config))))
(def test-connection (atom nil))
(defn- delete-test-db []
(try
(io/delete-file (io/file (str db-file ".mv.db")))
(io/delete-file (io/file (str db-file ".trace.db")))
(catch Exception _)))
(use-fixtures :each #(do (delete-test-db) (%)))
(defn- connect-to-test-db []
(reset! test-connection (conman/connect! {:jdbc-url db-uri})))
(defn- disconnect-test-db []
(swap! test-connection conman/disconnect!))
(use-fixtures :each #(do (disconnect-test-db)
(delete-test-db)
(connect-to-test-db)
(println test-connection)
(%)
(disconnect-test-db)
(delete-test-db)))
(deftest test-migrations
(doseq [migration migrations]
......@@ -257,13 +272,6 @@
(delete-test-db)
(apply migratus/up config (map :id migrations)))
(def test-connection
(atom {:classname "org.h2.Driver"
:connection-uri db-uri
:make-pool? true
:naming {:keys clojure.string/lower-case
:fields clojure.string/upper-case}}))
(defn test-query-set [session-name
food-requests-fn
purchases-fn
......@@ -273,11 +281,9 @@
expected-results]
(testing session-name
(let [connection test-connection
session (second (first (db/create-session<!
{:name session-name}
{:connection @connection})))]
session (second (first (db/create-session<! @connection {:name session-name})))]
(letfn [(run-query [query-fn data]
(doall (map #(query-fn % {:connection @connection}) data)))]
(doall (map #(query-fn @connection %) data)))]
(run-query db/add-food! (food-requests-fn session))
(run-query db/add-price! (purchases-fn session))
(run-query db/edit-order! (changes-fn session))
......@@ -310,17 +316,16 @@
payments-2
expected-results-2))
(deftest test-session-management []
(deftest test-session-management
(migrate-test-db)
(let [connection test-connection
session-1 (db/create-session<! {:name "Test Session 1"} {:connection connection})]
(is (= (db/get-sessions {} {:connection connection})
'({:id session-1
:name "Test Session 1"})))
(let [session-2 (db/create-session<! {:name "Test Session 2"} {:connection connection})]
(is (= (db/get-sessions {} {:connection connection})
'({:id session-1
:name "Test Session 1"}
{:id session-2
:name "Test Session 2"}))))))
session-1 (second (first (db/create-session<! @connection {:name "Test Session 1"})))]
(is (= (db/get-sessions @connection {})
(list {:id ,session-1
:name "Test Session 1"})))
(let [session-2 (second (first (db/create-session<! @connection {:name "Test Session 2"})))]
(is (= (db/get-sessions @connection {})
(list {:id session-2
:name "Test Session 2"}
{:id session-1
:name "Test Session 1"}))))))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment