fix: remove datoms when uploading if eid or value ref deleted

This commit is contained in:
Tienson Qin
2026-01-20 17:13:33 +08:00
parent 4ec99c8f17
commit d0cd12a034
3 changed files with 37 additions and 38 deletions

View File

@@ -10,14 +10,12 @@
[logseq.common.path :as path]
[logseq.common.util :as common-util]
[logseq.db :as ldb]
[logseq.db-sync.compare :as sync-compare]
[logseq.db-sync.cycle :as sync-cycle]
[logseq.db-sync.malli-schema :as db-sync-schema]
[logseq.db-sync.order :as sync-order]
[logseq.db.common.normalize :as db-normalize]
[logseq.db.sqlite.util :as sqlite-util]
[logseq.outliner.core :as outliner-core]
[logseq.outliner.pipeline :as outliner-pipeline]
[logseq.outliner.transaction :as outliner-tx]
[promesa.core :as p]))
@@ -327,10 +325,11 @@
(let [tx-ids (mapv :tx-id batch)
txs (mapcat :tx batch)
tx-data (->> txs
db-normalize/remove-retract-entity-ref
keep-last-update
distinct)]
;; (prn :debug :before-keep-last-update txs)
;; (prn :debug :upload :tx-data tx-data)
(prn :debug :upload :tx-data tx-data)
(when (seq txs)
(reset! (:inflight client) tx-ids)
(send! ws {:type "tx/batch"

View File

@@ -107,48 +107,49 @@
(defn- fix-page-tags
"Add missing attributes and remove #Page when inserting or updating block/title with inline tags"
[{:keys [db-after tx-data]}]
(let [page-tag (d/entity db-after :logseq.class/Page)
tag (d/entity db-after :logseq.class/Tag)]
(assert page-tag "Page tag doesn't exist")
(mapcat
(fn [datom]
(when (and (= :block/tags (:a datom))
(:added datom))
(let [entity (d/entity db-after (:e datom))
v-entity (d/entity db-after (:v datom))]
(cond
[{:keys [db-after tx-data tx-meta]}]
(when-not (:rtc-tx? tx-meta)
(let [page-tag (d/entity db-after :logseq.class/Page)
tag (d/entity db-after :logseq.class/Tag)]
(assert page-tag "Page tag doesn't exist")
(mapcat
(fn [datom]
(when (and (= :block/tags (:a datom))
(:added datom))
(let [entity (d/entity db-after (:e datom))
v-entity (d/entity db-after (:v datom))]
(cond
;; add missing :db/ident and :logseq.property.class/extends for new tag
(and (= (:v datom) (:db/id tag))
(not (ldb/inline-tag? (:block/raw-title entity) tag))
(not (:db/ident entity)))
(let [eid (:db/id entity)]
[[:db/add eid :db/ident (db-class/create-user-class-ident-from-name db-after (:block/title entity))]
[:db/add eid :logseq.property.class/extends :logseq.class/Root]
[:db/retract eid :block/tags :logseq.class/Page]])
(and (= (:v datom) (:db/id tag))
(not (ldb/inline-tag? (:block/raw-title entity) tag))
(not (:db/ident entity)))
(let [eid (:db/id entity)]
[[:db/add eid :db/ident (db-class/create-user-class-ident-from-name db-after (:block/title entity))]
[:db/add eid :logseq.property.class/extends :logseq.class/Root]
[:db/retract eid :block/tags :logseq.class/Page]])
;; remove #Page from tags/journals/whiteboards, etc.
(= (:db/id page-tag) (:v datom))
(let [tags (->> entity
:block/tags
(map :db/ident)
(remove #{:logseq.class/Page}))]
(when (and (seq tags)
(= (:db/id page-tag) (:v datom))
(let [tags (->> entity
:block/tags
(map :db/ident)
(remove #{:logseq.class/Page}))]
(when (and (seq tags)
;; has other page-classes other than `:logseq.class/Page`
(some db-class/page-classes tags))
[[:db/retract (:e datom) :block/tags :logseq.class/Page]]))
(some db-class/page-classes tags))
[[:db/retract (:e datom) :block/tags :logseq.class/Page]]))
;; Add other page classes to an existing page
;; Caused by invalid tags data from server
;; TODO: remove this case
;; DEADLINE: 2025-11-30
(and (contains? (disj db-class/page-classes :logseq.class/Page) (:db/ident v-entity))
(ldb/internal-page? entity))
[[:db/retract (:e datom) :block/tags :logseq.class/Page]]
(and (contains? (disj db-class/page-classes :logseq.class/Page) (:db/ident v-entity))
(ldb/internal-page? entity))
[[:db/retract (:e datom) :block/tags :logseq.class/Page]]
:else
nil))))
tx-data)))
:else
nil))))
tx-data))))
(defn- remove-inline-page-class-from-title
"Remove inline page tag from title"