Skip to content

Instantly share code, notes, and snippets.

@Hendekagon
Created July 28, 2018 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hendekagon/9e88a993eecd759e19878a5a85bee9b2 to your computer and use it in GitHub Desktop.
Save Hendekagon/9e88a993eecd759e19878a5a85bee9b2 to your computer and use it in GitHub Desktop.
changing :db/unique
(defn make-attr-test [conn]
(let [tx
[
{:db/ident :y :db/cardinality :db.cardinality/many :db/valueType :db.type/ref :db/isComponent true}
{:db/ident :ya :db/cardinality :db.cardinality/one :db/valueType :db.type/keyword}
{:db/ident :yb :db/cardinality :db.cardinality/one :db/valueType :db.type/string}
]]
(d/transact conn {:tx-data tx})))
(defn add-test-data [conn]
(let [tx
{:y [{:ya :x :yb "x0"} {:ya :x :yb "x1"} {:ya :x :yb "x2"}]}
]
(d/transact conn {:tx-data [tx]})))
(defn change-attr-unique [conn]
(let [tx
[
{:db/ident :ya :db/unique :db.unique/identity}
]]
(d/transact conn {:tx-data tx})))
@Hendekagon
Copy link
Author


; This is a test of trying to assert :db/unique on an attribute
; after we've mistakenly made multiple assertions 

; create the schema

(make-attr-test tdb1)
=>
{:db-before {:database-id "acf2a497-edd2-4bb4-9593-7a5518e7cf9f",
             :db-name "reverse-pull-test",
             :t 22,
             :next-t 23,
             :type :datomic.client/db},
 :db-after {:database-id "acf2a497-edd2-4bb4-9593-7a5518e7cf9f",
            :db-name "reverse-pull-test",
            :t 23,
            :next-t 24,
            :type :datomic.client/db},
 :tx-data [#datom[13194139533335
                  50
                  #inst"2018-07-28T09:58:41.686-00:00"
                  13194139533335
                  true]
           #datom[74 10 :y 13194139533335 true]
           #datom[74 41 36 13194139533335 true]
           #datom[74 40 20 13194139533335 true]
           #datom[74 43 true 13194139533335 true]
           #datom[75 10 :ya 13194139533335 true]
           #datom[75 41 35 13194139533335 true]
           #datom[75 40 21 13194139533335 true]
           #datom[76 10 :yb 13194139533335 true]
           #datom[76 41 35 13194139533335 true]
           #datom[76 40 23 13194139533335 true]
           #datom[0 13 74 13194139533335 true]
           #datom[0 13 75 13194139533335 true]
           #datom[0 13 76 13194139533335 true]],
 :tempids {}}

; add some data

(add-test-data tdb1)
=>
{:db-before {:database-id "acf2a497-edd2-4bb4-9593-7a5518e7cf9f",
             :db-name "reverse-pull-test",
             :t 23,
             :next-t 24,
             :type :datomic.client/db},
 :db-after {:database-id "acf2a497-edd2-4bb4-9593-7a5518e7cf9f",
            :db-name "reverse-pull-test",
            :t 24,
            :next-t 25,
            :type :datomic.client/db},
 :tx-data [#datom[13194139533336
                  50
                  #inst"2018-07-28T09:58:55.448-00:00"
                  13194139533336
                  true]
           #datom[66005882038648911
                  74
                  66005882038648912
                  13194139533336
                  true]
           #datom[66005882038648912 75 :x 13194139533336 true]
           #datom[66005882038648912 76 "x0" 13194139533336 true]
           #datom[66005882038648911
                  74
                  66005882038648913
                  13194139533336
                  true]
           #datom[66005882038648913 75 :x 13194139533336 true]
           #datom[66005882038648913 76 "x1" 13194139533336 true]
           #datom[66005882038648911
                  74
                  66005882038648914
                  13194139533336
                  true]
           #datom[66005882038648914 75 :x 13194139533336 true]
           #datom[66005882038648914 76 "x2" 13194139533336 true]],
 :tempids {"datomic.temp-3403948" 66005882038648913,
           "datomic.temp-3403947" 66005882038648912,
           "datomic.temp-3403949" 66005882038648914}}
=> nil

; whooops! didn't mean to do that!

(change-attr-unique tdb1)
ExceptionInfo Error: {:db/error :db.error/unique-violation, :datoms [#datom[66005882038648912 75 :x 13194139533336 true] #datom[66005882038648913 75 :x 13194139533336 true]]}  clojure.core/ex-info (core.clj:4739)

; that's because we have multiple values for that attribute y

(d/q '{:find [?e] :where [[?n :y ?e]]} (d/db tdb1))
=> [[66005882038648912] [66005882038648913] [66005882038648914]]

(d/q '{:find [?n ?e] :where [[?n :y ?e]]} (d/db tdb1))
=>
[[66005882038648911 66005882038648912]
 [66005882038648911 66005882038648913]
 [66005882038648911 66005882038648914]]

; 3 values and therefore not unique in the set of current database assertions
; let's retract them

(d/transact tdb1 {:tx-data [[:db/retract 66005882038648911 :y 66005882038648914] [:db/retract 66005882038648911 :y 66005882038648913]]})
=>
{:db-before {:database-id "acf2a497-edd2-4bb4-9593-7a5518e7cf9f",
             :db-name "reverse-pull-test",
             :t 24,
             :next-t 25,
             :type :datomic.client/db},
 :db-after {:database-id "acf2a497-edd2-4bb4-9593-7a5518e7cf9f",
            :db-name "reverse-pull-test",
            :t 25,
            :next-t 26,
            :type :datomic.client/db},
 :tx-data [#datom[13194139533337
                  50
                  #inst"2018-07-28T10:06:25.969-00:00"
                  13194139533337
                  true]
           #datom[66005882038648911
                  74
                  66005882038648914
                  13194139533337
                  false]
           #datom[66005882038648911
                  74
                  66005882038648913
                  13194139533337
                  false]],
 :tempids {}}

; retracted 2 of them

(d/q '{:find [?n ?e] :where [[?n :y ?e]]} (d/db tdb1))
=> [[66005882038648911 66005882038648912]]

; now there's 1 left, which counts as unique right ?

(change-attr-unique tdb1)
ExceptionInfo Error: {:db/error :db.error/unique-violation, :datoms [#datom[66005882038648912 75 :x 13194139533336 true] #datom[66005882038648913 75 :x 13194139533336 true]]}  clojure.core/ex-info (core.clj:4739)

; nope
; let's retract that remaining one

(d/transact tdb1 {:tx-data [[:db/retract 66005882038648911 :y 66005882038648912]]})
=>
{:db-before {:database-id "acf2a497-edd2-4bb4-9593-7a5518e7cf9f",
             :db-name "reverse-pull-test",
             :t 25,
             :next-t 26,
             :type :datomic.client/db},
 :db-after {:database-id "acf2a497-edd2-4bb4-9593-7a5518e7cf9f",
            :db-name "reverse-pull-test",
            :t 26,
            :next-t 27,
            :type :datomic.client/db},
 :tx-data [#datom[13194139533338
                  50
                  #inst"2018-07-28T10:07:00.629-00:00"
                  13194139533338
                  true]
           #datom[66005882038648911
                  74
                  66005882038648912
                  13194139533338
                  false]],
 :tempids {}}

; check

(d/q '{:find [?n ?e] :where [[?n :y ?e]]} (d/db tdb1))
=> []

; now there are none


(change-attr-unique tdb1)
ExceptionInfo Error: {:db/error :db.error/unique-violation, :datoms [#datom[66005882038648912 75 :x 13194139533336 true] #datom[66005882038648913 75 :x 13194139533336 true]]}  clojure.core/ex-info (core.clj:4739)

; still can't change assert that :db/unique

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment