@@ -3,8 +3,6 @@ defmodule AshPostgres.MigrationGenerator do
3
3
4
4
require Logger
5
5
6
- import Mix.Generator
7
-
8
6
alias AshPostgres.MigrationGenerator . { Operation , Phase }
9
7
10
8
defstruct snapshot_path: nil ,
@@ -52,12 +50,46 @@ defmodule AshPostgres.MigrationGenerator do
52
50
|> Enum . concat ( find_repos ( ) )
53
51
|> Enum . uniq ( )
54
52
55
- Mix . shell ( ) . info ( "\n Extension Migrations: " )
56
- create_extension_migrations ( repos , opts )
57
- Mix . shell ( ) . info ( "\n Generating Tenant Migrations: " )
58
- create_migrations ( tenant_snapshots , opts , true , snapshots )
59
- Mix . shell ( ) . info ( "\n Generating Migrations:" )
60
- create_migrations ( snapshots , opts , false )
53
+ extension_migration_files =
54
+ create_extension_migrations ( repos , opts )
55
+
56
+ tenant_migration_files =
57
+ create_migrations ( tenant_snapshots , opts , true , snapshots )
58
+
59
+ migration_files =
60
+ create_migrations ( snapshots , opts , false )
61
+
62
+ case extension_migration_files ++ tenant_migration_files ++ migration_files do
63
+ [ ] ->
64
+ if ! opts . check || opts . dry_run do
65
+ Mix . shell ( ) . info (
66
+ "No changes detected, so no migrations or snapshots have been created."
67
+ )
68
+ end
69
+
70
+ :ok
71
+
72
+ files ->
73
+ cond do
74
+ opts . check ->
75
+ raise Ash.Error.Framework.PendingCodegen ,
76
+ diff: files
77
+
78
+ opts . dry_run ->
79
+ Mix . shell ( ) . info (
80
+ files
81
+ |> Enum . sort_by ( & elem ( & 1 , 0 ) )
82
+ |> Enum . map_join ( "\n \n " , fn { file , contents } ->
83
+ "#{ file } \n #{ contents } "
84
+ end )
85
+ )
86
+
87
+ true ->
88
+ Enum . each ( files , fn { file , contents } ->
89
+ Mix.Generator . create_file ( file , contents , force?: true )
90
+ end )
91
+ end
92
+ end
61
93
end
62
94
63
95
defp find_repos do
@@ -225,8 +257,7 @@ defmodule AshPostgres.MigrationGenerator do
225
257
|> Enum . map ( fn { _name , extension } -> extension end )
226
258
227
259
if Enum . empty? ( to_install ) do
228
- Mix . shell ( ) . info ( "No extensions to install" )
229
- :ok
260
+ [ ]
230
261
else
231
262
dev = if opts . dev , do: "_dev"
232
263
@@ -378,40 +409,13 @@ defmodule AshPostgres.MigrationGenerator do
378
409
379
410
contents = format ( migration_file , contents , opts )
380
411
381
- if opts . dry_run do
382
- Mix . shell ( ) . info ( snapshot_contents )
383
- Mix . shell ( ) . info ( contents )
384
-
385
- if opts . check do
386
- Mix . shell ( ) . error ( """
387
- Migrations would have been generated, but the --check flag was provided.
388
-
389
- To see what migration would have been generated, run with the `--dry-run`
390
- option instead. To generate those migrations, run without either flag.
391
- """ )
392
-
393
- exit ( { :shutdown , 1 } )
394
- end
395
- else
396
- if opts . check do
397
- Mix . shell ( ) . error ( """
398
- Migrations would have been generated, but the --check flag was provided.
399
-
400
- To see what migration would have been generated, run with the `--dry-run`
401
- option instead. To generate those migrations, run without either flag.
402
- """ )
403
-
404
- exit ( { :shutdown , 1 } )
405
- end
406
-
407
- create_file ( snapshot_file , snapshot_contents , force: true )
408
-
409
- if ! opts . snapshots_only do
410
- create_file ( migration_file , contents )
411
- end
412
- end
412
+ [
413
+ { snapshot_file , snapshot_contents } ,
414
+ { migration_file , contents }
415
+ ]
413
416
end
414
417
end
418
+ |> List . flatten ( )
415
419
end
416
420
417
421
defp set_ash_functions ( snapshot , installed_extensions ) do
@@ -429,7 +433,7 @@ defmodule AshPostgres.MigrationGenerator do
429
433
defp create_migrations ( snapshots , opts , tenant? , non_tenant_snapshots \\ [ ] ) do
430
434
snapshots
431
435
|> Enum . group_by ( & & 1 . repo )
432
- |> Enum . each ( fn { repo , snapshots } ->
436
+ |> Enum . flat_map ( fn { repo , snapshots } ->
433
437
deduped = deduplicate_snapshots ( snapshots , opts , non_tenant_snapshots )
434
438
435
439
snapshots_with_operations =
@@ -444,18 +448,7 @@ defmodule AshPostgres.MigrationGenerator do
444
448
|> Enum . uniq ( )
445
449
|> case do
446
450
[ ] ->
447
- tenant_str =
448
- if tenant? do
449
- "tenant "
450
- else
451
- ""
452
- end
453
-
454
- Mix . shell ( ) . info (
455
- "No #{ tenant_str } changes detected, so no migrations or snapshots have been created."
456
- )
457
-
458
- :ok
451
+ [ ]
459
452
460
453
operations ->
461
454
dev_migrations = get_dev_migrations ( opts , tenant? , repo )
@@ -475,21 +468,12 @@ defmodule AshPostgres.MigrationGenerator do
475
468
end
476
469
end
477
470
478
- if opts . check do
479
- Mix . shell ( ) . error ( """
480
- Migrations would have been generated, but the --check flag was provided.
481
-
482
- To see what migration would have been generated, run with the `--dry-run`
483
- option instead. To generate those migrations, run without either flag.
484
- """ )
485
-
486
- exit ( { :shutdown , 1 } )
487
- end
488
-
489
- if ! opts . snapshots_only do
471
+ if opts . snapshots_only do
472
+ [ ]
473
+ else
490
474
operations
491
475
|> split_into_migrations ( )
492
- |> Enum . each ( fn operations ->
476
+ |> Enum . map ( fn operations ->
493
477
run_without_transaction? =
494
478
Enum . any? ( operations , fn
495
479
% Operation.AddCustomIndex { index: % { concurrently: true } } ->
@@ -505,11 +489,10 @@ defmodule AshPostgres.MigrationGenerator do
505
489
operations
506
490
|> organize_operations
507
491
|> build_up_and_down ( )
508
- |> write_migration! ( repo , opts , tenant? , run_without_transaction? )
492
+ |> migration ( repo , opts , tenant? , run_without_transaction? )
509
493
end )
510
494
end
511
-
512
- create_new_snapshot ( snapshots , repo_name ( repo ) , opts , tenant? )
495
+ |> Enum . concat ( create_new_snapshot ( snapshots , repo_name ( repo ) , opts , tenant? ) )
513
496
end
514
497
end )
515
498
end
@@ -1110,7 +1093,7 @@ defmodule AshPostgres.MigrationGenerator do
1110
1093
repo |> Module . split ( ) |> List . last ( ) |> Macro . underscore ( )
1111
1094
end
1112
1095
1113
- defp write_migration! ( { up , down } , repo , opts , tenant? , run_without_transaction? ) do
1096
+ defp migration ( { up , down } , repo , opts , tenant? , run_without_transaction? ) do
1114
1097
migration_path = migration_path ( opts , repo , tenant? )
1115
1098
1116
1099
require_name! ( opts )
@@ -1185,13 +1168,7 @@ defmodule AshPostgres.MigrationGenerator do
1185
1168
"""
1186
1169
1187
1170
try do
1188
- contents = format ( migration_file , contents , opts )
1189
-
1190
- if opts . dry_run do
1191
- Mix . shell ( ) . info ( contents )
1192
- else
1193
- create_file ( migration_file , contents )
1194
- end
1171
+ { migration_file , format ( migration_file , contents , opts ) }
1195
1172
rescue
1196
1173
exception ->
1197
1174
reraise (
@@ -1223,45 +1200,44 @@ defmodule AshPostgres.MigrationGenerator do
1223
1200
end
1224
1201
1225
1202
defp create_new_snapshot ( snapshots , repo_name , opts , tenant? ) do
1226
- if ! opts . dry_run do
1227
- Enum . each ( snapshots , fn snapshot ->
1228
- snapshot_binary = snapshot_to_binary ( snapshot )
1203
+ Enum . map ( snapshots , fn snapshot ->
1204
+ snapshot_binary = snapshot_to_binary ( snapshot )
1229
1205
1230
- snapshot_folder =
1231
- if tenant? do
1232
- opts
1233
- |> snapshot_path ( snapshot . repo )
1234
- |> Path . join ( repo_name )
1235
- |> Path . join ( "tenants" )
1236
- else
1237
- opts
1238
- |> snapshot_path ( snapshot . repo )
1239
- |> Path . join ( repo_name )
1240
- end
1206
+ snapshot_folder =
1207
+ if tenant? do
1208
+ opts
1209
+ |> snapshot_path ( snapshot . repo )
1210
+ |> Path . join ( repo_name )
1211
+ |> Path . join ( "tenants" )
1212
+ else
1213
+ opts
1214
+ |> snapshot_path ( snapshot . repo )
1215
+ |> Path . join ( repo_name )
1216
+ end
1241
1217
1242
- dev = if opts . dev , do: "_dev"
1218
+ dev = if opts . dev , do: "_dev"
1243
1219
1244
- snapshot_file =
1245
- if snapshot . schema do
1246
- Path . join (
1247
- snapshot_folder ,
1248
- "#{ snapshot . schema } .#{ snapshot . table } /#{ timestamp ( ) } #{ dev } .json"
1249
- )
1250
- else
1251
- Path . join ( snapshot_folder , "#{ snapshot . table } /#{ timestamp ( ) } #{ dev } .json" )
1252
- end
1220
+ snapshot_file =
1221
+ if snapshot . schema do
1222
+ Path . join (
1223
+ snapshot_folder ,
1224
+ "#{ snapshot . schema } .#{ snapshot . table } /#{ timestamp ( ) } #{ dev } .json"
1225
+ )
1226
+ else
1227
+ Path . join ( snapshot_folder , "#{ snapshot . table } /#{ timestamp ( ) } #{ dev } .json" )
1228
+ end
1253
1229
1254
- File . mkdir_p ( Path . dirname ( snapshot_file ) )
1255
- create_file ( snapshot_file , snapshot_binary , force: true )
1230
+ File . mkdir_p ( Path . dirname ( snapshot_file ) )
1256
1231
1257
- old_snapshot_folder = Path . join ( snapshot_folder , "#{ snapshot . table } #{ dev } .json" )
1232
+ old_snapshot_folder = Path . join ( snapshot_folder , "#{ snapshot . table } #{ dev } .json" )
1258
1233
1259
- if File . exists? ( old_snapshot_folder ) do
1260
- new_snapshot_folder = Path . join ( snapshot_folder , "#{ snapshot . table } /initial#{ dev } .json" )
1261
- File . rename ( old_snapshot_folder , new_snapshot_folder )
1262
- end
1263
- end )
1264
- end
1234
+ if File . exists? ( old_snapshot_folder ) do
1235
+ new_snapshot_folder = Path . join ( snapshot_folder , "#{ snapshot . table } /initial#{ dev } .json" )
1236
+ File . rename ( old_snapshot_folder , new_snapshot_folder )
1237
+ end
1238
+
1239
+ { snapshot_file , snapshot_binary }
1240
+ end )
1265
1241
end
1266
1242
1267
1243
@ doc false
@@ -2961,13 +2937,13 @@ defmodule AshPostgres.MigrationGenerator do
2961
2937
2962
2938
defp renaming? ( table , removing , opts ) do
2963
2939
if opts . dev do
2940
+ false
2941
+ else
2964
2942
if opts . no_shell? do
2965
2943
raise "Unimplemented: cannot determine: Are you renaming #{ table } .#{ removing . source } ? without shell input"
2966
2944
else
2967
2945
yes? ( opts , "Are you renaming #{ table } .#{ removing . source } ?" )
2968
2946
end
2969
- else
2970
- false
2971
2947
end
2972
2948
end
2973
2949
0 commit comments