Skip to content

Commit

Permalink
fixed id allocation bug (closes #66)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Jul 19, 2015
1 parent 9f20183 commit 08730fd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- fix in Pull API for reverse non-component attributes (#91, thx [Matt Senior](https://github.com/mattsenior))
- Node.js and Browser repls for dev profile (#93)
- Preconditions to validate db/conn arguments (#101)
- Id allocation bug (#66)

# 0.11.5

Expand Down
6 changes: 4 additions & 2 deletions src/datascript/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -908,8 +908,10 @@
new-eid (or (:db/id upserted) (next-eid db))
new-entity (assoc upserted :db/id new-eid)
new-report (cond
(nil? old-eid) (allocate-eid report new-eid)
(shim/neg-number? old-eid) (allocate-eid report old-eid new-eid)
(nil? old-eid) (allocate-eid report new-eid)
(shim/neg-number? old-eid) (allocate-eid report old-eid new-eid)
(and (number? old-eid)
(> old-eid (:max-eid db))) (allocate-eid report old-eid)
:else report)]
(recur new-report (concat (explode db new-entity) entities)))

Expand Down
34 changes: 34 additions & 0 deletions test/datascript/test/explode.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,37 @@
[ {:email "@2" :_profile [{:name "Ivan"} {:name "Petr"} ]} ]
#{ [1 :email "@2"] [2 :name "Ivan"] [2 :profile 1] [3 :name "Petr"] [3 :profile 1] }
))))

(deftest test-circular-refs
(let [schema {:comp {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many
:db/isComponent true}}
db (d/db-with (d/empty-db schema)
[{:db/id 1, :comp [{:name "C"}]}])]
(is (= (mapv (juxt :e :a :v) (d/datoms db :eavt))
[ [ 1 :comp 2 ]
[ 2 :name "C"] ])))

(let [schema {:comp {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}}
db (d/db-with (d/empty-db schema)
[{:db/id 1, :comp [{:name "C"}]}])]
(is (= (mapv (juxt :e :a :v) (d/datoms db :eavt))
[ [ 1 :comp 2 ]
[ 2 :name "C"] ])))

(let [schema {:comp {:db/valueType :db.type/ref
:db/isComponent true}}
db (d/db-with (d/empty-db schema)
[{:db/id 1, :comp {:name "C"}}])]
(is (= (mapv (juxt :e :a :v) (d/datoms db :eavt))
[ [ 1 :comp 2 ]
[ 2 :name "C"] ])))

(let [schema {:comp {:db/valueType :db.type/ref}}
db (d/db-with (d/empty-db schema)
[{:db/id 1, :comp {:name "C"}}])]
(is (= (mapv (juxt :e :a :v) (d/datoms db :eavt))
[ [ 1 :comp 2 ]
[ 2 :name "C"] ]))))

0 comments on commit 08730fd

Please sign in to comment.