Created
September 9, 2016 15:00
-
-
Save Jaretbinford/28e14beaac238c0749517bfb07f88ecf to your computer and use it in GitHub Desktop.
Example of Merging two attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns merge.core | |
(:import datomic.Util) | |
(require [datomic.api :as d] | |
[clojure.java.io :as io])) | |
(def db-uri "datomic:dev://localhost:4334/merge") | |
(d/create-database db-uri) | |
(def conn (d/connect db-uri)) | |
;; bin/transactor /Users/jbin/Desktop/Jaret/Tools/datomic-pro-0.9.5394/config/samples/dev-transactor-template.properties | |
(def schema-tx (read-string (slurp "/Users/jbin/Desktop/Jaret/Projects/merge/resources/schema.edn"))) | |
@(d/transact conn schema-tx) | |
(def db (d/db conn)) | |
;; Pull the attributes I have inserted. | |
(d/pull db '[*] 250) | |
(d/pull db '[*] 251) | |
;;Transact some identical usernames to each username and e-mail | |
(def test-data-tx (read-string (slurp "/Users/jbin/Desktop/Jaret/Projects/merge/Data.edn"))) | |
@(d/transact conn test-data-tx) | |
(def db (d/db conn)) | |
;;query all the emails and IDs | |
(d/q '[:find ?e ?email | |
:where [?e :user/email-depricated ?email]] db) | |
(def db (d/db conn)) | |
;;create alter email-depricated | |
(def schema [{:db/id #db/id [:db.part/db 250], | |
:db/ident :user/email-depricated, | |
:db.alter/_attribute :db.part/db}]) | |
@(d/transact conn schema) | |
;;all to be depricated and merged e-mails | |
(d/q '[:find ?e ?email | |
:where [?e :user/email-depricated ?email]] db) | |
;; Move all e-mail-depricated to the username attribute | |
(def db (d/db conn)) | |
(defn merge-all | |
"Merge all mapped results" | |
[db] | |
(->> (d/q '[:find ?e ?email | |
:where | |
[?e :user/email-depricated ?email]] | |
db) | |
(mapcat (fn[[e email]] | |
[[:db/add e :user/username email] | |
[:db/retract e :user/email-depricated email]])) | |
)) | |
(merge-all db) | |
@(d/transact conn (merge-all db)) | |
;; Pull the email and username | |
(def db (d/db conn)) | |
(d/q '[:find ?e ?email | |
:where [?e :user/username ?email]] db) | |
(d/pull db '[*] 17592186045423) | |
(d/pull db '[*] 17592186045424) | |
;;Reference Schema | |
;; [{:db/ident :user/email, | |
;; :db/valueType :db.type/string, | |
;; :db/cardinality :db.cardinality/one, | |
;; :db/doc "E-mail of user", | |
;; :db.install/_attribute :db.part/db, | |
;; :db/id #db/id[:db.part/db 250]} | |
;;{:db/ident :user/username, | |
;; :db/valueType :db.type/string, | |
;; :db/cardinality :db.cardinality/one, | |
;; :db/doc "Username of user", | |
;; :db.install/_attribute :db.part/db, | |
;; :db/id #db/id[:db.part/db 251]}] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment