Last active
October 5, 2016 08:08
-
-
Save greywolve/9313e516be438f53b6a3a1cb4cd3e0c3 to your computer and use it in GitHub Desktop.
Get all schema from a Datomic database, in a form that can be re-transacted.
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
(defn get-schema [db] | |
(letfn [(attr->schema-tx [db attr] | |
(let [e (d/entity db attr) | |
em (->> e | |
(map identity) | |
(into {}))] | |
(cond | |
;; attribute | |
(:db/valueType e) | |
(merge em {:db/id (d/tempid :db.part/db) | |
:db.install/_attribute :db.part/db}) | |
;; partition | |
(-> e :db.install/_partition) | |
(merge em | |
{:db/id (d/tempid :db.part/db) | |
:db.install/_partition :db.part/db}) | |
;; enum or database fn | |
:else | |
(let [p (->> e :db/id d/part (d/entity db) :db/ident)] | |
(cond-> em | |
true (assoc :db/id (d/tempid p)) | |
(:db/fn em) (assoc-in [:db/fn :fnref] nil))))))] | |
(->> (d/q '[:find ?tx ?id | |
:where | |
[?e :db/ident ?id ?tx] | |
[(str ?id) ?ident-str] | |
(not [(.startsWith ?ident-str ":db")]) | |
(not [(.startsWith ?ident-str ":fressian")])] | |
db) | |
(map (juxt | |
first | |
(comp (partial attr->schema-tx db) second))) | |
(sort-by first) | |
(partition-by first) | |
(map #(mapv second %))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment