0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-12 15:51:37 -05:00

Backport db module improvements from develop

This commit is contained in:
Andrey Antukh 2023-08-10 15:44:45 +02:00
parent 28836d82cd
commit 7a9777419c
3 changed files with 96 additions and 4 deletions

View file

@ -6,7 +6,6 @@
rumext.v2/defc clojure.core/defn rumext.v2/defc clojure.core/defn
rumext.v2/fnc clojure.core/fn rumext.v2/fnc clojure.core/fn
app.common.data/export clojure.core/def 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/get-in clojure.core/get-in
app.common.data.macros/with-open clojure.core/with-open app.common.data.macros/with-open clojure.core/with-open
app.common.data.macros/select-keys clojure.core/select-keys app.common.data.macros/select-keys clojure.core/select-keys
@ -17,6 +16,7 @@
{app.common.data.macros/export hooks.export/export {app.common.data.macros/export hooks.export/export
potok.core/reify hooks.export/potok-reify potok.core/reify hooks.export/potok-reify
app.util.services/defmethod hooks.export/service-defmethod app.util.services/defmethod hooks.export/service-defmethod
app.db/with-atomic hooks.export/penpot-with-atomic
}} }}
:output :output

View file

@ -39,6 +39,43 @@
other))] other))]
{:node result}))) {: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 (defn clojure-specify
[{:keys [:node]}] [{:keys [:node]}]
(let [[rnode rtype & other] (:children node) (let [[rnode rtype & other] (:children node)
@ -48,7 +85,6 @@
other))] other))]
{:node result})) {:node result}))
(defn service-defmethod (defn service-defmethod
[{:keys [:node]}] [{:keys [:node]}]
(let [[rnode rtype ?meta & other] (:children node) (let [[rnode rtype ?meta & other] (:children node)

View file

@ -5,7 +5,7 @@
;; Copyright (c) KALEIDOS INC ;; Copyright (c) KALEIDOS INC
(ns app.db (ns app.db
(:refer-clojure :exclude [get]) (:refer-clojure :exclude [get run!])
(:require (:require
[app.common.data :as d] [app.common.data :as d]
[app.common.exceptions :as ex] [app.common.exceptions :as ex]
@ -218,7 +218,13 @@
(defmacro with-atomic (defmacro with-atomic
[& args] [& 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 (defn open
[pool] [pool]
@ -293,6 +299,10 @@
:hint "database object not found")) :hint "database object not found"))
row)) row))
(defn plan
[ds sql]
(jdbc/plan ds sql sql/default-opts))
(defn get-by-id (defn get-by-id
[ds table id & {:as opts}] [ds table id & {:as opts}]
(get ds table {:id id} opts)) (get ds table {:id id} opts))
@ -381,6 +391,52 @@
([^Connection conn ^Savepoint sp] ([^Connection conn ^Savepoint sp]
(.rollback conn 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 (defn interval
[o] [o]
(cond (cond