Skip to content

Commit

Permalink
Fixed tempid/upsert resolution when multiple tempids are added first (c…
Browse files Browse the repository at this point in the history
…loses #472)
  • Loading branch information
tonsky committed Jun 17, 2024
1 parent d270292 commit 9b76d9a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

# 1.6.5 - May 3, 2024

Expand Down
9 changes: 6 additions & 3 deletions src/datascript/db.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1562,15 +1562,17 @@
(declare+ transact-tx-data [initial-report initial-es])

(defn- retry-with-tempid [initial-report report es tempid upserted-eid]
(if (contains? (:tempids initial-report) tempid)
(if-some [eid (get (::upserted-tempids initial-report) tempid)]
(raise "Conflicting upsert: " tempid " resolves"
" both to " upserted-eid " and " (get-in initial-report [:tempids tempid])
" both to " upserted-eid " and " eid
{:error :transact/upsert})
;; try to re-run from the beginning
;; but remembering that `tempid` will resolve to `upserted-eid`
(let [tempids' (-> (:tempids report)
(assoc tempid upserted-eid))
report' (assoc initial-report :tempids tempids')]
report' (-> initial-report
(assoc :tempids tempids')
(update ::upserted-tempids assoc tempid upserted-eid))]
(transact-tx-data report' es))))

(def builtin-fn?
Expand Down Expand Up @@ -1634,6 +1636,7 @@
(empty? es)
(-> report
(check-value-tempids)
(dissoc ::upserted-tempids)
(update :tempids assoc :db/current-tx (current-tx report))
(update :db-after update :max-tx inc)
#_(update :db-after persistent!))
Expand Down
21 changes: 20 additions & 1 deletion test/datascript/test/upsert.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@

))


(deftest test-redefining-ids
(let [db (-> (d/empty-db {:name { :db/unique :db.unique/identity }})
(d/db-with [{:db/id -1 :name "Ivan"}]))]
Expand Down Expand Up @@ -253,6 +252,26 @@
(d/db-with db [[:db/add -1, :name "Alice"]
{:age 36, :ref -1}]))))))

;; https://github.com/tonsky/datascript/issues/472
(deftest test-two-tempids-two-retries
(let [schema {:name {:db/unique :db.unique/identity}
:ref {:db/valueType :db.type/ref}}
db (d/db-with
(d/empty-db schema)
[{:name "Alice"}
{:name "Bob"}])
expected #{[1 :name "Alice"]
[2 :name "Bob"]
[3 :ref 1]
[4 :ref 2]}]
(is (= expected
(tdc/all-datoms
(d/db-with db
[{:db/id 3, :ref "A"}
{:db/id 4, :ref "B"}
{:db/id "A", :name "Alice"}
{:db/id "B", :name "Bob"}]))))))

(deftest test-vector-upsert
(let [db (-> (d/empty-db {:name {:db/unique :db.unique/identity}})
(d/db-with [{:db/id -1, :name "Ivan"}]))]
Expand Down

0 comments on commit 9b76d9a

Please sign in to comment.