Skip to content

Instantly share code, notes, and snippets.

@adamneilson
Created May 16, 2015 16:32
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 adamneilson/04f2b6b1979a76677f38 to your computer and use it in GitHub Desktop.
Save adamneilson/04f2b6b1979a76677f38 to your computer and use it in GitHub Desktop.
De-componentizes the attributes that are not refs but are components.
;; assume conn to be your database connection
(let [dbval (d/db conn)
;; dump the whole schema
schema (->> (d/q '[:find ?ident
:in $ ?p
:where
[:db.part/db :db.install/attribute ?attr]
[?attr :db/ident ?ident]]
dbval :db.part/user)
(map (fn [attr] [(d/entity dbval (first attr))]))
(map (fn [attr]
{:db/id #db/id[:db.part/db]
:db/ident (:db/ident (first attr))
:db/valueType (:db/valueType (first attr))
:db/cardinality (:db/cardinality (first attr))
:db/doc (:db/doc (first attr))
:db/isComponent (or (:db/isComponent (first attr)) (not (nil? (:db/isComponent (first attr)))))
:db/fulltext (when (:db/fulltext (first attr)) true)
:db/unique (when (not (nil? (:db/unique (first attr)))) :db.unique/identity)
:db.install/_attribute :db.part/db}))
; remove all the nils
(map (fn [attr] (apply dissoc attr (for [[k v] attr :when (nil? v)] k))))
; remove datomic's db attrs
(filter (fn [attr] (not= (subs (namespace (:db/ident attr)) 0 2) "db")))
(sort-by :db/ident))]
;; filter out component attributes that are not refs
(let [targetAttributes (->> schema
(filter (fn [attr] (and (not= (:db/valueType attr) :db.type/ref)
(:db/isComponent attr))))
(mapv (fn [attr] (:db/ident attr))))]
;; switch these attributes to non-component types
@(d/transact conn (mapv
(fn [attrIdent]
{:db/id attrIdent
:db/isComponent false
:db.alter/_attribute :db.part/db})
targetAttributes)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment