Skip to content

Commit

Permalink
Support unlogged-spec for queries without logging
Browse files Browse the repository at this point in the history
  • Loading branch information
danielytics committed Sep 29, 2020
1 parent b135e8c commit 5c9bc2c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/duct/database/sql/hikaricp.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@

(defmethod ig/init-key :duct.database.sql/hikaricp
[_ {:keys [logger connection-uri jdbc-url] :as options}]
(sql/->Boundary {:datasource
(-> (dissoc options :logger)
(let [datasource (-> (dissoc options :logger)
(assoc :jdbc-url (or jdbc-url connection-uri))
(hikari-cp/make-datasource)
(cond-> logger (wrap-logger logger)))}))
(hikari-cp/make-datasource))]
(if logger
(-> (sql/->Boundary {:datasource (wrap-logger datasource logger)})
(assoc :unlogged-spec {:datasource datasource}))
(sql/->Boundary {:datasource datasource}))))

(defmethod ig/halt-key! :duct.database.sql/hikaricp [_ {:keys [spec]}]
(let [ds (unwrap-logger (:datasource spec))]
Expand Down
10 changes: 10 additions & 0 deletions test/duct/database/sql/hikaricp_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,13 @@
(is (not (.isClosed (unwrap-logger (:datasource spec)))))
(ig/halt-key! ::sql/hikaricp hikaricp)
(is (.isClosed (unwrap-logger (:datasource spec))))))

(deftest unlogged-test
(let [logs (atom [])
logger (->AtomLogger logs)
hikaricp (ig/init-key ::sql/hikaricp {:jdbc-url "jdbc:sqlite:" :logger logger})
spec (:unlogged-spec hikaricp)]
(jdbc/execute! spec ["CREATE TABLE foo (id INT, body TEXT)"])
(jdbc/db-do-commands spec ["INSERT INTO foo VALUES (1, 'a')"
"INSERT INTO foo VALUES (2, 'b')"])
(is (empty? @logs))))

0 comments on commit 5c9bc2c

Please sign in to comment.