Skip to content

Commit f91fec5

Browse files
committed
fix: ensures old cardinality/one datoms retracted for db-with-tail
1 parent d630596 commit f91fec5

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/datascript/storage.cljs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,8 @@
170170
tail)]))))
171171

172172
(defn db-with-tail [db tail]
173-
(reduce
174-
(fn [db datoms]
175-
(if (empty? datoms)
176-
db
177-
(try
178-
(as-> db %
179-
(reduce db/with-datom % datoms)
180-
(assoc % :max-tx (:tx (first datoms))))
181-
(catch :default e
182-
(js/console.error e)
183-
db))))
184-
db tail))
173+
;; Ensures old cardinality/one datoms retracted
174+
(:db-after (db/transact-tx-data (db/->TxReport db db [] {} {}) (apply concat tail))))
185175

186176
(defn restore
187177
([storage]

test/datascript/test/storage.cljs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[clojure.test :as t :refer [is are deftest testing]]
66
#_[cognitect.transit :as transit]
77
[datascript.core :as d]
8+
[datascript.db :as db]
89
[datascript.storage :as storage]
910
#_[datascript.test.core :as tdc]))
1011

@@ -139,6 +140,31 @@
139140
(is (= 8 (count @(:*writes storage))))))) ;; root, tail + 3 leves * 2 indexes
140141
))
141142

143+
(deftest test-db-with-tail
144+
(testing "db-with-tail retracts stale cardinality/one values when replaying tail datoms"
145+
(let [schema {:block/updated-at {:db/index true}}
146+
db (-> (d/empty-db schema)
147+
(d/db-with [[:db/add 1 :block/updated-at 2]]))
148+
tail [[(db/datom 1 :block/updated-at 1772979060646 536870915 true)]
149+
[(db/datom 1 :block/updated-at 1772979061145 536870916 true)]]
150+
db' (storage/db-with-tail db tail)]
151+
(is (= [1772979061145]
152+
(mapv :v (d/datoms db' :avet :block/updated-at))))))
153+
154+
(testing "restore replays stored tail without leaving stale cardinality/one values"
155+
(let [schema {:block/updated-at {:db/index true}}
156+
storage (make-storage {:stats true})
157+
db (-> (d/empty-db schema {:storage storage
158+
:branching-factor 32
159+
:ref-type :strong})
160+
(d/db-with [[:db/add 1 :block/updated-at 2]]))
161+
tail [[(db/datom 1 :block/updated-at 1772979060646 536870915 true)]
162+
[(db/datom 1 :block/updated-at 1772979061145 536870916 true)]]]
163+
(d/store db)
164+
(storage/store-tail db tail)
165+
(is (= [1772979061145]
166+
(mapv :v (d/datoms (d/restore storage) :avet :block/updated-at)))))))
167+
142168
;; Commented out test-gc test-file-storage tests and helpers b/c they are not implemented in cljs
143169

144170
;; (defmacro with-dir [dir & body]

0 commit comments

Comments
 (0)