mirror of
https://github.com/penpot/penpot.git
synced 2025-05-20 16:56:11 +02:00
✨ Improve options handling on db module
This commit is contained in:
parent
6bff6d24b9
commit
0a77bae8a7
5 changed files with 41 additions and 23 deletions
|
@ -268,7 +268,7 @@
|
||||||
(jdbc/execute! sv default-opts)))
|
(jdbc/execute! sv default-opts)))
|
||||||
([ds sv opts]
|
([ds sv opts]
|
||||||
(-> (get-connectable ds)
|
(-> (get-connectable ds)
|
||||||
(jdbc/execute! sv (merge default-opts opts)))))
|
(jdbc/execute! sv (into default-opts (sql/adapt-opts opts))))))
|
||||||
|
|
||||||
(defn exec-one!
|
(defn exec-one!
|
||||||
([ds sv]
|
([ds sv]
|
||||||
|
@ -276,33 +276,31 @@
|
||||||
(jdbc/execute-one! sv default-opts)))
|
(jdbc/execute-one! sv default-opts)))
|
||||||
([ds sv opts]
|
([ds sv opts]
|
||||||
(-> (get-connectable ds)
|
(-> (get-connectable ds)
|
||||||
(jdbc/execute-one! sv
|
(jdbc/execute-one! sv (into default-opts (sql/adapt-opts opts))))))
|
||||||
(-> (merge default-opts opts)
|
|
||||||
(assoc :return-keys (::return-keys? opts false)))))))
|
|
||||||
|
|
||||||
(defn insert!
|
(defn insert!
|
||||||
[ds table params & {:as opts}]
|
[ds table params & {:as opts :keys [::return-keys?] :or {return-keys? true}}]
|
||||||
(-> (get-connectable ds)
|
(-> (get-connectable ds)
|
||||||
(exec-one! (sql/insert table params opts)
|
(exec-one! (sql/insert table params opts)
|
||||||
(merge {::return-keys? true} opts))))
|
(assoc opts ::return-keys? return-keys?))))
|
||||||
|
|
||||||
(defn insert-multi!
|
(defn insert-multi!
|
||||||
[ds table cols rows & {:as opts}]
|
[ds table cols rows & {:as opts :keys [::return-keys?] :or {return-keys? true}}]
|
||||||
(-> (get-connectable ds)
|
(-> (get-connectable ds)
|
||||||
(exec! (sql/insert-multi table cols rows opts)
|
(exec! (sql/insert-multi table cols rows opts)
|
||||||
(merge {::return-keys? true} opts))))
|
(assoc opts ::return-keys? return-keys?))))
|
||||||
|
|
||||||
(defn update!
|
(defn update!
|
||||||
[ds table params where & {:as opts}]
|
[ds table params where & {:as opts :keys [::return-keys?] :or {return-keys? true}}]
|
||||||
(-> (get-connectable ds)
|
(-> (get-connectable ds)
|
||||||
(exec-one! (sql/update table params where opts)
|
(exec-one! (sql/update table params where opts)
|
||||||
(merge {::return-keys? true} opts))))
|
(assoc opts ::return-keys? return-keys?))))
|
||||||
|
|
||||||
(defn delete!
|
(defn delete!
|
||||||
[ds table params & {:as opts}]
|
[ds table params & {:as opts :keys [::return-keys?] :or {return-keys? true}}]
|
||||||
(-> (get-connectable ds)
|
(-> (get-connectable ds)
|
||||||
(exec-one! (sql/delete table params opts)
|
(exec-one! (sql/delete table params opts)
|
||||||
(merge {::return-keys? true} opts))))
|
(assoc opts ::return-keys? return-keys?))))
|
||||||
|
|
||||||
(defn is-row-deleted?
|
(defn is-row-deleted?
|
||||||
[{:keys [deleted-at]}]
|
[{:keys [deleted-at]}]
|
||||||
|
@ -416,10 +414,14 @@
|
||||||
(.releaseSavepoint conn sp))
|
(.releaseSavepoint conn sp))
|
||||||
|
|
||||||
(defn rollback!
|
(defn rollback!
|
||||||
([^Connection conn]
|
([conn]
|
||||||
(.rollback conn))
|
(let [^Connection conn (get-connection conn)]
|
||||||
([^Connection conn ^Savepoint sp]
|
(l/trc :hint "explicit rollback requested")
|
||||||
(.rollback conn sp)))
|
(.rollback conn)))
|
||||||
|
([conn ^Savepoint sp]
|
||||||
|
(let [^Connection conn (get-connection conn)]
|
||||||
|
(l/trc :hint "explicit rollback requested")
|
||||||
|
(.rollback conn sp))))
|
||||||
|
|
||||||
(defn tx-run!
|
(defn tx-run!
|
||||||
[system f & params]
|
[system f & params]
|
||||||
|
@ -446,7 +448,6 @@
|
||||||
(let [system (assoc system ::conn conn)
|
(let [system (assoc system ::conn conn)
|
||||||
result (apply f system params)]
|
result (apply f system params)]
|
||||||
(when (::rollback system)
|
(when (::rollback system)
|
||||||
(l/dbg :hint "explicit rollback requested")
|
|
||||||
(rollback! conn))
|
(rollback! conn))
|
||||||
result))
|
result))
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
(:refer-clojure :exclude [update])
|
(:refer-clojure :exclude [update])
|
||||||
(:require
|
(:require
|
||||||
[app.db :as-alias db]
|
[app.db :as-alias db]
|
||||||
|
[clojure.set :as set]
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
[next.jdbc.optional :as jdbc-opt]
|
[next.jdbc.optional :as jdbc-opt]
|
||||||
[next.jdbc.sql.builder :as sql]))
|
[next.jdbc.sql.builder :as sql]))
|
||||||
|
@ -19,6 +20,14 @@
|
||||||
{:table-fn snake-case
|
{:table-fn snake-case
|
||||||
:column-fn snake-case})
|
:column-fn snake-case})
|
||||||
|
|
||||||
|
(def params-mapping
|
||||||
|
{::db/return-keys? :return-keys
|
||||||
|
::db/columns :columns})
|
||||||
|
|
||||||
|
(defn adapt-opts
|
||||||
|
[opts]
|
||||||
|
(set/rename-keys opts params-mapping))
|
||||||
|
|
||||||
(defn as-kebab-maps
|
(defn as-kebab-maps
|
||||||
[rs opts]
|
[rs opts]
|
||||||
(jdbc-opt/as-unqualified-modified-maps rs (assoc opts :label-fn kebab-case)))
|
(jdbc-opt/as-unqualified-modified-maps rs (assoc opts :label-fn kebab-case)))
|
||||||
|
@ -29,7 +38,7 @@
|
||||||
([table key-map opts]
|
([table key-map opts]
|
||||||
(let [opts (merge default-opts opts)
|
(let [opts (merge default-opts opts)
|
||||||
opts (cond-> opts
|
opts (cond-> opts
|
||||||
(:on-conflict-do-nothing opts)
|
(::db/on-conflict-do-nothing? opts)
|
||||||
(assoc :suffix "ON CONFLICT DO NOTHING"))]
|
(assoc :suffix "ON CONFLICT DO NOTHING"))]
|
||||||
(sql/for-insert table key-map opts))))
|
(sql/for-insert table key-map opts))))
|
||||||
|
|
||||||
|
@ -44,6 +53,7 @@
|
||||||
([table where-params opts]
|
([table where-params opts]
|
||||||
(let [opts (merge default-opts opts)
|
(let [opts (merge default-opts opts)
|
||||||
opts (cond-> opts
|
opts (cond-> opts
|
||||||
|
(::db/columns opts) (assoc :columns (::db/columns opts))
|
||||||
(::db/for-update? opts) (assoc :suffix "FOR UPDATE")
|
(::db/for-update? opts) (assoc :suffix "FOR UPDATE")
|
||||||
(::db/for-share? opts) (assoc :suffix "FOR KEY SHARE")
|
(::db/for-share? opts) (assoc :suffix "FOR KEY SHARE")
|
||||||
(:for-update opts) (assoc :suffix "FOR UPDATE")
|
(:for-update opts) (assoc :suffix "FOR UPDATE")
|
||||||
|
@ -54,7 +64,13 @@
|
||||||
([table key-map where-params]
|
([table key-map where-params]
|
||||||
(update table key-map where-params nil))
|
(update table key-map where-params nil))
|
||||||
([table key-map where-params opts]
|
([table key-map where-params opts]
|
||||||
(let [opts (merge default-opts opts)]
|
(let [opts (into default-opts opts)
|
||||||
|
opts (if-let [columns (::db/columns opts)]
|
||||||
|
(let [columns (if (seq columns)
|
||||||
|
(sql/as-cols columns opts)
|
||||||
|
"*")]
|
||||||
|
(assoc opts :suffix (str "RETURNING " columns)))
|
||||||
|
opts)]
|
||||||
(sql/for-update table key-map where-params opts))))
|
(sql/for-update table key-map where-params opts))))
|
||||||
|
|
||||||
(defn delete
|
(defn delete
|
||||||
|
|
|
@ -899,7 +899,7 @@
|
||||||
(assoc :file-id file-id)
|
(assoc :file-id file-id)
|
||||||
(d/update-when :media-id lookup-index)
|
(d/update-when :media-id lookup-index)
|
||||||
(d/update-when :thumbnail-id lookup-index))
|
(d/update-when :thumbnail-id lookup-index))
|
||||||
{:on-conflict-do-nothing overwrite?}))))
|
{::db/on-conflict-do-nothing? overwrite?}))))
|
||||||
|
|
||||||
(doseq [item (:thumbnails @*state*)]
|
(doseq [item (:thumbnails @*state*)]
|
||||||
(let [item (update item :media-id lookup-index)]
|
(let [item (update item :media-id lookup-index)]
|
||||||
|
@ -909,7 +909,7 @@
|
||||||
:object-id (:object-id item)
|
:object-id (:object-id item)
|
||||||
::l/sync? true)
|
::l/sync? true)
|
||||||
(db/insert! conn :file-tagged-object-thumbnail item
|
(db/insert! conn :file-tagged-object-thumbnail item
|
||||||
{:on-conflict-do-nothing overwrite?})))))
|
{::db/on-conflict-do-nothing? overwrite?})))))
|
||||||
|
|
||||||
(defn- lookup-index
|
(defn- lookup-index
|
||||||
[id]
|
[id]
|
||||||
|
|
|
@ -729,7 +729,8 @@
|
||||||
(role->params role))]
|
(role->params role))]
|
||||||
|
|
||||||
;; Insert the invited member to the team
|
;; Insert the invited member to the team
|
||||||
(db/insert! conn :team-profile-rel params {:on-conflict-do-nothing true})
|
(db/insert! conn :team-profile-rel params
|
||||||
|
{::db/on-conflict-do-nothing? true})
|
||||||
|
|
||||||
;; If profile is not yet verified, mark it as verified because
|
;; If profile is not yet verified, mark it as verified because
|
||||||
;; accepting an invitation link serves as verification.
|
;; accepting an invitation link serves as verification.
|
||||||
|
|
|
@ -105,7 +105,7 @@
|
||||||
::quotes/team-id team-id})
|
::quotes/team-id team-id})
|
||||||
|
|
||||||
;; Insert the invited member to the team
|
;; Insert the invited member to the team
|
||||||
(db/insert! conn :team-profile-rel params {:on-conflict-do-nothing true})
|
(db/insert! conn :team-profile-rel params {::db/on-conflict-do-nothing? true})
|
||||||
|
|
||||||
;; If profile is not yet verified, mark it as verified because
|
;; If profile is not yet verified, mark it as verified because
|
||||||
;; accepting an invitation link serves as verification.
|
;; accepting an invitation link serves as verification.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue