|
93 | 93 | slurp |
94 | 94 | read-string)) |
95 | 95 |
|
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")))))))) |
| 96 | +(defn migration [{:keys [source target check]}] |
| 97 | + (let [read-cfg (fn [cfg] (when (some? cfg) |
| 98 | + (if-let [path-from-env (some? (System/getenv cfg))] |
| 99 | + path-from-env |
| 100 | + (when (.exists (io/file cfg)) |
| 101 | + cfg))))] |
| 102 | + (if-let [source (read-cfg source)] |
| 103 | + (if-let [target (read-cfg target)] |
| 104 | + (let [src-cfg (load-config source) |
| 105 | + tgt-cfg (load-config target) |
| 106 | + src-type (:wanderung/type src-cfg) |
| 107 | + tgt-type (:wanderung/type tgt-cfg)] |
| 108 | + (cond |
| 109 | + (not (multimethod-for-dispatch-value? datoms-from-storage src-type)) |
| 110 | + (println "Cannot use" src-type "as source database.") |
| 111 | + |
| 112 | + (not (multimethod-for-dispatch-value? datoms-to-storage tgt-type)) |
| 113 | + (println "Cannot use" tgt-type "as target database.") |
| 114 | + |
| 115 | + (:wanderung/read-only? tgt-cfg) |
| 116 | + (println "Cannot migrate to read-only database.") |
| 117 | + |
| 118 | + :else (do |
| 119 | + (println "➜ Start migrating from" src-type "to" tgt-type "...") |
| 120 | + (migrate src-cfg tgt-cfg) |
| 121 | + (println " ✓ Done") |
| 122 | + (when check |
| 123 | + (if (multimethod-for-dispatch-value? datoms-from-storage tgt-type) |
| 124 | + (do |
| 125 | + (println "➜ Comparing datoms between source and target...") |
| 126 | + (if (datom/similar-datoms? (datoms-from-storage src-cfg) |
| 127 | + (datoms-from-storage tgt-cfg)) |
| 128 | + (println " ✓ Success: Datoms look the same.") |
| 129 | + (println "ERROR: The datoms differ between source and target."))) |
| 130 | + (println "ERROR: The target does not support reading datoms")))))) |
| 131 | + (println "ERORR: Invalid target configuration. Must be either file path or environment variable.")) |
| 132 | + (println "ERORR: Invalid source configuration. Must be either file path or environment variable.")))) |
145 | 133 |
|
146 | 134 | (defn -main [& args] |
147 | 135 | (let [{options :options |
|
0 commit comments