-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrations.clj
1034 lines (860 loc) · 39.1 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(ns automigrate.migrations
"Module for applying changes to migrations and db.
Also contains tools for inspection of db state by migrations
and state of migrations itself."
(:require [next.jdbc :as jdbc]
#_{:clj-kondo/ignore [:unused-namespace]}
[clojure.spec.alpha :as s]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.set :as set]
[clojure.pprint :as pprint]
#_{:clj-kondo/ignore [:unused-referred-var]}
[slingshot.slingshot :refer [throw+ try+]]
[differ.core :as differ]
[weavejester.dependency :as dep]
[automigrate.actions :as actions]
[automigrate.models :as models]
[automigrate.fields :as fields]
[automigrate.errors :as errors]
[automigrate.sql :as sql]
[automigrate.schema :as schema]
[automigrate.util.file :as file-util]
[automigrate.util.db :as db-util]
[automigrate.util.spec :as su]
[automigrate.util.model :as model-util])
(:import [java.io FileNotFoundException]))
(def ^:private DROPPED-ENTITY-VALUE 0)
(def ^:private DEFAULT-ROOT-NODE :root)
(def ^:private AUTO-MIGRATION-PREFIX "auto")
(def ^:private AUTO-MIGRATION-POSTFIX "etc")
(def ^:private FORWARD-DIRECTION :forward)
(def ^:private BACKWARD-DIRECTION :backward)
(def ^:private AUTO-MIGRATION-EXT :edn)
(def ^:private SQL-MIGRATION-EXT :sql)
(def ^:private EXPLAIN-FORMAT-SQL :sql)
(def ^:private EXPLAIN-FORMAT-HUMAN :human)
(def EMPTY-SQL-MIGRATION-TYPE :empty-sql)
(def ^:private FORWARD-MIGRATION-DELIMITER "-- FORWARD")
(def ^:private BACKWARD-MIGRATION-DELIMITER "-- BACKWARD")
(def ^:private LIST-SIGN-COMPLETED "x")
(def ^:private SQL-MIGRATION-TEMPLATE
(format "%s\n\n\n%s\n" FORWARD-MIGRATION-DELIMITER BACKWARD-MIGRATION-DELIMITER))
(defn- get-migration-type
"Return migration type by migration file extension."
[migration-name]
(-> (str/split migration-name #"\.")
(last)
(keyword)))
(defn- get-forward-sql-migration
[migration]
(-> (str/split migration (re-pattern BACKWARD-MIGRATION-DELIMITER))
(first)
(str/replace (re-pattern FORWARD-MIGRATION-DELIMITER) "")
(vector)))
(defn- get-backward-sql-migration
[migration]
(-> (str/split migration (re-pattern BACKWARD-MIGRATION-DELIMITER))
(last)
(vector)))
(defn- create-migrations-dir
"Create migrations root dir if it is not exist."
[migrations-dir]
(when-not (.isDirectory (io/file migrations-dir))
(.mkdir (java.io.File. migrations-dir))))
(defn- save-migration!
"Save migration to db after applying it."
[db migration-name migrations-table]
(->> {:insert-into migrations-table
:values [{:name migration-name}]}
(db-util/exec! db))
(println (str migration-name " successfully applied.")))
(defn- delete-migration!
"Delete reverted migration from db."
[db migration-name migrations-table]
(->> {:delete-from migrations-table
:where [:= :name migration-name]}
(db-util/exec! db))
(println (str migration-name " successfully reverted.")))
(defn- get-migration-name
"Return migration name without file format."
[file-name]
(first (str/split file-name #"\.")))
(defn- get-migration-number
[migration-name]
(-> (str/split migration-name #"_")
(first)
(Integer/parseInt)))
(defn- validate-migration-numbers
[migrations]
(let [duplicated-numbers (->> migrations
(map get-migration-number)
(frequencies)
(filter #(> (val %) 1))
(keys)
(set))]
(when (seq duplicated-numbers)
(throw+ {:type ::duplicated-migration-numbers
:numbers duplicated-numbers
:message (str "There are duplicated migration numbers: "
(str/join ", " duplicated-numbers)
". Please resolve the conflict and try again.")}))
migrations))
(defn- migrations-list
"Get migrations' files list."
[migrations-dir]
(->> (file-util/list-files migrations-dir)
(map #(.getName %))
(sort)
(validate-migration-numbers)))
(defn- next-migration-number
[file-names]
; migration numbers starting from 1
(file-util/zfill (inc (count file-names))))
(defn- extract-item-name
[action]
(condp contains? (:action action)
#{actions/CREATE-TABLE-ACTION
actions/DROP-TABLE-ACTION} (:model-name action)
#{actions/ADD-COLUMN-ACTION
actions/DROP-COLUMN-ACTION
actions/ALTER-COLUMN-ACTION} (:field-name action)
#{actions/CREATE-INDEX-ACTION
actions/DROP-INDEX-ACTION
actions/ALTER-INDEX-ACTION} (:index-name action)
#{actions/CREATE-TYPE-ACTION
actions/DROP-TYPE-ACTION
actions/ALTER-TYPE-ACTION} (:type-name action)))
(defn- get-action-description-vec-basic
[action]
(let [action-name (-> action :action name (str/replace #"-" "_") (str/split #"_"))
item-name (-> action extract-item-name name (str/replace #"-" "_"))]
(conj action-name item-name)))
(defn- get-action-description-vec-with-table
[action preposition]
(let [action-desc-basic (get-action-description-vec-basic action)
model-name (-> action :model-name name (str/replace #"-" "_"))]
(conj action-desc-basic preposition model-name)))
(defn- get-action-description-vec
[action]
(condp contains? (:action action)
#{actions/ADD-COLUMN-ACTION} (get-action-description-vec-with-table action "to")
#{actions/ALTER-COLUMN-ACTION} (get-action-description-vec-with-table action "in")
#{actions/DROP-COLUMN-ACTION} (get-action-description-vec-with-table action "from")
#{actions/CREATE-INDEX-ACTION
actions/ALTER-INDEX-ACTION
actions/DROP-INDEX-ACTION} (get-action-description-vec-with-table action "on")
; default
(get-action-description-vec-basic action)))
(defn- get-next-migration-name-auto
[actions]
(let [first-action (first actions)
action-desc-vec (get-action-description-vec first-action)
action-desc-vec* (cond-> (concat [AUTO-MIGRATION-PREFIX] action-desc-vec)
(> (count actions) 1) (concat [AUTO-MIGRATION-POSTFIX]))]
(str/join #"_" action-desc-vec*)))
(defn- get-next-migration-name
"Return given custom name with underscores or first action name."
[actions custom-name]
(let [migration-name (or custom-name (get-next-migration-name-auto actions))]
(str/replace migration-name #"-" "_")))
(defn- new-field?
[old-model fields-diff field-name]
(and (contains? fields-diff field-name)
(not (contains? (:fields old-model) field-name))))
(defn- drop-field?
[fields-removals field-name]
(= DROPPED-ENTITY-VALUE (get fields-removals field-name)))
(defn- options-dropped
[removals]
(-> (filter #(= DROPPED-ENTITY-VALUE (val %)) removals)
(keys)
(set)
(set/difference #{:type})))
(defn- options-added
"Get field options with deletion changes.
Example:
- old: `{:type [:decimal 10 2]}`
- new: `{:type [:decimal 10]}`"
[{:keys [to-add to-drop new-field]}]
(reduce-kv
(fn [m k v]
(cond-> m
; Check diff value from differ not 0, then add new value for option
; to be able to see it in `:changes` key of migration action.
(not= DROPPED-ENTITY-VALUE v) (assoc k (get new-field k))))
to-add
to-drop))
(defn- assoc-option-to-add
[old-field changes option-key new-option-value]
(let [old-option-value (if (contains? old-field option-key)
(get old-field option-key)
model-util/EMPTY-OPTION)]
(-> changes
(assoc-in [option-key :from] old-option-value)
(assoc-in [option-key :to] new-option-value))))
(defn- assoc-option-to-drop
[old-field changes option-key]
(-> changes
(assoc-in [option-key :from] (get old-field option-key))
(assoc-in [option-key :to] model-util/EMPTY-OPTION)))
(defn- get-changes
[old-options options-to-add options-to-drop]
(as-> {} $
(reduce-kv (partial assoc-option-to-add old-options) $ options-to-add)
(reduce (partial assoc-option-to-drop old-options) $ options-to-drop)))
(defn- get-options-to-add
"Update option in diff to option from new model if diff is a vector.
It is a caveat how differ lib works with changes in vectors. For example, it uses
in case when we change field type [:varchar 100] to [:varchar 200]. In diff we see [1 200]
cause just second item of a vector has been changed. So for us it is important to have whole
new type in options to add, and we just copy it from new model."
[fields-diff field-name new-model]
(let [field-options-diff (get fields-diff field-name)
fields-options-new (get-in new-model [:fields field-name])]
(reduce-kv
(fn [m k v]
(if (vector? v)
(assoc m k (get fields-options-new k))
(assoc m k v)))
{}
field-options-diff)))
(defn- parse-fields-diff
"Return field's migrations for model."
[{:keys [model-diff removals old-model new-model model-name]}]
(let [fields-diff (:fields model-diff)
fields-removals (:fields removals)
changed-fields (-> (set (keys fields-diff))
(set/union (set (keys fields-removals))))]
(for [field-name changed-fields
:let [options-to-add (get-options-to-add fields-diff field-name new-model)
options-to-drop (get fields-removals field-name)
new-field?* (new-field? old-model fields-diff field-name)
drop-field?* (drop-field? fields-removals field-name)
field-options-old (get-in old-model [:fields field-name])
field-options-new (get-in new-model [:fields field-name])]]
(cond
new-field?* {:action actions/ADD-COLUMN-ACTION
:field-name field-name
:model-name model-name
:options options-to-add}
drop-field?* {:action actions/DROP-COLUMN-ACTION
:field-name field-name
:model-name model-name}
:else {:action actions/ALTER-COLUMN-ACTION
:field-name field-name
:model-name model-name
:options field-options-new
:changes (get-changes field-options-old
(options-added
{:to-add options-to-add
:to-drop options-to-drop
:new-field field-options-new})
(options-dropped options-to-drop))}))))
(defn- new-model?
[alterations old-schema model-name]
(and (contains? alterations model-name)
(not (contains? old-schema model-name))))
(defn- drop-model?
[removals model-name]
(= DROPPED-ENTITY-VALUE (get removals model-name)))
(defn- read-models
"Read and validate models from file."
[models-file]
(->> models-file
(file-util/read-edn)
(models/->internal-models)))
(s/def ::model-name keyword?)
(s/def ::field-name keyword?)
(s/def ::type-name keyword?)
(s/def ::action-dependencies-ret-item
(s/keys
:opt-un [::model-name
::field-name
::type-name]))
(s/def ::action-dependencies-ret
(s/coll-of ::action-dependencies-ret-item))
(defn- action-dependencies
"Return dependencies as vector of vectors for an action or nil."
[action]
{:pre [(su/assert! map? action)]
:post [(su/assert! ::action-dependencies-ret %)]}
(let [changes-to-add (model-util/changes-to-add (:changes action))
fk (condp contains? (:action action)
#{actions/ADD-COLUMN-ACTION} (get-in action [:options :foreign-key])
#{actions/ALTER-COLUMN-ACTION} (:foreign-key changes-to-add)
nil)
type-def (condp contains? (:action action)
#{actions/ADD-COLUMN-ACTION} (get-in action [:options :type])
#{actions/ALTER-COLUMN-ACTION} (:type changes-to-add)
nil)]
(->> (condp contains? (:action action)
#{actions/ADD-COLUMN-ACTION
actions/ALTER-COLUMN-ACTION} (cond-> [{:model-name (:model-name action)}]
(some? fk) (conj (model-util/kw->map fk))
(s/valid? ::fields/enum-type type-def)
(conj {:type-name (last type-def)}))
#{actions/DROP-COLUMN-ACTION} (cond-> [{:model-name (:model-name action)}]
(some? fk) (conj (model-util/kw->map fk)))
#{actions/CREATE-TABLE-ACTION} (mapv
(fn [field]
(cond
(contains? field :foreign-key)
(-> field
:foreign-key
model-util/kw->map)
(s/valid? ::fields/enum-type (:type field))
{:type-name (-> field :type last)}))
(vals (:fields action)))
#{actions/CREATE-INDEX-ACTION
actions/ALTER-INDEX-ACTION} (mapv (fn [field]
{:model-name (:model-name action)
:field-name field})
(get-in action [:options :fields]))
#{actions/DROP-TYPE-ACTION} [{:type-name (:type-name action)}]
[])
(remove nil?))))
(defn- parent-action?
"Check if action is parent to one with presented dependencies."
[old-schema deps next-action action]
(let [model-names (set (map :model-name deps))
type-names (set (map :type-name deps))]
(if (= next-action action)
false
(condp contains? (:action action)
#{actions/CREATE-TABLE-ACTION} (contains? model-names (:model-name action))
#{actions/ADD-COLUMN-ACTION
actions/ALTER-COLUMN-ACTION} (some
#(and (= (:model-name action) (:model-name %))
(= (:field-name action) (:field-name %)))
deps)
; First, drop enum column, then drop enum.
#{actions/DROP-COLUMN-ACTION} (let [field-type (get-in old-schema
[(:model-name action)
:fields
(:field-name action)
:type])]
(and (s/valid? ::fields/enum-type field-type)
(contains? type-names (last field-type))))
; First, drop table with enum column, then drop enum.
#{actions/DROP-TABLE-ACTION} (let [fields (get-in old-schema
[(:model-name action)
:fields])
field-types (mapv :type (vals fields))]
(some
#(and (s/valid? ::fields/enum-type %)
(contains? type-names (last %)))
field-types))
; First, create/alter enum type, then add/alter column/table
#{actions/CREATE-TYPE-ACTION
actions/ALTER-TYPE-ACTION} (contains? type-names (:type-name action))
false))))
(defn- assoc-action-deps
"Assoc dependencies to graph by actions."
[old-schema actions graph next-action]
(let [deps (action-dependencies next-action)
parent-actions (filter
(partial parent-action? old-schema deps next-action)
actions)]
(as-> graph $
(dep/depend $ next-action DEFAULT-ROOT-NODE)
(reduce #(dep/depend %1 next-action %2) $ parent-actions))))
(defn- compare-actions
"Secondary comparator for sorting actions in migration the same way."
[a b]
(< (hash a) (hash b)))
(defn- sort-actions
"Apply order for migration's actions by foreign key between models."
[old-schema actions]
(->> actions
(reduce (partial assoc-action-deps old-schema actions) (dep/graph))
(dep/topo-sort compare-actions)
; drop first default root node `:root`
(drop 1)))
(defn- new-index?
[old-model indexes-diff index-name]
(and (contains? indexes-diff index-name)
(not (contains? (:indexes old-model) index-name))))
(defn- drop-index?
[indexes-removals index-name]
(= DROPPED-ENTITY-VALUE (get indexes-removals index-name)))
(defn- parse-indexes-diff
"Return index's migrations for model."
[model-diff removals old-model new-model model-name]
(let [indexes-diff (:indexes model-diff)
indexes-removals (if (= DROPPED-ENTITY-VALUE (:indexes removals))
(->> (:indexes old-model)
(reduce-kv (fn [m k _v] (assoc m k DROPPED-ENTITY-VALUE)) {}))
(:indexes removals))
changed-indexes (-> (set (keys indexes-diff))
(set/union (set (keys indexes-removals))))]
(for [index-name changed-indexes
:let [options-to-add (get indexes-diff index-name)
options-to-alter (get-in new-model [:indexes index-name])
new-index?* (new-index? old-model indexes-diff index-name)
drop-index?* (drop-index? indexes-removals index-name)]]
(cond
new-index?* {:action actions/CREATE-INDEX-ACTION
:index-name index-name
:model-name model-name
:options options-to-add}
drop-index?* {:action actions/DROP-INDEX-ACTION
:index-name index-name
:model-name model-name}
:else {:action actions/ALTER-INDEX-ACTION
:index-name index-name
:model-name model-name
:options options-to-alter}))))
(defn- new-type?
[old-model types-diff type-name]
(and (contains? types-diff type-name)
(not (contains? (:types old-model) type-name))))
(defn- drop-type?
[types-removals type-name type-from-dropped-model?]
(or
(= DROPPED-ENTITY-VALUE (get types-removals type-name))
type-from-dropped-model?))
(defn- parse-types-diff
"Return type's migrations for model."
[{:keys [model-diff model-removals old-model new-model model-name
type-from-dropped-model?]}]
; TODO: try to abstract this function for types/indexes/fields
(let [types-diff (:types model-diff)
types-removals (if (or (= DROPPED-ENTITY-VALUE (:types model-removals))
type-from-dropped-model?)
(->> (:types old-model)
(reduce-kv (fn [m k _v] (assoc m k DROPPED-ENTITY-VALUE)) {}))
(:types model-removals))
changed-types (-> (set (keys types-diff))
(set/union (set (keys types-removals))))]
(for [type-name changed-types
:let [options-to-add (get types-diff type-name)
new-type?* (new-type? old-model types-diff type-name)
drop-type?* (drop-type? types-removals type-name type-from-dropped-model?)
type-options-old (get-in old-model [:types type-name])
type-options-new (get-in new-model [:types type-name])]]
(cond
new-type?* {:action actions/CREATE-TYPE-ACTION
:type-name type-name
:model-name model-name
:options options-to-add}
drop-type?* {:action actions/DROP-TYPE-ACTION
:type-name type-name
:model-name model-name}
:else {:action actions/ALTER-TYPE-ACTION
:type-name type-name
:model-name model-name
:options type-options-new
:changes (get-changes
type-options-old
(options-added
{:to-add (dissoc type-options-new :type)})
#{})}))))
(defn- make-migration*
[old-schema new-schema]
(let [[alterations removals] (differ/diff old-schema new-schema)
changed-models (-> (set (keys alterations))
(set/union (set (keys removals))))
actions (for [model-name changed-models
:let [old-model (get old-schema model-name)
new-model (get new-schema model-name)
model-diff (get alterations model-name)
model-removals (get removals model-name)
new-model?* (new-model? alterations old-schema model-name)
drop-model?* (drop-model? removals model-name)]]
(concat
(cond
new-model?* [{:action actions/CREATE-TABLE-ACTION
:model-name model-name
:fields (:fields model-diff)}]
drop-model?* [{:action actions/DROP-TABLE-ACTION
:model-name model-name}]
:else (parse-fields-diff {:model-diff model-diff
:removals model-removals
:old-model old-model
:new-model new-model
:model-name model-name}))
(parse-indexes-diff model-diff model-removals old-model new-model model-name)
(parse-types-diff
{:model-diff model-diff
:model-removals model-removals
:old-model old-model
:new-model new-model
:model-name model-name
:type-from-dropped-model? drop-model?*})))]
(->> actions
(flatten)
(sort-actions old-schema)
(actions/->migrations))))
(defmulti migration->actions (juxt :migration-type :direction))
(defmethod migration->actions [AUTO-MIGRATION-EXT FORWARD-DIRECTION]
[{:keys [file-name migrations-dir]}]
(let [migration-file-path (file-util/join-path migrations-dir file-name)]
(file-util/read-edn migration-file-path)))
(defn- ->file
[file-name migrations-dir]
(io/file (file-util/join-path migrations-dir file-name)))
(defmethod migration->actions [AUTO-MIGRATION-EXT BACKWARD-DIRECTION]
[{:keys [migrations-dir number-int all-migrations]}]
(let [migrations-from (->> all-migrations
(take-while #(<= (:number-int %) number-int))
(filterv #(= AUTO-MIGRATION-EXT (:migration-type %)))
(map #(-> % :file-name (->file migrations-dir))))
schema-from (schema/current-db-schema migrations-from)
migrations-to (butlast migrations-from)
schema-to (schema/current-db-schema migrations-to)]
(make-migration* schema-from schema-to)))
(defmethod migration->actions [SQL-MIGRATION-EXT FORWARD-DIRECTION]
[{:keys [file-name migrations-dir]}]
(-> (file-util/join-path migrations-dir file-name)
(slurp)
(get-forward-sql-migration)))
(defmethod migration->actions [SQL-MIGRATION-EXT BACKWARD-DIRECTION]
[{:keys [file-name migrations-dir]}]
(-> (file-util/join-path migrations-dir file-name)
(slurp)
(get-backward-sql-migration)))
(defn- get-next-migration-file-name
"Return next migration file name based on existing migrations."
[{:keys [migration-type migrations-dir next-migration-name]}]
(let [migration-names (migrations-list migrations-dir)
migration-number (next-migration-number migration-names)
migration-file-name (str migration-number "_" next-migration-name)
migration-file-with-ext (str migration-file-name "." (name migration-type))]
(file-util/join-path migrations-dir migration-file-with-ext)))
(defn- auto-migration?
"Return true if migration has been created automatically false otherwise."
[f]
(str/ends-with? (.getName f) (str "." (name AUTO-MIGRATION-EXT))))
(defn- make-next-migration
"Return actions for next migration."
[{:keys [models-file migrations-dir]}]
(let [auto-migration-files (->> (file-util/list-files migrations-dir)
(filter auto-migration?))
old-schema (schema/current-db-schema auto-migration-files)
new-schema (read-models models-file)]
(-> (make-migration* old-schema new-schema)
(flatten)
(seq))))
(defn- get-action-name-verbose
[action]
(->> action
(get-action-description-vec)
(cons " -")
(str/join " ")))
(defn- print-action-names
[actions]
(let [action-names (mapv get-action-name-verbose actions)]
(file-util/safe-println (cons "Actions:" action-names) "")))
(defmulti make-migration :type)
(defmethod make-migration :default
; Make new migration based on models definitions automatically.
[{:keys [models-file migrations-dir]
custom-migration-name :name}]
(try+
(if-let [next-migration (make-next-migration {:models-file models-file
:migrations-dir migrations-dir})]
(let [_ (create-migrations-dir migrations-dir)
next-migration-name (get-next-migration-name next-migration custom-migration-name)
migration-file-name-full-path (get-next-migration-file-name
{:migration-type AUTO-MIGRATION-EXT
:migrations-dir migrations-dir
:next-migration-name next-migration-name})]
(spit migration-file-name-full-path
(with-out-str
(pprint/pprint next-migration)))
(println (str "Created migration: " migration-file-name-full-path))
; Print all actions from migration in human-readable format
(print-action-names next-migration))
(println "There are no changes in models."))
(catch [:type ::s/invalid] e
(file-util/prn-err e))
(catch #(contains? #{::models/missing-referenced-model
::models/missing-referenced-field
::models/referenced-field-is-not-unique
::models/fk-fields-have-different-types} (:type %)) e
(-> e
(errors/custom-error->error-report)
(file-util/prn-err)))
(catch [:reason ::dep/circular-dependency] e
(-> {:title "MIGRATION ERROR"
:message (format (str "Circular dependency between two migration actions: \n %s\nand\n %s\n\n"
"Please split actions by different migrations.")
(pr-str (:dependency e))
(pr-str (:node e)))}
(errors/custom-error->error-report)
(file-util/prn-err)))
(catch FileNotFoundException e
(-> {:title "ERROR"
:message (format "Missing file:\n\n %s" (ex-message e))}
(errors/custom-error->error-report)
(file-util/prn-err)))))
(defmethod make-migration EMPTY-SQL-MIGRATION-TYPE
; Make new migrations based on models definitions automatically.
[{next-migration-name :name
migrations-dir :migrations-dir}]
(try+
(when (empty? next-migration-name)
(throw+ {:type ::missing-migration-name
:message "Missing migration name."}))
(let [_ (create-migrations-dir migrations-dir)
next-migration-name* (str/replace next-migration-name #"-" "_")
migration-file-name-full-path (get-next-migration-file-name
{:migration-type SQL-MIGRATION-EXT
:migrations-dir migrations-dir
:next-migration-name next-migration-name*})]
(spit migration-file-name-full-path SQL-MIGRATION-TEMPLATE)
(println (str "Created migration: " migration-file-name-full-path)))
(catch [:type ::s/invalid] e
(file-util/prn-err e))
(catch #(contains? #{::missing-migration-name
::duplicated-migration-numbers} (:type %)) e
(-> e
(errors/custom-error->error-report)
(file-util/prn-err)))))
(defn- get-migration-by-number
"Return migration file name by number."
[migration-names number]
{:pre [(su/assert! (s/coll-of string?) migration-names)
(su/assert! integer? number)]}
(->> migration-names
(filter #(= number (get-migration-number %)))
(first)))
(defmulti explain*
(juxt :migration-type :explain-format))
(defn- add-transaction-to-explain
[actions]
(concat ["BEGIN"] actions ["COMMIT;"]))
(defn- detailed-migration
"Return detailed info for each migration file."
[file-name]
{:file-name file-name
:migration-name (get-migration-name file-name)
:number-int (get-migration-number file-name)
:migration-type (get-migration-type file-name)})
(defmethod explain* [AUTO-MIGRATION-EXT EXPLAIN-FORMAT-SQL]
[{:keys [actions]}]
(->> actions
(mapv sql/->sql)
(flatten)
(add-transaction-to-explain)
(file-util/safe-println)))
(defmethod explain* [AUTO-MIGRATION-EXT EXPLAIN-FORMAT-HUMAN]
[{:keys [actions]}]
(let [actions-explained (mapv get-action-name-verbose actions)]
(file-util/safe-println actions-explained "")))
(defmethod explain* [SQL-MIGRATION-EXT EXPLAIN-FORMAT-SQL]
[{:keys [actions]}]
(file-util/safe-println actions))
(defmethod explain* [SQL-MIGRATION-EXT EXPLAIN-FORMAT-HUMAN]
[_]
(println "Explain in human-readable format is not supported for custom SQL migration."))
(defn explain
"Generate raw sql or human-readable text from migration."
[{:keys [migrations-dir number direction]
explain-format :format
:or {direction FORWARD-DIRECTION
explain-format EXPLAIN-FORMAT-SQL}}]
(try+
(let [migration-names (migrations-list migrations-dir)
file-name (get-migration-by-number migration-names number)
format-title (condp = explain-format
:sql "SQL"
:human "Actions")]
(when-not (some? file-name)
(throw+ {:type ::no-migration-by-number
:number number
:message (format "Missing migration by number %s" (str number))}))
(file-util/safe-println
[(format "%s for %s migration %s:\n" format-title (name direction) file-name)])
(let [all-migrations (migrations-list migrations-dir)
all-migrations-detailed (map detailed-migration all-migrations)
migration-type (get-migration-type file-name)
actions (migration->actions {:file-name file-name
:migrations-dir migrations-dir
:migration-type migration-type
:direction direction
:number-int (get-migration-number file-name)
:all-migrations all-migrations-detailed})]
(explain* {:actions actions
:migration-type (get-migration-type file-name)
:explain-format explain-format})))
(catch [:type ::s/invalid] e
(file-util/prn-err e))
(catch #(contains? #{::no-migration-by-number
::duplicated-migration-numbers} (:type %)) e
(-> e
(errors/custom-error->error-report)
(file-util/prn-err)))))
(defn- already-migrated
"Get names of previously migrated migrations from db."
[db migrations-table]
(try
(->> {:select [:name]
:from [migrations-table]
:order-by [:created-at]}
(db-util/exec! db)
(map :name))
(catch Exception e
(let [msg (ex-message e)
table-exists-err-pattern #"relation .+ does not exist"]
(if (re-find table-exists-err-pattern msg)
(throw+ {:type ::no-migrations-table
:message "Migrations table does not exist."})
(throw+ {:type ::unexpected-db-error
:data (or (ex-message e) (str e))
:message "Unexpected db error."}))))))
(defmulti exec-action! :migration-type)
(defn- action->honeysql
[action]
(su/conform ::sql/->sql action))
(defmethod exec-action! AUTO-MIGRATION-EXT
[{:keys [db action]}]
(let [formatted-action (action->honeysql action)]
(if (sequential? formatted-action)
(doseq [sub-action formatted-action]
(db-util/exec! db sub-action))
(db-util/exec! db formatted-action))))
(defmethod exec-action! SQL-MIGRATION-EXT
[{:keys [db action]}]
(db-util/exec-raw! db action))
(defn- exec-actions!
"Perform list of actions on a database."
[{:keys [db actions migration-type]}]
(doseq [action actions]
(exec-action! {:db db
:action action
:migration-type migration-type})))
(defn- current-migration-number
"Return current migration name."
[migrated]
(if (seq migrated)
(let [res (->> (last migrated)
(get-migration-number))]
res)
0))
(defn- get-detailed-migrations-to-migrate
"Return migrations to migrate and migration direction."
[all-migrations migrated target-number]
(if-not (seq all-migrations)
{}
(let [all-numbers (set (map :number-int all-migrations))
last-number (apply max all-numbers)
target-number* (or target-number last-number)
current-number (current-migration-number migrated)
direction (if (> target-number* current-number)
FORWARD-DIRECTION
BACKWARD-DIRECTION)]
(when-not (or (contains? all-numbers target-number*)
(= 0 target-number*))
(throw+ {:type ::invalid-target-migration-number
:number target-number*
:message "Invalid target migration number."}))
(if (= target-number* current-number)
[]
(condp contains? direction
#{FORWARD-DIRECTION} {:to-migrate (->> all-migrations
(drop-while #(>= current-number (:number-int %)))
(take-while #(>= target-number* (:number-int %))))
:direction direction}
#{BACKWARD-DIRECTION} {:to-migrate (->> all-migrations
(drop-while #(>= target-number* (:number-int %)))
(take-while #(>= current-number (:number-int %)))
(sort-by :number-int >))
:direction direction})))))
(defn migrate
"Run migration on a db."
[{:keys [migrations-dir jdbc-url number migrations-table]
:or {migrations-table db-util/MIGRATIONS-TABLE}}]
(try+
(let [db (db-util/db-conn jdbc-url)
_ (db-util/create-migrations-table db migrations-table)
migrated (already-migrated db migrations-table)
all-migrations (migrations-list migrations-dir)
all-migrations-detailed (map detailed-migration all-migrations)
{:keys [to-migrate direction]}
(get-detailed-migrations-to-migrate all-migrations-detailed migrated number)]
(if (seq to-migrate)
(doseq [{:keys [migration-name file-name migration-type number-int]} to-migrate]
(condp = direction
FORWARD-DIRECTION (println (str "Applying " migration-name "..."))
BACKWARD-DIRECTION (println (str "Reverting " migration-name "...")))
(jdbc/with-transaction [tx db]
(let [actions (migration->actions {:file-name file-name
:migrations-dir migrations-dir
:migration-type migration-type
:number-int number-int
:direction direction
:all-migrations all-migrations-detailed})]
(exec-actions! {:db tx
:actions actions
:migration-type migration-type}))
(if (= direction FORWARD-DIRECTION)
(save-migration! db migration-name migrations-table)
(delete-migration! db migration-name migrations-table))))
(println "Nothing to migrate.")))
(catch [:type ::s/invalid] e
(file-util/prn-err e))
(catch #(contains? #{::duplicated-migration-numbers
::invalid-target-migration-number} (:type %)) e
(-> e
(errors/custom-error->error-report)
(file-util/prn-err)))))
(defn- get-already-migrated-migrations
[db migrations-table]
(try+
(set (already-migrated db migrations-table))
(catch [:type ::no-migrations-table]
; There is no migrated migrations if table doesn't exist.
[])))
(defn list-migrations
"Print migration list with status."
[{:keys [migrations-dir jdbc-url migrations-table]
:or {migrations-table db-util/MIGRATIONS-TABLE}}]
; TODO: reduce duplication with `migrate` fn!
(try+
(let [migration-names (migrations-list migrations-dir)
db (db-util/db-conn jdbc-url)
migrated (set (get-already-migrated-migrations db migrations-table))]
(if (seq migration-names)
(do
(println "Existing migrations:\n")
(doseq [file-name migration-names
:let [migration-name (get-migration-name file-name)
sign (if (contains? migrated migration-name)
LIST-SIGN-COMPLETED
" ")]]
(file-util/safe-println [(format "[%s] %s" sign file-name)])))
(println "Migrations not found.")))
(catch [:type ::s/invalid] e
(file-util/prn-err e))
(catch #(contains? #{::duplicated-migration-numbers