fix(db): fix unique constraint issue when loading files

pull/1058/head
Tienson Qin 2021-01-04 15:15:54 +08:00
parent 951ed57f21
commit 89370ac645
5 changed files with 28 additions and 8 deletions

View File

@ -950,9 +950,11 @@
:span
{:class "block-tags"}
(mapv (fn [tag]
[:a.tag.mx-1 {:key (str "tag-" tag)
:href (rfe/href :page {:name tag})}
(str "#" tag)])
(when-let [page (db/entity (:db/id tag))]
(let [tag (:page/name page)]
[:a.tag.mx-1 {:key (str "tag-" (:db/id tag))
:href (rfe/href :page {:name tag})}
(str "#" tag)])))
tags))))
(defn build-block-part

View File

@ -64,6 +64,13 @@
;; referenced pages
:block/ref-pages {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}
;; Referenced pages
;; Notice: it's only for org mode, :tag1:tag2:
;; Markdown tags will be only stored in :block/ref-pages
:block/tags {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}
;; referenced blocks
:block/ref-blocks {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}

View File

@ -201,6 +201,13 @@
(assoc :repeated? true))))))]
(apply merge m)))
(defn block-tags->pages
[{:keys [tags] :as block}]
(if (seq tags)
(assoc block :tags (map (fn [tag]
[:page/name (string/lower-case tag)]) tags))
block))
(defn with-page-refs
[{:keys [title body tags ref-pages] :as block}]
(let [ref-pages (->> (concat tags ref-pages)
@ -326,9 +333,11 @@
block (if (seq timestamps)
(merge block (timestamps->scheduled-and-deadline timestamps))
block)
block (with-page-refs block)
block (with-block-refs block)
block (update-src-pos-meta! block)
block (-> block
with-page-refs
with-block-refs
block-tags->pages
update-src-pos-meta!)
last-pos' (get-in block [:meta :start-pos])]
(recur (conj headings block) [] (rest blocks) {} {} last-pos' (:level block) children))

View File

@ -536,6 +536,7 @@
:end-pos new-end-pos})
(block/parse-block block format))
block-retracted-attrs (when-not pre-block?
;; TODO: should we retract the whole block instead?
(when-let [id (:db/id block)]
[[:db/retract id :block/properties]
[:db/retract id :block/priority]
@ -544,7 +545,6 @@
[:db/retract id :block/scheduled]
[:db/retract id :block/scheduled-ast]
[:db/retract id :block/marker]
[:db/retract id :block/tags]
[:db/retract id :block/repeated?]]))
[after-blocks block-children-content new-end-pos] (rebuild-after-blocks-indent-outdent repo file block (:end-pos (:block/meta block)) end-pos indent-left?)
retract-refs (compute-retract-refs (:db/id e) (first blocks) ref-pages ref-blocks)

View File

@ -159,8 +159,10 @@
(when (seq result)
(let [[pages block-ids blocks] (apply map concat result)
block-ids-set (set block-ids)
;; To prevent "unique constraint" on datascript
pages-index (map #(select-keys % [:page/name]) pages)
blocks (map (fn [b]
(-> b
(update :block/ref-blocks #(set/intersection (set %) block-ids-set))
(update :block/embed-blocks #(set/intersection (set %) block-ids-set)))) blocks)]
(apply concat [pages block-ids blocks]))))))
(apply concat [pages-index pages block-ids blocks]))))))