Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@souenzzo
Created June 28, 2018 17:29
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 souenzzo/d8e6afe21e990530f58fab5c8c3abc8c to your computer and use it in GitHub Desktop.
Save souenzzo/d8e6afe21e990530f58fab5c8c3abc8c to your computer and use it in GitHub Desktop.
(require '[datomic.api :as d])
(defn undo-transact
[db tx]
(let [ref? (set (d/q '[:find [?ident ...] :where [?e :db/ident ?ident] [?e :db/valueType :db.type/ref]] db))
ident (into {} (d/q '[:find ?id ?ident :where [?id :db/ident ?ident]] db))
hist (d/history db)
data-from-tx (d/q '[:find ?e ?a ?v ?tx ?op
:in $ ?tx
:where
[?i :db/ident ?a]
[?e ?i ?v ?tx ?op]]
hist tx)
tx-data (for [[e a v tx op] data-from-tx]
[e a (if (ref? a) (ident v v) v) tx op])
tx-data (keep (fn [[e a v tx op]]
(when-not (= e tx)
[(if op :db/retract :db/add) e a v])) tx-data)]
(into [[:db/add (d/tempid :db.part/tx) :audit/undo-tx tx]] tx-data)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment