Skip to content

Commit

Permalink
fix: don't write to files when db transaction failed
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Mar 8, 2021
1 parent 5f674ce commit 86250f1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 61 deletions.
76 changes: 36 additions & 40 deletions src/main/frontend/db/react.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -283,46 +283,42 @@
[repo-url tx-data {:keys [key data files-db?] :as handler-opts
:or {files-db? false}}]
(when-not config/publishing?
(try
(let [repo-url (or repo-url (state/get-current-repo))
tx-data (->> (util/remove-nils tx-data)
(remove nil?))
get-conn (fn [] (if files-db?
(conn/get-files-conn repo-url)
(conn/get-conn repo-url false)))]
(when (and (seq tx-data) (get-conn))
(let [tx-result (d/transact! (get-conn) (vec tx-data))
db (:db-after tx-result)
handler-keys (get-handler-keys handler-opts)]
(doseq [handler-key handler-keys]
(let [handler-key (vec (cons repo-url handler-key))]
(when-let [cache (get @query-state handler-key)]
(let [{:keys [query inputs transform-fn query-fn inputs-fn]} cache]
(when (or query query-fn)
(let [new-result (->
(cond
query-fn
(profile
"Query:"
(doall (query-fn db)))

inputs-fn
(let [inputs (inputs-fn)]
(apply d/q query db inputs))

(keyword? query)
(db-utils/get-key-value repo-url query)

(seq inputs)
(apply d/q query db inputs)

:else
(d/q query db))
transform-fn)]
(set-new-result! handler-key new-result))))))))))
(catch js/Error e
;; FIXME: check error type and notice user
(log/error :db/transact! e)))))
(let [repo-url (or repo-url (state/get-current-repo))
tx-data (->> (util/remove-nils tx-data)
(remove nil?))
get-conn (fn [] (if files-db?
(conn/get-files-conn repo-url)
(conn/get-conn repo-url false)))]
(when (and (seq tx-data) (get-conn))
(let [tx-result (d/transact! (get-conn) (vec tx-data))
db (:db-after tx-result)
handler-keys (get-handler-keys handler-opts)]
(doseq [handler-key handler-keys]
(let [handler-key (vec (cons repo-url handler-key))]
(when-let [cache (get @query-state handler-key)]
(let [{:keys [query inputs transform-fn query-fn inputs-fn]} cache]
(when (or query query-fn)
(let [new-result (->
(cond
query-fn
(profile
"Query:"
(doall (query-fn db)))

inputs-fn
(let [inputs (inputs-fn)]
(apply d/q query db inputs))

(keyword? query)
(db-utils/get-key-value repo-url query)

(seq inputs)
(apply d/q query db inputs)

:else
(d/q query db))
transform-fn)]
(set-new-result! handler-key new-result))))))))))))

(defn set-key-value
[repo-url key value]
Expand Down
18 changes: 8 additions & 10 deletions src/main/frontend/handler/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2017,16 +2017,14 @@
file-content (db/get-file file-path)
new-content (utf8/insert! file-content start-pos old-end-pos (apply str (map :block/content blocks)))
blocks (map (fn [b] (dissoc b :block/children)) blocks)]
(profile
"Indent/outdent: "
(repo-handler/transact-react-and-alter-file!
repo
(concat
blocks
after-blocks)
{:key :block/change
:data (map (fn [block] (assoc block :block/page page)) blocks)}
[[file-path new-content]])))
(repo-handler/transact-react-and-alter-file!
repo
(concat
blocks
after-blocks)
{:key :block/change
:data (map (fn [block] (assoc block :block/page page)) blocks)}
[[file-path new-content]]))

(gdom/getElement "date-time-picker")
nil
Expand Down
28 changes: 17 additions & 11 deletions src/main/frontend/handler/repo.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -369,17 +369,23 @@
:idx idx
:container (gobj/get container "id")})))

(when (seq files)
(file-handler/alter-files repo files opts))

(db/transact-react!
repo
tx
transact-option)
(when (seq pages)
(let [children-tx (mapcat #(rebuild-page-blocks-children repo %) pages)]
(when (seq children-tx)
(db/transact! repo children-tx)))))))
;; try catch so that if db transaction failed, it'll not write to the files
(try
(do
(db/transact-react!
repo
tx
transact-option)

(when (seq pages)
(let [children-tx (mapcat #(rebuild-page-blocks-children repo %) pages)]
(when (seq children-tx)
(db/transact! repo children-tx))))

(when (seq files)
(file-handler/alter-files repo files opts)))
(catch js/Error e
(log/error :transact-react/failed e))))))

(declare push)

Expand Down

0 comments on commit 86250f1

Please sign in to comment.