fix: test

This commit is contained in:
Tienson Qin
2026-04-02 20:42:58 +08:00
parent 333f3e10b1
commit 9f19cf8e2b
3 changed files with 88 additions and 114 deletions

View File

@@ -97,24 +97,6 @@
created-at
(outliner-op->sql outliner-op)))
(defn- with-sql-transaction!
[sql transaction-sync-f f]
(if (fn? transaction-sync-f)
(transaction-sync-f f)
(do
;; Node sqlite wrapper supports SQL transaction statements directly.
;; Durable Objects sqlite must use state.storage.transactionSync().
(common/sql-exec sql "BEGIN IMMEDIATE")
(try
(let [result (f)]
(common/sql-exec sql "COMMIT")
result)
(catch :default error
(try
(common/sql-exec sql "ROLLBACK")
(catch :default _))
(throw error))))))
(defn fetch-tx-since [sql since-t]
(let [rows (common/get-sql-rows
(common/sql-exec sql
@@ -162,36 +144,30 @@
(restore-data-from-addr sql addr))))
(defn- append-tx-for-tx-report
[sql transaction-sync-f {:keys [db-after db-before tx-data tx-meta] :as tx-report}]
[sql {:keys [db-after db-before tx-data tx-meta] :as tx-report}]
(let [created-at (common/now-ms)
normalized-data (->> tx-data
(db-normalize/normalize-tx-data db-after db-before))
;; _ (prn :debug :tx-data tx-data)
;; _ (prn :debug :normalized-data normalized-data)
tx-str (common/write-transit normalized-data)]
(with-sql-transaction!
sql
transaction-sync-f
(fn []
(let [new-t (next-t! sql)
prev-checksum (get-checksum sql)
checksum (sync-checksum/update-checksum prev-checksum tx-report)]
(set-checksum! sql checksum)
(append-tx! sql new-t tx-str created-at (:outliner-op tx-meta)))))))
tx-str (common/write-transit normalized-data)
new-t (next-t! sql)
prev-checksum (get-checksum sql)
checksum (sync-checksum/update-checksum prev-checksum tx-report)]
(set-checksum! sql checksum)
(append-tx! sql new-t tx-str created-at (:outliner-op tx-meta))))
(defn- listen-db-updates!
[sql conn transaction-sync-f]
[sql conn]
(d/listen! conn ::listen-db-updates
(fn [tx-report]
(append-tx-for-tx-report sql transaction-sync-f tx-report))))
(append-tx-for-tx-report sql tx-report))))
(defn open-conn
([sql]
(open-conn sql nil))
([sql {:keys [transaction-sync-f]}]
(init-schema! sql)
(let [storage (new-sqlite-storage sql)
schema db-schema/schema
conn (common-sqlite/get-storage-conn storage schema)]
(listen-db-updates! sql conn transaction-sync-f)
conn)))
[sql]
(init-schema! sql)
(let [storage (new-sqlite-storage sql)
schema db-schema/schema
conn (common-sqlite/get-storage-conn storage schema)]
(listen-db-updates! sql conn)
conn))

View File

