From 7a9777419cb576618e3f31c045579d3175824fe1 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Thu, 10 Aug 2023 15:44:45 +0200 Subject: [PATCH] :sparkles: Backport db module improvements from develop --- .clj-kondo/config.edn | 2 +- .clj-kondo/hooks/export.clj | 38 ++++++++++++++++++++++- backend/src/app/db.clj | 60 +++++++++++++++++++++++++++++++++++-- 3 files changed, 96 insertions(+), 4 deletions(-) diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn index f5321b2f6..29d5e04b9 100644 --- a/.clj-kondo/config.edn +++ b/.clj-kondo/config.edn @@ -6,7 +6,6 @@ rumext.v2/defc clojure.core/defn rumext.v2/fnc clojure.core/fn app.common.data/export clojure.core/def - app.db/with-atomic clojure.core/with-open app.common.data.macros/get-in clojure.core/get-in app.common.data.macros/with-open clojure.core/with-open app.common.data.macros/select-keys clojure.core/select-keys @@ -17,6 +16,7 @@ {app.common.data.macros/export hooks.export/export potok.core/reify hooks.export/potok-reify app.util.services/defmethod hooks.export/service-defmethod + app.db/with-atomic hooks.export/penpot-with-atomic }} :output diff --git a/.clj-kondo/hooks/export.clj b/.clj-kondo/hooks/export.clj index f59bd669e..71a837597 100644 --- a/.clj-kondo/hooks/export.clj +++ b/.clj-kondo/hooks/export.clj @@ -39,6 +39,43 @@ other))] {:node result}))) +(defn penpot-with-atomic + [{:keys [node]}] + (let [[_ params & other] (:children node) + + result (if (api/vector-node? params) + (api/list-node + (into [(api/token-node (symbol "clojure.core" "with-open")) params] other)) + (api/list-node + (into [(api/token-node (symbol "clojure.core" "with-open")) + (api/vector-node [params params])] + other))) + + ] + {:node result})) + +(defn penpot-defrecord + [{:keys [:node]}] + (let [[rnode rtype rparams & other] (:children node) + + nodes [(api/token-node (symbol "do")) + (api/list-node + (into [(api/token-node (symbol (name (:value rnode)))) rtype rparams] other)) + (api/list-node + [(api/token-node (symbol "defn")) + (api/token-node (symbol (str "pos->" (:string-value rtype)))) + (api/vector-node + (->> (:children rparams) + (mapv (fn [t] + (api/token-node (symbol (str "_" (:string-value t)))))))) + (api/token-node nil)])] + + result (api/list-node nodes)] + + ;; (prn "=====>" (into {} rparams)) + ;; (prn (api/sexpr result)) + {:node result})) + (defn clojure-specify [{:keys [:node]}] (let [[rnode rtype & other] (:children node) @@ -48,7 +85,6 @@ other))] {:node result})) - (defn service-defmethod [{:keys [:node]}] (let [[rnode rtype ?meta & other] (:children node) diff --git a/backend/src/app/db.clj b/backend/src/app/db.clj index 8beaa78ec..13046c15c 100644 --- a/backend/src/app/db.clj +++ b/backend/src/app/db.clj @@ -5,7 +5,7 @@ ;; Copyright (c) KALEIDOS INC (ns app.db - (:refer-clojure :exclude [get]) + (:refer-clojure :exclude [get run!]) (:require [app.common.data :as d] [app.common.exceptions :as ex] @@ -218,7 +218,13 @@ (defmacro with-atomic [& args] - `(jdbc/with-transaction ~@args)) + (if (symbol? (first args)) + (let [cfgs (first args) + body (rest args)] + `(jdbc/with-transaction [conn# (::pool ~cfgs)] + (let [~cfgs (assoc ~cfgs ::conn conn#)] + ~@body))) + `(jdbc/with-transaction ~@args))) (defn open [pool] @@ -293,6 +299,10 @@ :hint "database object not found")) row)) +(defn plan + [ds sql] + (jdbc/plan ds sql sql/default-opts)) + (defn get-by-id [ds table id & {:as opts}] (get ds table {:id id} opts)) @@ -381,6 +391,52 @@ ([^Connection conn ^Savepoint sp] (.rollback conn sp))) +(defn tx-run! + [cfg f] + (cond + (connection? cfg) + (tx-run! {::conn cfg} f) + + (pool? cfg) + (tx-run! {::pool cfg} f) + + (::conn cfg) + (let [conn (::conn cfg) + sp (savepoint conn)] + (try + (let [result (f cfg)] + (release! conn sp) + result) + (catch Throwable cause + (rollback! sp) + (throw cause)))) + + (::pool cfg) + (with-atomic [conn (::pool cfg)] + (f (assoc cfg ::conn conn))) + + :else + (throw (IllegalArgumentException. "invalid arguments")))) + +(defn run! + [cfg f] + (cond + (connection? cfg) + (run! {::conn cfg} f) + + (pool? cfg) + (run! {::pool cfg} f) + + (::conn cfg) + (f cfg) + + (::pool cfg) + (with-open [^Connection conn (open (::pool cfg))] + (f (assoc cfg ::conn conn))) + + :else + (throw (IllegalArgumentException. "invalid arguments")))) + (defn interval [o] (cond