Browse Source

refactor: db migrate

Tienson Qin 4 năm trước cách đây
mục cha
commit
15dd3c46a7

+ 1 - 1
src/main/frontend/components/block.cljs

@@ -1557,7 +1557,7 @@
                                    (remove nil?)))]
                     (text/build-data-value refs))
         data-refs-self (let [refs  (model/get-page-names-by-ids
-                                    (->> (map :db/id (:block/ref-pages block))
+                                    (->> (map :db/id (:block/refs block))
                                          (remove nil?)))]
                          (text/build-data-value refs))]
     [:div.ls-block.flex.flex-col.rounded-sm

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
src/main/frontend/config.cljs


+ 1 - 1
src/main/frontend/db/debug.cljs

@@ -33,7 +33,7 @@
                         {:block/children ...}])
         :in $ ?pages
         :where
-        [?b :block/ref-pages ?ref-page]
+        [?b :block/refs ?ref-page]
         [(contains? ?pages ?ref-page)]]
       (get-conn)
       page-and-aliases)

+ 18 - 18
src/main/frontend/db/model.cljs

@@ -362,7 +362,7 @@
   (->> (d/q
         '[:find ?id2 ?id1
           :where
-          [?id1 :block/ref-blocks ?id2]]
+          [?id1 :block/refs ?id2]]
         (conn/get-conn repo))
        (map first)
        (frequencies)))
@@ -832,7 +832,7 @@
                                     :where
                                     [?block :block/page ?p]
                                     [(contains? ?pages ?p)]
-                                    [?block :block/ref-pages ?ref-page]
+                                    [?block :block/refs ?ref-page]
                                     [?ref-page :block/name ?ref-page-name]]
                                   pages)
                          react
@@ -848,8 +848,8 @@
          :in $ % ?page
          :where
          [?p :block/name ?page]
-         [?b :block/path-ref-pages ?p]
-         [?b :block/ref-pages ?other-p]
+         [?b :block/path-refs ?p]
+         [?b :block/refs ?other-p]
          [(not= ?p ?other-p)]
          [?other-p :block/name ?ref-page]]
        conn
@@ -883,14 +883,14 @@
                 :where
                 [?p :block/name ?page]
                 [?block :block/page ?p]
-                [?block :block/ref-pages ?ref-page]
+                [?block :block/refs ?ref-page]
                 [?ref-page :block/name ?ref-page-name]]
               '[:find ?page ?ref-page-name
                 :where
                 [?p :block/journal? false]
                 [?p :block/name ?page]
                 [?block :block/page ?p]
-                [?block :block/ref-pages ?ref-page]
+                [?block :block/refs ?ref-page]
                 [?ref-page :block/name ?ref-page-name]])]
       (->>
        (d/q q conn)
@@ -907,7 +907,7 @@
                                         '[:find ?mentioned-page-name
                                           :in $ ?pages ?page-name
                                           :where
-                                          [?block :block/ref-pages ?p]
+                                          [?block :block/refs ?p]
                                           [(contains? ?pages ?p)]
                                           [?block :block/page ?mentioned-page]
                                           [?mentioned-page :block/name ?mentioned-page-name]]
@@ -938,7 +938,7 @@
                      :where
                      (parent ?p ?b)
                      [(contains? ?block-ids ?p)]
-                     [?b :block/ref-pages ?ref]]
+                     [?b :block/refs ?ref]]
                    conn
                    rules
                    block-ids)
@@ -961,7 +961,7 @@
                                            [?block :block/page ?alias]
                                            [(contains? ?aliases ?alias)]]
                                           [(find-blocks ?block ?ref-page ?pages ?alias ?aliases)
-                                           [?block :block/ref-pages ?ref-page]
+                                           [?block :block/refs ?ref-page]
                                            [(contains? ?pages ?ref-page)]]]]
                               (react/q repo [:page/refed-blocks page-id] {}
                                        '[:find (pull ?block [*])
@@ -975,7 +975,7 @@
                                      '[:find (pull ?block [*])
                                        :in $ ?pages
                                        :where
-                                       [?block :block/ref-pages ?ref-page]
+                                       [?block :block/refs ?ref-page]
                                        [(contains? ?pages ?ref-page)]]
                                      pages))
              result (->> query-result
@@ -1020,7 +1020,7 @@
             '[:find ?path
               :in $ ?page-id
               :where
-              [?block :block/ref-pages ?page-id]
+              [?block :block/refs ?page-id]
               [?block :block/page ?p]
               [?p :block/file ?f]
               [?f :file/path ?path]]
@@ -1045,7 +1045,7 @@
               pattern)
              db-utils/seq-flatten
              (remove (fn [block]
-                       (let [ref-pages (set (map :db/id (:block/ref-pages block)))]
+                       (let [ref-pages (set (map :db/id (:block/refs block)))]
                          (or
                           (= (get-in block [:block/page :db/id]) page-id)
                           (seq (set/intersection
@@ -1065,7 +1065,7 @@
                       :in $ ?block-uuid
                       :where
                       [?block :block/uuid ?block-uuid]
-                      [?ref-block :block/ref-blocks ?block]]
+                      [?ref-block :block/refs ?block]]
                     block-uuid)
            react
            db-utils/seq-flatten
@@ -1293,17 +1293,17 @@
   (d/q '[:find [(pull ?b [*]) ...]
          :in $ % ?refs
          :where
-         [?b :block/ref-pages ?p]
+         [?b :block/refs ?p]
          ;; Filter other blocks
          [(contains? ?refs ?p)]
          (or-join [?b ?refs]
-                  (matches-all ?b :block/ref-pages ?refs)
+                  (matches-all ?b :block/refs ?refs)
                   (and
                    (parent ?p ?b)
                    ;; FIXME: not working
-                   ;; (matches-all (union ?p ?b) :block/ref-pages ?refs)
-                   [?p :block/ref-pages ?p-ref]
-                   [?b :block/ref-pages ?b-ref]
+                   ;; (matches-all (union ?p ?b) :block/refs ?refs)
+                   [?p :block/refs ?p-ref]
+                   [?b :block/refs ?b-ref]
                    [(not= ?p-ref ?b-ref)]
                    [(contains? ?refs ?p-ref)]
                    [(contains? ?refs ?b-ref)]))]

+ 1 - 1
src/main/frontend/db/query_dsl.cljs

@@ -146,7 +146,7 @@
        page-ref?
        (let [page-name (-> (text/page-ref-un-brackets! e)
                            (string/lower-case))]
-         [['?b :block/path-ref-pages [:block/name page-name]]])
+         [['?b :block/path-refs [:block/name page-name]]])
 
        (contains? #{'and 'or 'not} fe)
        (let [clauses (->> (map (fn [form]

+ 12 - 20
src/main/frontend/db_schema.cljs

@@ -37,12 +37,12 @@
    ;; belongs to which page
    :block/page {:db/valueType :db.type/ref
                 :db/index true}
-   ;; referenced pages
-   :block/ref-pages {:db/valueType :db.type/ref
-                     :db/cardinality :db.cardinality/many}
+   ;; reference blocks
+   :block/refs {:db/valueType :db.type/ref
+                :db/cardinality :db.cardinality/many}
    ;; referenced pages inherited from the parents
-   :block/path-ref-pages {:db/valueType   :db.type/ref
-                          :db/cardinality :db.cardinality/many}
+   :block/path-refs {:db/valueType   :db.type/ref
+                     :db/cardinality :db.cardinality/many}
 
    :block/tags {:db/valueType :db.type/ref
                 :db/cardinality :db.cardinality/many}
@@ -50,14 +50,6 @@
    :block/alias {: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}
-   :block/embed-blocks {:db/valueType :db.type/ref
-                        :db/cardinality :db.cardinality/many}
-
-   :block/embed-pages {:db/valueType :db.type/ref
-                       :db/cardinality :db.cardinality/many}
    :block/content {}
    :block/marker {}
    :block/priority {}
@@ -90,10 +82,10 @@
 
    ;; file
    :file/path {:db/unique :db.unique/identity}
-   :file/created-at {}
-   :file/last-modified-at {}
-   :file/size {}
-   :file/handle {}
+   ;; :file/created-at {}
+   ;; :file/last-modified-at {}
+   ;; :file/size {}
+   ;; :file/handle {}
 
    ;; git
    :repo/cloned? {}
@@ -122,9 +114,9 @@
    :block/title {}
    :block/content {}
    :block/properties {}
-   :block/ref-blocks {:db/valueType :db.type/ref
-                      :db/cardinality :db.cardinality/many}
-   :block/embed-blocks {:db/cardinality :db.cardinality/many}
+   :block/refs {:db/valueType :db.type/ref
+                :db/cardinality :db.cardinality/many}
+   :block/embeds {:db/cardinality :db.cardinality/many}
    :block/created-at {}
    :block/updated-at {}
    :block/alias {:db/cardinality :db.cardinality/many}

+ 8 - 8
src/main/frontend/format/block.cljs

@@ -297,27 +297,27 @@
             [path-refs parents]
             (cond
               (zero? level-diff)            ; sibling
-              (let [path-refs (mapcat :block/ref-pages (drop-last parents))
+              (let [path-refs (mapcat :block/refs (drop-last parents))
                     parents (conj (vec (butlast parents)) block)]
                 [path-refs parents])
 
               (> level-diff 0)              ; child
-              (let [path-refs (mapcat :block/ref-pages parents)]
+              (let [path-refs (mapcat :block/refs parents)]
                 [path-refs (conj parents block)])
 
               (< level-diff 0)              ; new parent
               (let [parents (vec (take-while (fn [p] (< (:block/level p) cur-level)) parents))
-                    path-refs (mapcat :block/ref-pages parents)]
+                    path-refs (mapcat :block/refs parents)]
                 [path-refs (conj parents block)]))
             path-ref-pages (->> path-refs
-                                (concat (:block/ref-pages block))
+                                (concat (:block/refs block))
                                 (remove string/blank?)
                                 (map string/lower-case)
                                 (distinct)
                                 (map (fn [p]
                                        {:block/name p})))]
         (recur (rest blocks)
-               (conj acc (assoc block :block/path-ref-pages path-ref-pages))
+               (conj acc (assoc block :block/path-refs path-ref-pages))
                parents)))))
 
 (defn block-tags->pages
@@ -454,7 +454,7 @@
            blocks (extract-blocks ast content-length encoded-content)
            ref-pages-atom (atom [])
            parent-ref-pages (->> (db/get-block-parent (state/get-current-repo) uuid)
-                                 :block/path-ref-pages
+                                 :block/path-refs
                                  (map :db/id))
            blocks (doall
                    (map-indexed
@@ -475,12 +475,12 @@
                                     :block/content (utf8/substring encoded-content
                                                                    (:start-pos meta)
                                                                    (:end-pos meta))
-                                    :block/path-ref-pages path-ref-pages}
+                                    :block/path-refs path-ref-pages}
                                    ;; Preserve the original block id
                                    (when (zero? idx)
                                      {:block/uuid uuid})
                                    (when (seq ref-pages)
-                                     {:block/ref-pages
+                                     {:block/refs
                                       (mapv
                                        (fn [page]
                                          (let [page (page-name->map page)]

+ 4 - 4
src/main/frontend/handler/block.cljs

@@ -22,7 +22,7 @@
                        (let [refs (if-let [refs (seq (:block/refs-with-children block))]
                                     refs
                                     (concat
-                                     (:block/ref-pages block)
+                                     (:block/refs block)
                                      (:block/tags block)))]
                          (distinct refs)))]
     (loop [col (reverse col)
@@ -198,7 +198,7 @@
   (let [ref-pages (->> (if group-by-page?
                          (mapcat last ref-blocks)
                          ref-blocks)
-                       (mapcat (fn [b] (concat (:block/ref-pages b) (:block/children-refs b))))
+                       (mapcat (fn [b] (concat (:block/refs b) (:block/children-refs b))))
                        (distinct)
                        (map :db/id)
                        (db/pull-many repo '[:db/id :block/name]))
@@ -215,13 +215,13 @@
                        (cond->> ref-blocks
                          (seq exclude-ids)
                          (remove (fn [block]
-                                   (let [ids (set (concat (map :db/id (:block/ref-pages block))
+                                   (let [ids (set (concat (map :db/id (:block/refs block))
                                                           (map :db/id (:block/children-refs block))))]
                                      (seq (set/intersection exclude-ids ids)))))
 
                          (seq include-ids)
                          (remove (fn [block]
-                                   (let [ids (set (concat (map :db/id (:block/ref-pages block))
+                                   (let [ids (set (concat (map :db/id (:block/refs block))
                                                           (map :db/id (:block/children-refs block))))]
                                      (empty? (set/intersection include-ids ids)))))
                          ))]

+ 2 - 4
src/main/frontend/handler/editor.cljs

@@ -321,10 +321,8 @@
                                      done
                                      (conj done (:db/id current))))
                                  [] old-ref-blocks)]
-    ;; removes retracted pages and blocks
-    (into
-     (mapv (fn [ref] [:db/retract eid :block/ref-pages ref]) retracted-pages)
-     (mapv (fn [ref] [:db/retract eid :block/ref-blocks ref]) retracted-blocks))))
+    ;; removes refs
+    (mapv (fn [ref] [:db/retract eid :block/refs ref]) retracted-pages)))
 
 (defn- block-with-title
   [content format]

+ 5 - 7
src/main/frontend/handler/extract.cljs

@@ -36,8 +36,8 @@
                    (fn [[page blocks]]
                      (if page
                        (map (fn [block]
-                              (let [block-ref-pages (seq (:block/ref-pages block))
-                                    block-path-ref-pages (seq (:block/path-ref-pages block))]
+                              (let [block-ref-pages (seq (:block/refs block))
+                                    block-path-ref-pages (seq (:block/path-refs block))]
                                 (when block-ref-pages
                                   (swap! ref-pages set/union (set block-ref-pages)))
                                 (-> block
@@ -46,11 +46,11 @@
                                            :block/file [:file/path file]
                                            :block/format format
                                            :block/page [:block/name (string/lower-case page)]
-                                           :block/ref-pages (mapv
+                                           :block/refs (mapv
                                                              (fn [page]
                                                                (block/page-name->map page))
                                                              block-ref-pages)
-                                           :block/path-ref-pages block-path-ref-pages))))
+                                           :block/path-refs block-path-ref-pages))))
                             blocks)))
                    (remove nil? pages)))
           pages (doall
@@ -164,7 +164,5 @@
               ;; To prevent "unique constraint" on datascript
               pages-index (map #(select-keys % [:block/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)]
+                            (update b :block/refs #(set/intersection (set %) block-ids-set))) blocks)]
           (apply concat [pages-index pages block-ids blocks]))))))

+ 16 - 16
src/test/frontend/db/query_dsl_test.cljs

@@ -136,11 +136,11 @@ parent: child page 2
   (testing "Single page query"
     (are [x y] (= (q-count x) y)
       "[[page 1]]"
-      {:query '[[?b :block/path-ref-pages [:block/name "page 1"]]]
+      {:query '[[?b :block/path-refs [:block/name "page 1"]]]
        :count 6}
 
       "[[page 2]]"
-      {:query '[[?b :block/path-ref-pages [:block/name "page 2"]]]
+      {:query '[[?b :block/path-refs [:block/name "page 2"]]]
        :count 4}))
 
   (testing "Block properties query"
@@ -327,29 +327,29 @@ parent: child page 2
   (testing "AND queries"
     (are [x y] (= (q-count x) y)
       "(and [[tag1]] [[page 2]])"
-      {:query '([?b :block/path-ref-pages [:block/name "tag1"]]
-                [?b :block/path-ref-pages [:block/name "page 2"]])
+      {:query '([?b :block/path-refs [:block/name "tag1"]]
+                [?b :block/path-refs [:block/name "page 2"]])
        :count 1})
 
     (are [x y] (= (q-count x) y)
       "(and [[tag1]] [[page 2]])"
-      {:query '([?b :block/path-ref-pages [:block/name "tag1"]]
-                [?b :block/path-ref-pages [:block/name "page 2"]])
+      {:query '([?b :block/path-refs [:block/name "tag1"]]
+                [?b :block/path-refs [:block/name "page 2"]])
        :count 1}))
 
   (testing "OR queries"
     (are [x y] (= (q-count x) y)
       "(or [[tag1]] [[page 2]])"
       {:query '(or
-                (and [?b :block/path-ref-pages [:block/name "tag1"]])
-                (and [?b :block/path-ref-pages [:block/name "page 2"]]))
+                (and [?b :block/path-refs [:block/name "tag1"]])
+                (and [?b :block/path-refs [:block/name "page 2"]]))
        :count 4}))
 
   (testing "NOT queries"
     (are [x y] (= (q-count x) y)
       "(not [[page 1]])"
       {:query '([?b :block/uuid]
-                (not [?b :block/path-ref-pages [:block/name "page 1"]]))
+                (not [?b :block/path-refs [:block/name "page 1"]]))
        :count 8}))
 
   (testing "Between query"
@@ -375,15 +375,15 @@ parent: child page 2
       {:query '([?b :block/uuid]
                 [?b :block/marker ?marker]
                 [(contains? #{"DONE"} ?marker)]
-                (not [?b :block/path-ref-pages [:block/name "page 1"]]))
+                (not [?b :block/path-refs [:block/name "page 1"]]))
        :count 0})
 
     (are [x y] (= (q-count x) y)
       "(and (todo now later) (or [[page 1]] [[page 2]]))"
       {:query '([?b :block/marker ?marker]
                 [(contains? #{"NOW" "LATER"} ?marker)]
-                (or (and [?b :block/path-ref-pages [:block/name "page 1"]])
-                    (and [?b :block/path-ref-pages [:block/name "page 2"]])))
+                (or (and [?b :block/path-refs [:block/name "page 1"]])
+                    (and [?b :block/path-refs [:block/name "page 2"]])))
        :count 3})
 
     (are [x y] (= (q-count x) y)
@@ -393,8 +393,8 @@ parent: child page 2
                  [?b :block/marker ?marker]
                  [(contains? #{"NOW" "LATER"} ?marker)]
                  (or
-                  (and [?b :block/path-ref-pages [:block/name "page 1"]])
-                  (and [?b :block/path-ref-pages [:block/name "page 2"]]))))
+                  (and [?b :block/path-refs [:block/name "page 1"]])
+                  (and [?b :block/path-refs [:block/name "page 2"]]))))
        :count 11})
 
     ;; FIXME: not working
@@ -412,8 +412,8 @@ parent: child page 2
                 [?b :block/marker ?marker]
                 [(contains? #{"NOW" "LATER" "DONE"} ?marker)]
                 (or
-                 (and [?b :block/path-ref-pages [:block/name "page 1"]])
-                 (and (not [?b :block/path-ref-pages [:block/name "page 1"]]))))
+                 (and [?b :block/path-refs [:block/name "page 1"]])
+                 (and (not [?b :block/path-refs [:block/name "page 1"]]))))
        :count 5}))
 
   (testing "sort-by (created_at defaults to desc)"

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác