|
5 | 5 | [wanderung.datahike :as wd] |
6 | 6 | [wanderung.datom :as datom] |
7 | 7 | [clojure.tools.cli :refer [parse-opts]] |
8 | | - [clojure.string :refer [split]] |
9 | 8 | [clojure.java.io :as io] |
10 | 9 | [taoensso.nippy :as nippy]) |
11 | | - (:gen-class)) |
| 10 | + (:import [clojure.lang IExceptionInfo])) |
12 | 11 |
|
13 | 12 | ;;;------- Basic datoms interface ------- |
14 | 13 |
|
|
94 | 93 | slurp |
95 | 94 | read-string)) |
96 | 95 |
|
97 | | -(defn execute-migration [options] |
98 | | - (let [{:keys [source target help check]} options |
99 | | - src-cfg (load-config source) |
100 | | - tgt-cfg (load-config target) |
101 | | - src-type (:wanderung/type src-cfg) |
102 | | - tgt-type (:wanderung/type tgt-cfg)] |
103 | | - (cond |
104 | | - (not (multimethod-for-dispatch-value? datoms-from-storage src-type)) |
105 | | - (println "Cannot use" src-type "as source database.") |
106 | | - |
107 | | - (not (multimethod-for-dispatch-value? datoms-to-storage tgt-type)) |
108 | | - (println "Cannot use" tgt-type "as target database.") |
109 | | - |
110 | | - (:wanderung/read-only? tgt-cfg) |
111 | | - (println "Cannot migrate to read-only database.") |
112 | | - |
113 | | - :default (do |
114 | | - (println "➜ Start migrating from" src-type "to" tgt-type "...") |
115 | | - (migrate src-cfg tgt-cfg) |
116 | | - (println " ✓ Done") |
117 | | - (when check |
118 | | - (if (multimethod-for-dispatch-value? datoms-from-storage tgt-type) |
119 | | - (do |
120 | | - (println "➜ Comparing datoms between source and target...") |
121 | | - (if (datom/similar-datoms? (datoms-from-storage src-cfg) |
122 | | - (datoms-from-storage tgt-cfg)) |
123 | | - (println " ✓ Success: Datoms look the same.") |
124 | | - (println "ERROR: The datoms differ between source and target."))) |
125 | | - (println "ERROR: The target does not support reading datoms"))))))) |
| 96 | +(defn help |
| 97 | + ([] |
| 98 | + (help {})) |
| 99 | + ([_] |
| 100 | + (println "WANDERUNG") |
| 101 | + (println "---------") |
| 102 | + (println "Run migrations with Datahike to and from various sources") |
| 103 | + (println "USAGE:") |
| 104 | + (println "clj -Twanderung [function] [function args]") |
| 105 | + (println "FUNCTIONS:") |
| 106 | + (println "---------") |
| 107 | + (println "migration :source SOURCE :target TARGET") |
| 108 | + (println "Description: Migrates from given source file to a target file.") |
| 109 | + (println "Example: clj -Twanderung migration :source '\"./source-cfg.edn\"' :target '\"target-cfg.edn\"'") |
| 110 | + (println "---------") |
| 111 | + (println "help") |
| 112 | + (println "Description: Prints this lovely help.") |
| 113 | + (println "Example: clj -Twanderung help"))) |
| 114 | + |
| 115 | +(defn migration [{:keys [source target check] show-help :help}] |
| 116 | + (if show-help |
| 117 | + (help) |
| 118 | + (let [src-cfg (load-config source) |
| 119 | + tgt-cfg (load-config target) |
| 120 | + src-type (:wanderung/type src-cfg) |
| 121 | + tgt-type (:wanderung/type tgt-cfg)] |
| 122 | + (cond |
| 123 | + (not (multimethod-for-dispatch-value? datoms-from-storage src-type)) |
| 124 | + (println "Cannot use" src-type "as source database.") |
| 125 | + |
| 126 | + (not (multimethod-for-dispatch-value? datoms-to-storage tgt-type)) |
| 127 | + (println "Cannot use" tgt-type "as target database.") |
| 128 | + |
| 129 | + (:wanderung/read-only? tgt-cfg) |
| 130 | + (println "Cannot migrate to read-only database.") |
| 131 | + |
| 132 | + :else (do |
| 133 | + (println "➜ Start migrating from" src-type "to" tgt-type "...") |
| 134 | + (migrate src-cfg tgt-cfg) |
| 135 | + (println " ✓ Done") |
| 136 | + (when check |
| 137 | + (if (multimethod-for-dispatch-value? datoms-from-storage tgt-type) |
| 138 | + (do |
| 139 | + (println "➜ Comparing datoms between source and target...") |
| 140 | + (if (datom/similar-datoms? (datoms-from-storage src-cfg) |
| 141 | + (datoms-from-storage tgt-cfg)) |
| 142 | + (println " ✓ Success: Datoms look the same.") |
| 143 | + (println "ERROR: The datoms differ between source and target."))) |
| 144 | + (println "ERROR: The target does not support reading datoms")))))))) |
126 | 145 |
|
127 | 146 | (defn -main [& args] |
128 | 147 | (let [{options :options |
|
135 | 154 | (println "Run migrations to datahike from various sources") |
136 | 155 | (println "USAGE:") |
137 | 156 | (println summary)) |
138 | | - (execute-migration options))))) |
| 157 | + (try |
| 158 | + (migration options) |
| 159 | + (catch Throwable t |
| 160 | + (println (.getMessage t)) |
| 161 | + (when-not (instance? IExceptionInfo t) |
| 162 | + (.printStackTrace t)) |
| 163 | + (System/exit 1)) |
| 164 | + (finally |
| 165 | + (shutdown-agents))))))) |
0 commit comments