aboutsummaryrefslogtreecommitdiff
path: root/src/com/github/ivarref/yoltq/migrate.clj
blob: c97f67920ad637b19752cc74525196487a7a69b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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*))