-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
159 lines (117 loc) · 4.06 KB
/
core.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
159
(ns automigrate.core
"Public interface for lib's users."
(:require [clojure.spec.alpha :as s]
[clojure.string :as str]
[slingshot.slingshot :refer [try+]]
[automigrate.migrations :as migrations]
[automigrate.util.spec :as spec-util]
[automigrate.util.file :as file-util]
[automigrate.errors :as errors]
[automigrate.help :as automigrate-help])
(:refer-clojure :exclude [list]))
(s/def ::models-file string?)
(s/def ::migrations-dir string?)
(s/def ::jdbc-url (s/conformer str))
(s/def ::number int?)
(s/def ::cmd (set automigrate-help/HELP-CMDS-ORDER))
(s/def ::format
(s/and
(s/conformer keyword)
#{:sql :human}))
(s/def ::type
(s/and
(s/conformer keyword)
#{migrations/EMPTY-SQL-MIGRATION-TYPE}))
(s/def ::name (s/conformer name))
(s/def ::migrations-table
(s/and
string?
(s/conformer
(fn [v]
(keyword (str/replace v #"_" "-"))))))
(s/def ::direction
(s/and
(s/conformer keyword)
#{:forward :backward}))
(s/def ::make-args
(s/keys
:req-un [::models-file
::migrations-dir]
:opt-un [::type
::name]))
(s/def ::migrate-args
(s/keys
:req-un [::jdbc-url
::migrations-dir]
:opt-un [::number
::migrations-table]))
(s/def ::explain-args
(s/keys
:req-un [::migrations-dir
::number]
:opt-un [::direction
::format]))
(s/def ::list-args
(s/keys
:req-un [::jdbc-url
::migrations-dir]
:opt-un [::migrations-table]))
(s/def ::help-args
(s/keys
:opt-un [::cmd]))
(defn- run-fn
[f args args-spec]
(try+
(let [args* (spec-util/conform args-spec args)]
(f args*))
(catch [:type ::s/invalid] e
(file-util/prn-err e))
(catch Object e
(let [message (or (ex-message e) (str e))]
(-> {:title "UNEXPECTED ERROR"
:message message}
(errors/custom-error->error-report)
(file-util/prn-err))))))
; Public interface
(defn make
"Create a new migration based on changes to the models.
Available options:
:models-file - Path to the file with model definitions. (required)
:migrations-dir - Path to directory containing migration files. (required)
:name - Custom name for a migration. (optional)
:type - Type of a new migration, empty by default for auto-migration.
Also available `:empty-sql` - for creating an empty raw SQL migration. (optional)"
[args]
(run-fn migrations/make-migration args ::make-args))
(defn migrate
"Run existing migrations and change the database schema.
Available options:
:jdbc-url - JDBC url for the database connection. (required)
:migrations-dir - Path to directory containing migration files. (required)
:migrations-table - Custom name for the migrations table in the database. (optional)
:number - Integer number of the target migration. (optional)"
[args]
(run-fn migrations/migrate args ::migrate-args))
(defn explain
"Show raw SQL or human-readable description for a migration by number.
Available options:
:migrations-dir - Path to directory containing migration files. (required)
:number - Integer number of the migration to explain. (required)
:direction - Direction of the migration to explain, can be `forward` (default) or `backward`. (optional)
:format - Format of explanation, can be `sql` (default) or `human`. (optional)"
[args]
(run-fn migrations/explain args ::explain-args))
(defn list
"Show the list of existing migrations with status.
Available options:
:jdbc-url - JDBC url for the database connection. (required)
:migrations-dir - Path to directory containing migration files. (required)
:migrations-table - Custom name for the migrations table in the database. (optional)"
[args]
(run-fn migrations/list-migrations args ::list-args))
(defn help
"Help information for all commands of automigrate tool.
Available options:
:cmd - Command name to display help information for a specific command. (optional)"
[args]
(run-fn automigrate-help/show-help! args ::help-args))