Skip to content

Commit 8874012

Browse files
darkleaftonsky
authored andcommitted
Diff dbs with different types of the same attribute (closes #369)
1 parent def78a2 commit 8874012

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/datascript/db.cljc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,15 @@
422422
:else
423423
(let [first-a (first a)
424424
first-b (first b)
425-
diff (cmp first-a first-b)]
425+
diff (try
426+
(cmp first-a first-b)
427+
(catch #?(:clj ClassCastException :cljs js/Error) _
428+
:incomparable))]
426429
(cond
427-
(== diff 0) (recur only-a only-b (conj both first-a) (next a) (next b))
428-
(< diff 0) (recur (conj only-a first-a) only-b both (next a) b)
429-
(> diff 0) (recur only-a (conj only-b first-b) both a (next b)))))))
430+
(= diff :incomparable) (recur (conj only-a first-a) (conj only-b first-b) both (next a) (next b))
431+
(== diff 0) (recur only-a only-b (conj both first-a) (next a) (next b))
432+
(< diff 0) (recur (conj only-a first-a) only-b both (next a) b)
433+
(> diff 0) (recur only-a (conj only-b first-b) both a (next b)))))))
430434

431435
;; ----------------------------------------------------------------------------
432436

test/datascript/test/issues.cljc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,12 @@
3434
filtered (ds/filter base (constantly true))]
3535
(t/is (= (with-out-str (clojure.pprint/pprint base))
3636
(with-out-str (clojure.pprint/pprint filtered)))))))
37+
38+
(deftest ^{:doc "Can't diff databases with different types of the same attribute"}
39+
issue-369
40+
(let [db1 (-> (ds/empty-db)
41+
(ds/db-with [[:db/add 1 :attr :aa]]))
42+
db2 (-> (ds/empty-db)
43+
(ds/db-with [[:db/add 1 :attr "aa"]]))]
44+
(t/is (= [[(ds/datom 1 :attr :aa)] [(ds/datom 1 :attr "aa")] nil]
45+
(clojure.data/diff db1 db2)))))

0 commit comments

Comments
 (0)