@@ -48,13 +48,8 @@
(defn- ensure-conn! [^js self]
(ensure-schema! self)
(when-not (.-conn self)
(let [storage (some-> self .-state .-storage)
transaction-sync-f (when (fn? (some-> storage .-transactionSync))
(fn [f]
(.transactionSync storage f)))]
(set! (.-conn self)
(storage/open-conn (.-sql self)
{:transaction-sync-f transaction-sync-f})))))
(set! (.-conn self)
(storage/open-conn (.-sql self)))))
(defn t-now [^js self]
(ensure-schema! self)

View File

@@ -557,8 +557,23 @@
(defn- property-ref-value
[db property-id value]
(let [property-type (some-> (d/entity db property-id) :logseq.property/type)]
(if (contains? db-property-type/all-ref-property-types property-type)
(cond
;; Number property values are stored as ref entities but the semantic op
;; uses scalar content for undo/redo payloads.
(= :number property-type)
(let [to-content (fn [v]
(if (some? (:db/id v))
(or (db-property/property-value-content v) v)
v))]
(cond
(set? value) (set (map to-content value))
(sequential? value) (mapv to-content value)
:else (to-content value)))
(contains? db-property-type/all-ref-property-types property-type)
(sanitize-ref-value db value)
:else
value)))
(defn- block-property-value
@@ -620,82 +635,70 @@
ops)]
(seq ops')))
(defn- property-history-cleanup-op
[db-before db-after tx-data block-ids property-id]
(when-let [history-refs (property-history-refs-from-tx-data
db-before
db-after
tx-data
block-ids
property-id)]
[:delete-blocks [history-refs {}]]))
(defn- restore-property-op
[before-value block-ref property-id {:keys [remove-when-nil?]}]
(if (nil? before-value)
(when remove-when-nil?
[:remove-block-property [block-ref property-id]])
[:set-block-property [block-ref property-id before-value]]))
(defn- inverse-property-ops-for-blocks
[db-before block-ids property-id restore-opts]
(->> block-ids
(keep (fn [block-id]
(let [before-value (block-property-value db-before block-id property-id)
block-ref (stable-entity-ref db-before block-id)]
(restore-property-op before-value block-ref property-id restore-opts))))
vec
seq))
(defn- inverse-property-change-op
[db-before db-after tx-data block-ids property-id restore-opts]
(let [cleanup-op (property-history-cleanup-op
db-before
db-after
tx-data
block-ids
property-id)
inverse-ops (inverse-property-ops-for-blocks
db-before
block-ids
property-id
restore-opts)]
(prepend-history-cleanup-op cleanup-op inverse-ops)))
(defn- inverse-property-op
[db-before db-after tx-data op args]
(case op
:set-block-property
(let [[block-id property-id _value] args
before-value (block-property-value db-before block-id property-id)
block-ref (stable-entity-ref db-before block-id)
cleanup-op (when-let [history-refs (property-history-refs-from-tx-data
db-before
db-after
tx-data
[block-id]
property-id)]
[:delete-blocks [history-refs {}]])]
(prepend-history-cleanup-op
cleanup-op
(if (nil? before-value)
[:remove-block-property [block-ref property-id]]
[:set-block-property [block-ref property-id before-value]])))
(let [[block-id property-id _value] args]
(inverse-property-change-op
db-before db-after tx-data [block-id] property-id {:remove-when-nil? true}))
:remove-block-property
(let [[block-id property-id] args
before-value (block-property-value db-before block-id property-id)
block-ref (stable-entity-ref db-before block-id)
cleanup-op (when-let [history-refs (property-history-refs-from-tx-data
db-before
db-after
tx-data
[block-id]
property-id)]
[:delete-blocks [history-refs {}]])]
(prepend-history-cleanup-op
cleanup-op
(when (some? before-value)
[:set-block-property [block-ref property-id before-value]])))
(let [[block-id property-id] args]
(inverse-property-change-op
db-before db-after tx-data [block-id] property-id {:remove-when-nil? false}))
:batch-set-property
(let [[block-ids property-id _value _opts] args
cleanup-op (when-let [history-refs (property-history-refs-from-tx-data
db-before
db-after
tx-data
block-ids
property-id)]
[:delete-blocks [history-refs {}]])]
(prepend-history-cleanup-op
cleanup-op
(->> block-ids
(keep (fn [block-id]
(let [before-value (block-property-value db-before block-id property-id)
block-ref (stable-entity-ref db-before block-id)]
(if (nil? before-value)
[:remove-block-property [block-ref property-id]]
[:set-block-property [block-ref property-id before-value]]))))
vec
seq)))
(let [[block-ids property-id _value _opts] args]
(inverse-property-change-op
db-before db-after tx-data block-ids property-id {:remove-when-nil? true}))
:batch-remove-property
(let [[block-ids property-id _opts] args
cleanup-op (when-let [history-refs (property-history-refs-from-tx-data
db-before
db-after
tx-data
block-ids
property-id)]
[:delete-blocks [history-refs {}]])]
(prepend-history-cleanup-op
cleanup-op
(->> block-ids
(keep (fn [block-id]
(let [before-value (block-property-value db-before block-id property-id)
block-ref (stable-entity-ref db-before block-id)]
(when (some? before-value)
[:set-block-property [block-ref property-id before-value]]))))
vec
seq)))
(let [[block-ids property-id _opts] args]
(inverse-property-change-op
db-before db-after tx-data block-ids property-id {:remove-when-nil? false}))
nil))