diff options
| author | EuAndreh <eu@euandre.org> | 2025-12-05 19:57:57 -0300 |
|---|---|---|
| committer | EuAndreh <eu@euandre.org> | 2025-12-05 19:57:57 -0300 |
| commit | e1b99db2364971d456a62e8ec7567e6b5250d004 (patch) | |
| tree | 8b1b7772cc44d90b97856d5d02481f3649e65908 /src/com/github/ivarref/yoltq/migrate.clj | |
| parent | rm -rf *: setup change to Clojure (diff) | |
| parent | Update for release (diff) | |
| download | fiinha-e1b99db2364971d456a62e8ec7567e6b5250d004.tar.gz fiinha-e1b99db2364971d456a62e8ec7567e6b5250d004.tar.xz | |
Merge remote-tracking branch 'upstream/main', where "upstream" is yoltq.
Diffstat (limited to 'src/com/github/ivarref/yoltq/migrate.clj')
| -rw-r--r-- | src/com/github/ivarref/yoltq/migrate.clj | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/com/github/ivarref/yoltq/migrate.clj b/src/com/github/ivarref/yoltq/migrate.clj new file mode 100644 index 0000000..c97f679 --- /dev/null +++ b/src/com/github/ivarref/yoltq/migrate.clj @@ -0,0 +1,61 @@ +(ns com.github.ivarref.yoltq.migrate + (:require [datomic.api :as d] + [clojure.tools.logging :as log])) + +(defn to->v2-ent [{:keys [conn]} now-ms id] + (log/info "Migrating id" id) + (let [attr-val (fn [attr] + (when-let [old (d/q '[:find ?time . + :in $ ?e ?a + :where + [?e ?a ?time]] + (d/db conn) + [:com.github.ivarref.yoltq/id id] + attr)] + (let [now-ms (or now-ms + (.getTime (d/q '[:find (max ?txinst) . + :in $ ?e ?a + :where + [?e ?a _ ?tx true] + [?tx :db/txInstant ?txinst]] + (d/history (d/db conn)) + [:com.github.ivarref.yoltq/id id] + attr)))] + (log/info "Updating" id attr "to" now-ms) + [[:db/cas [:com.github.ivarref.yoltq/id id] + attr old now-ms]])))] + (vec (concat [[:db/cas [:com.github.ivarref.yoltq/id id] + :com.github.ivarref.yoltq/version nil "2"]] + (mapcat attr-val [:com.github.ivarref.yoltq/init-time + :com.github.ivarref.yoltq/processing-time + :com.github.ivarref.yoltq/done-time + :com.github.ivarref.yoltq/error-time]))))) + +(defn to->v2 [{:keys [conn loop? now-ms] + :or {loop? true} + :as cfg}] + (loop [tx-vec []] + (if-let [id (some->> (d/q '[:find [?id ...] + :in $ + :where + [?e :com.github.ivarref.yoltq/id ?id] + [(missing? $ ?e :com.github.ivarref.yoltq/version)]] + (d/db conn)) + (sort) + (not-empty) + (first))] + (let [tx (to->v2-ent cfg now-ms id)] + @(d/transact conn tx) + (if loop? + (recur (vec (take 10 (conj tx-vec tx)))) + tx)) + (do + (log/info "No items left to migrate") + tx-vec)))) + + +(defn migrate! [cfg] + (to->v2 cfg)) + +(comment + (migrate! @com.github.ivarref.yoltq/*config*)) |
