Skip to content

Instantly share code, notes, and snippets.

@greywolve
Last active October 5, 2016 08:08
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 greywolve/9313e516be438f53b6a3a1cb4cd3e0c3 to your computer and use it in GitHub Desktop.
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.
(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