Skip to content

Commit e6e61e2

Browse files
committed
fix: cleanup to process all retracted entities (including pages)
related to logseq/db-test#726 related to logseq/db-test#752
1 parent 3c9dee9 commit e6e61e2

2 files changed

Lines changed: 33 additions & 12 deletions

File tree

deps/db/src/logseq/db/common/delete_blocks.cljs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,23 @@
4646
refs)))
4747

4848
(defn update-refs-history
49-
"When a block is deleted, a block's refs are updated and related property history, views and reactions
49+
"When an entity is deleted, related property history, views and reactions
5050
are deleted"
5151
[db txs _opts]
52-
(let [retracted-block-ids (->> (keep (fn [tx]
53-
(when (and (vector? tx)
54-
(contains? #{:db.fn/retractEntity :db/retractEntity} (first tx)))
55-
(second tx))) txs)
56-
(filter (fn [id]
57-
(not (entity-util/page? (d/entity db id))))))]
58-
(when (seq retracted-block-ids)
59-
(let [retracted-blocks (map #(d/entity db %) retracted-block-ids)
60-
reaction-entities (->> retracted-blocks
52+
(let [retracted-ids (keep (fn [tx]
53+
(when (and (vector? tx)
54+
(contains? #{:db.fn/retractEntity :db/retractEntity} (first tx)))
55+
(second tx))) txs)
56+
retracted-entities (map #(d/entity db %) retracted-ids)]
57+
(when (seq retracted-ids)
58+
(let [retracted-blocks (remove entity-util/page? retracted-entities)
59+
reaction-entities (->> retracted-entities
6160
(mapcat :logseq.property.reaction/_target)
6261
(common-util/distinct-by :db/id))
6362
retract-reactions-tx (map (fn [reaction] [:db/retractEntity (:db/id reaction)])
6463
reaction-entities)
6564
retracted-tx (build-retracted-tx retracted-blocks)
66-
history-entities (->> retracted-blocks
65+
history-entities (->> retracted-entities
6766
(mapcat (fn [e]
6867
(concat (:logseq.property.history/_block e)
6968
(:logseq.property.history/_ref-value e))))
@@ -75,6 +74,6 @@
7574
(fn [item]
7675
(let [block (d/entity db (:db/id item))]
7776
(:logseq.property/_view-for block)))
78-
retracted-blocks)
77+
retracted-entities)
7978
(map (fn [b] [:db/retractEntity (:db/id b)])))]
8079
(concat retracted-tx delete-views retract-history-tx retract-reactions-tx)))))

deps/db/test/logseq/db/common/delete_blocks_test.cljs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(:require [cljs.test :refer [deftest is testing]]
33
[datascript.core :as d]
44
[logseq.common.util :as common-util]
5+
[logseq.db :as ldb]
56
[logseq.db.common.delete-blocks :as delete-blocks]
67
[logseq.db.test.helper :as db-test]))
78

@@ -47,3 +48,24 @@
4748
extra (delete-blocks/update-refs-history @conn retracts {})]
4849
(d/transact! conn (concat retracts extra))
4950
(is (nil? (d/entity @conn (:db/id history-entity)))))))
51+
52+
(deftest delete-page-removes-history-with-ref-value
53+
(testing "deleting a page retracts property history entries that referenced that page"
54+
(let [conn (db-test/create-conn-with-blocks
55+
{:pages-and-blocks
56+
[{:page {:block/title "Journal"}
57+
:blocks [{:block/title "b1"}]}
58+
{:page {:block/title "Page to delete"}}]})
59+
block (db-test/find-block-by-content @conn "b1")
60+
page (db-test/find-page-by-title @conn "Page to delete")
61+
history-uuid (random-uuid)
62+
now (common-util/time-ms)
63+
_ (d/transact! conn [{:block/uuid history-uuid
64+
:block/created-at now
65+
:block/updated-at now
66+
:logseq.property.history/block (:db/id block)
67+
:logseq.property.history/property (:db/id (d/entity @conn :logseq.property/status))
68+
:logseq.property.history/ref-value (:db/id page)}])
69+
history-entity (d/entity @conn [:block/uuid history-uuid])]
70+
(ldb/transact! conn [[:db/retractEntity (:db/id page)]])
71+
(is (nil? (d/entity @conn (:db/id history-entity)))))))

0 commit comments

Comments
 (0)