From 1d6762bb2881f9838bbabe2e015fec45bb3ec3ad Mon Sep 17 00:00:00 2001 From: jonasseglare <5850088+jonasseglare@users.noreply.github.com> Date: Fri, 27 Sep 2024 23:16:41 +0200 Subject: [PATCH] Remove special treatment of :db/ident attribute (#711) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove special treatment of :db/ident attribute in lookup and transaction * FIXUP comment the unit test --------- Co-authored-by: Jonas Östlund --- src/datahike/db/utils.cljc | 1 - src/datahike/schema.cljc | 5 -- .../test/attribute_refs/differences_test.cljc | 53 +++++++++++++++---- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/datahike/db/utils.cljc b/src/datahike/db/utils.cljc index fb589403d..904fe9bcf 100644 --- a/src/datahike/db/utils.cljc +++ b/src/datahike/db/utils.cljc @@ -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] diff --git a/src/datahike/schema.cljc b/src/datahike/schema.cljc index 1266e67d7..50cbd82b0 100644 --- a/src/datahike/schema.cljc +++ b/src/datahike/schema.cljc @@ -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))) diff --git a/test/datahike/test/attribute_refs/differences_test.cljc b/test/datahike/test/attribute_refs/differences_test.cljc index 08dc4d01c..ffd10869e 100644 --- a/test/datahike/test/attribute_refs/differences_test.cljc +++ b/test/datahike/test/attribute_refs/differences_test.cljc @@ -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