Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refrain from re-initializing query-fn if queries files not modified #122

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions libs/kit-core/src/kit/ig_utils.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
(ns kit.ig-utils
"Integrant utilities"
(:require
[clojure.java.io :as io]
[clojure.tools.logging :as log]
[integrant.core :as ig]))
[integrant.core :as ig])
(:import (java.nio.file Files LinkOption Paths)))

(defn resume-handler
"Useful where you don't want to reset an integrant component in development"
Expand All @@ -11,4 +13,10 @@
(if (= opts old-opts)
old-impl
(do (ig/halt-key! k old-impl)
(ig/init-key k opts))))
(ig/init-key k opts))))

(defn last-modified [filename]
(-> (io/resource filename) (.toURI)
(Paths/get)
(Files/getLastModifiedTime (into-array LinkOption []))
(.toMillis)))
25 changes: 20 additions & 5 deletions libs/kit-sql-conman/src/kit/edge/db/sql/conman.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,23 @@
:or {options {}}}]
(let [filenames (or filenames [filename])
queries (apply conman/bind-connection-map conn options filenames)]
(fn
([query params]
(conman/query queries query params))
([conn query params & opts]
(apply conman/query conn queries query params opts)))))
{:mtimes (map ig-utils/last-modified filenames)
:query-fn (fn
([query params]
(conman/query queries query params))
([conn query params & opts]
(apply conman/query conn queries query params opts)))}))

(defmethod ig/resolve-key :db.sql/query-fn [_ {:keys [query-fn]}]
query-fn)

(defmethod ig/suspend-key! :db.sql/query-fn [_ _])

(defmethod ig/resume-key :db.sql/query-fn
[k {:keys [filename filenames] :as opts} old-opts old-impl]
(if (and (= opts old-opts)
(= (map ig-utils/last-modified (or filenames [filename]))
(:mtimes old-impl)))
old-impl
(do (ig/halt-key! k old-impl)
(ig/init-key k opts))))