Skip to content

Commit 9b76d9a

Browse files
committed
Fixed tempid/upsert resolution when multiple tempids are added first (closes #472)
1 parent d270292 commit 9b76d9a

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- Stable sorting of sequences of various types #470
44
- Correctly restore `:max-tx` from storage
5+
- Fixed tempid/upsert resolution when multiple tempids are added first #472
56

67
# 1.6.5 - May 3, 2024
78

src/datascript/db.cljc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,15 +1562,17 @@
15621562
(declare+ transact-tx-data [initial-report initial-es])
15631563

15641564
(defn- retry-with-tempid [initial-report report es tempid upserted-eid]
1565-
(if (contains? (:tempids initial-report) tempid)
1565+
(if-some [eid (get (::upserted-tempids initial-report) tempid)]
15661566
(raise "Conflicting upsert: " tempid " resolves"
1567-
" both to " upserted-eid " and " (get-in initial-report [:tempids tempid])
1567+
" both to " upserted-eid " and " eid
15681568
{:error :transact/upsert})
15691569
;; try to re-run from the beginning
15701570
;; but remembering that `tempid` will resolve to `upserted-eid`
15711571
(let [tempids' (-> (:tempids report)
15721572
(assoc tempid upserted-eid))
1573-
report' (assoc initial-report :tempids tempids')]
1573+
report' (-> initial-report
1574+
(assoc :tempids tempids')
1575+
(update ::upserted-tempids assoc tempid upserted-eid))]
15741576
(transact-tx-data report' es))))
15751577

15761578
(def builtin-fn?
@@ -1634,6 +1636,7 @@
16341636
(empty? es)
16351637
(-> report
16361638
(check-value-tempids)
1639+
(dissoc ::upserted-tempids)
16371640
(update :tempids assoc :db/current-tx (current-tx report))
16381641
(update :db-after update :max-tx inc)
16391642
#_(update :db-after persistent!))

test/datascript/test/upsert.cljc

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@
196196

197197
))
198198

199-
200199
(deftest test-redefining-ids
201200
(let [db (-> (d/empty-db {:name { :db/unique :db.unique/identity }})
202201
(d/db-with [{:db/id -1 :name "Ivan"}]))]
@@ -253,6 +252,26 @@
253252
(d/db-with db [[:db/add -1, :name "Alice"]
254253
{:age 36, :ref -1}]))))))
255254

255+
;; https://github.com/tonsky/datascript/issues/472
256+
(deftest test-two-tempids-two-retries
257+
(let [schema {:name {:db/unique :db.unique/identity}
258+
:ref {:db/valueType :db.type/ref}}
259+
db (d/db-with
260+
(d/empty-db schema)
261+
[{:name "Alice"}
262+
{:name "Bob"}])
263+
expected #{[1 :name "Alice"]
264+
[2 :name "Bob"]
265+
[3 :ref 1]
266+
[4 :ref 2]}]
267+
(is (= expected
268+
(tdc/all-datoms
269+
(d/db-with db
270+
[{:db/id 3, :ref "A"}
271+
{:db/id 4, :ref "B"}
272+
{:db/id "A", :name "Alice"}
273+
{:db/id "B", :name "Bob"}]))))))
274+
256275
(deftest test-vector-upsert
257276
(let [db (-> (d/empty-db {:name {:db/unique :db.unique/identity}})
258277
(d/db-with [{:db/id -1, :name "Ivan"}]))]

0 commit comments

Comments
 (0)