Created
August 6, 2015 15:17
-
-
Save stuarthalloway/8ad18bde7af89c6b871a to your computer and use it in GitHub Desktop.
Log walk filtered to a single attribute
This file contains 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
;; bin/run log_as.clj DB-URI t attr | |
;; This program walks the log extracting datoms about a particular attribute. | |
;; Reveals domain data, but only about that one attribute. | |
;; Scans log from t to the end, so run against a backup instead of production if possible! | |
(require | |
'[clojure.edn :as edn] | |
'[clojure.pprint :as pp] | |
'[datomic.api :as d]) | |
(defn log-as* | |
[conn t attr] | |
(let [log (d/log conn) | |
db (d/db conn) | |
instid (d/entid db :db/txInstant) | |
aid (d/entid db attr)] | |
(->> (d/tx-range log t nil) | |
(map (fn [tx] | |
{:t (:t tx) | |
:inst (->> (:data tx) | |
(filter (fn [{:keys [a]}] | |
(= a instid))) | |
first | |
:v) | |
:data (->> (:data tx) | |
(filter (fn [{:keys [a]}] | |
(= a aid))) | |
(into []))}))))) | |
(defn log-as | |
"Prints maps with | |
t transaction t | |
inst transaction db/txInstant | |
data datoms whose A is attr | |
running from t passed in to end of data." | |
[uri t attr] | |
(let [conn (d/connect uri)] | |
(binding [*print-length* nil] | |
(pp/pprint | |
(log-as* conn t attr))))) | |
(try | |
(let [[db-uri t attr] *command-line-args*] | |
(log-as db-uri (edn/read-string t) (edn/read-string attr))) | |
(finally | |
(d/shutdown true))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment