-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
migrations.clj
158 lines (139 loc) · 5.97 KB
/
migrations.clj
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
(ns migratus.migrations
(:require [clojure.java.io :as io]
[clojure.string :as str]
[clojure.tools.logging :as log]
migratus.migration.edn
migratus.migration.sql
[migratus.protocols :as proto]
[migratus.utils :as utils])
(:import [java.io File StringWriter]
[java.util Date TimeZone]
[java.util.jar JarEntry JarFile]
java.text.SimpleDateFormat
java.util.regex.Pattern))
(defn ->kebab-case [s]
(-> (reduce
(fn [s c]
(if (and
(not-empty s)
(Character/isLowerCase (char (last s)))
(Character/isUpperCase (char c)))
(str s "-" c)
(str s c)))
"" s)
(str/replace #"[\s]+" "-")
(.replaceAll "_" "-")
(.toLowerCase)))
(defn- timestamp []
(let [fmt (doto (SimpleDateFormat. "yyyyMMddHHmmss ")
(.setTimeZone (TimeZone/getTimeZone "UTC")))]
(.format fmt (Date.))))
(defn parse-migration-id [id]
(try
(Long/parseLong id)
(catch Exception e
(log/error e (str "failed to parse migration id: " id)))))
(def migration-file-pattern #"^(\d+)-([^\.]+)((?:\.[^\.]+)+)$")
(defn valid-extension?
"Returns true if file-name extension matches one of the file extensions supported
by all migration protocols/multimethods implemented"
[file-name]
(-> (->> (proto/get-all-supported-extensions)
(interpose "|")
(apply str)
(format "^.*?\\.(%s)$"))
re-pattern
(re-matches file-name)
boolean))
(defn parse-name [file-name]
(when (valid-extension? file-name)
(when-let [[id name ext] (next (re-matches migration-file-pattern file-name))]
[id name (remove empty? (str/split ext #"\."))])))
(defn warn-on-invalid-migration [file-name]
(log/warn (str "skipping: '" file-name "'")
"migrations must match pattern:"
(str migration-file-pattern)))
(defn migration-map
[[id name exts] content]
(assoc-in {}
(concat [id name] (map keyword (reverse exts)))
content))
(defn find-migration-files [migration-dir exclude-scripts]
(log/debug "Looking for migrations in" migration-dir)
(->> (for [f (filter (fn [^File f] (.isFile f))
(file-seq migration-dir))
:let [file-name (.getName ^File f)]]
(if-let [mig (parse-name file-name)]
(migration-map mig (slurp f))
(when-not (exclude-scripts (.getName ^File f))
(warn-on-invalid-migration file-name))))
(remove nil?)))
(defn find-migration-resources [dir jar exclude-scripts]
(log/debug "Looking for migrations in" dir jar)
(->> (for [entry (enumeration-seq (.entries ^JarFile jar))
:when (.matches (.getName ^JarEntry entry)
(str "^" (Pattern/quote dir) ".+"))
:let [entry-name (.replaceAll (.getName ^JarEntry entry) dir "")]]
(if-let [mig (parse-name entry-name)]
(let [w (StringWriter.)]
(io/copy (.getInputStream ^JarFile jar entry) w)
(migration-map mig (.toString w)))
(when-not (exclude-scripts entry-name)
(warn-on-invalid-migration entry-name))))
(remove nil?)))
(defn read-migrations [dir exclude-scripts]
(when-let [migration-dir (utils/find-migration-dir dir)]
(if (instance? File migration-dir)
(find-migration-files migration-dir exclude-scripts)
(find-migration-resources dir migration-dir exclude-scripts))))
(defn find-migrations [dir exclude-scripts]
(->> (read-migrations (utils/ensure-trailing-slash dir) exclude-scripts)
(apply utils/deep-merge)))
(defn find-or-create-migration-dir
([dir] (find-or-create-migration-dir utils/default-migration-parent dir))
([parent-dir dir]
(if-let [migration-dir (utils/find-migration-dir dir)]
migration-dir
;; Couldn't find the migration dir, create it
(let [new-migration-dir (io/file parent-dir dir)]
(io/make-parents new-migration-dir ".")
new-migration-dir))))
(defn make-migration
"Constructs a Migration instance from the merged migration file maps collected
by find-migrations. Expected structure for `mig` is:
{`migration-name` {`migration-type` payload}} where the structure of `payload`
is specific to the migration type."
[config id mig]
(if-let [id (parse-migration-id id)]
(if (= 1 (count mig))
(let [[mig-name mig'] (first mig)]
(if (= 1 (count mig'))
(let [[mig-type payload] (first mig')]
(proto/make-migration* mig-type id mig-name payload config))
(throw (Exception.
(format
"Multiple migration types specified for migration %d %s"
id (pr-str (map name (keys mig'))))))))
(throw (Exception. (format "Multiple migrations with id %d %s"
id (pr-str (keys mig))))))
(throw (Exception. (str "Invalid migration id: " id)))))
(defn list-migrations [config]
(doall
(for [[id mig] (find-migrations (utils/get-migration-dir config)
(utils/get-exclude-scripts config))]
(make-migration config id mig))))
(defn create [config name migration-type]
(let [migration-dir (find-or-create-migration-dir
(utils/get-parent-migration-dir config)
(utils/get-migration-dir config))
migration-name (->kebab-case (str (timestamp) name))]
(doseq [mig-file (proto/migration-files* migration-type migration-name)]
(.createNewFile (io/file migration-dir mig-file)))))
(defn destroy [config name]
(let [migration-dir (utils/find-migration-dir
(utils/get-migration-dir config))
migration-name (->kebab-case name)
pattern (re-pattern (str "[\\d]*-" migration-name "\\..*"))
migrations (file-seq migration-dir)]
(doseq [f (filter #(re-find pattern (.getName ^File %)) migrations)]
(.delete ^File f))))