Skip to content

Commit

Permalink
Remove special treatment of :db/ident attribute (#711)
Browse files Browse the repository at this point in the history
* Remove special treatment of :db/ident attribute in lookup and transaction

* FIXUP comment the unit test

---------

Co-authored-by: Jonas Östlund <[email protected]>
  • Loading branch information
jonasseglare and Jonas Östlund authored Sep 27, 2024
1 parent 441e0ed commit 1d6762b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/datahike/db/utils.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@

(defn attr-has-ref? [db attr]
(and (not (nil? attr))
(not (ds/built-in-attribute? attr))
(:attribute-refs? (dbi/-config db))))

(defn attr-ref-or-ident [db attr]
Expand Down
5 changes: 0 additions & 5 deletions src/datahike/schema.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,5 @@
(= "db" (first (clojure.string/split ns #"\.")))
false)))

(def built-in-attributes #{:db/ident})

(defn built-in-attribute? [x]
(contains? built-in-attributes x))

(defn get-user-schema [{:keys [schema] :as db}]
(into {} (filter #(not (is-system-keyword? (key %))) schema)))
53 changes: 44 additions & 9 deletions test/datahike/test/attribute_refs/differences_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,50 @@
(is (not (nil? (d/transact conn [[:db/add next-eid :db/ident :name]]))))
(d/release conn)))

(testing "Keyword transaction in reference DB"
(let [[no-ref-cfg ref-cfg] (init-cfgs)
conn (setup-db ref-cfg)
next-eid (inc (:max-eid @conn))]
(is (thrown-with-msg? Throwable
(re-pattern (str "Bad entity attribute :db/ident"
" at \\[:db/add " next-eid " :db/ident :name\\],"
" expected reference number"))
(d/transact conn [[:db/add next-eid :db/ident :name]])))
(testing "Using :db/ident attribute"
(doseq [cfg (init-cfgs)
:let [attribute-refs? (:attribute-refs? cfg)
conn (setup-db cfg)
next-eid (inc (:max-eid @conn))
init-datoms (d/datoms
@conn
{:index :aevt
:components [:db/ident]})]]

;; Check that the database with attribute-refs? being true
;; contains some initial atoms, and otherwise none.
(is (= (boolean (seq init-datoms))
(boolean attribute-refs?)))

;; Transact a datom for attribute :db/ident. This
;; must work no matter the value of :attribute-refs?
(is (some? (d/transact
conn
[[:db/add next-eid :db/ident :name]])))

;; Check that we can access the datom just transacted
;; using d/datoms.
(let [datoms (d/datoms @conn
{:index :aevt
:components [:db/ident]})]
(is (= (inc (count init-datoms))
(count datoms)))
(is (some (fn [[_ a v]]
(and (= v :name)
(or attribute-refs?
(= a :db/ident))))
datoms)))

;; Check for a more specific query.
(let [[ident-name-datom :as ident-name-datoms]
(d/datoms @conn
{:index :avet
:components [:db/ident
:name]})]
(is (= 1 (count ident-name-datoms)))
(is (= :name (:v ident-name-datom)))
(is (or attribute-refs?
(= :db/ident (:a ident-name-datom)))))
(d/release conn))))

(deftest test-transact-data-with-reference-attr
Expand Down

0 comments on commit 1d6762b

Please sign in to comment.