enhance: backup when add is notified and db content is not empty

This commit is contained in:
Tienson Qin
2022-01-04 17:36:17 +08:00
parent 351b126de7
commit ec289c788f

View File

@@ -25,6 +25,16 @@
(when-not (= (str id-property) (str block-id))
(editor/set-block-property! block-id "id" block-id)))))))
(defn- handle-add-and-change!
[repo path content db-content mtime backup?]
(p/let [
;; save the previous content in a bak file to avoid data overwritten.
_ (when backup? (ipc/ipc "backupDbFile" (config/get-local-dir repo) path db-content))
_ (file-handler/alter-file repo path content {:re-render-root? true
:from-disk? true})]
(set-missing-block-ids! content)
(db/set-file-last-modified-at! repo path mtime)))
(defn handle-changed!
[type {:keys [dir path content stat] :as payload}]
(when dir
@@ -38,10 +48,8 @@
(and (= "add" type)
(not= (string/trim content) (string/trim db-content))
(not (string/includes? path "logseq/pages-metadata.edn")))
(p/let [_ (file-handler/alter-file repo path content {:re-render-root? true
:from-disk? true})]
(set-missing-block-ids! content)
(db/set-file-last-modified-at! repo path mtime))
(let [backup? (not (string/blank? db-content))]
(handle-add-or-changed! repo path content db-content mtime backup?))
(and (= "change" type)
(not (db/file-exists? repo path)))
@@ -57,13 +65,7 @@
(string/trim (or (state/get-default-journal-template) "")))
(= (string/trim content) "-")
(= (string/trim content) "*")))
(p/let [
;; save the previous content in a bak file to avoid data overwritten.
_ (ipc/ipc "backupDbFile" (config/get-local-dir repo) path db-content)
_ (file-handler/alter-file repo path content {:re-render-root? true
:from-disk? true})]
(set-missing-block-ids! content)
(db/set-file-last-modified-at! repo path mtime)))
(handle-add-or-changed! repo path content db-content mtime true))
(and (= "unlink" type)
(db/file-exists? repo path))