@@ -19,6 +19,7 @@ defmodule AshPostgres.MigrationGenerator do
19
19
format: true ,
20
20
dry_run: false ,
21
21
check: false ,
22
+ dev: false ,
22
23
snapshots_only: false ,
23
24
dont_drop_columns: false
24
25
@@ -452,6 +453,20 @@ defmodule AshPostgres.MigrationGenerator do
452
453
:ok
453
454
454
455
operations ->
456
+ if ! opts . dev and any_dev_migrations? ( opts , repo ) do
457
+ if opts . check do
458
+ Mix . shell ( ) . error ( """
459
+ Generated migrations are from dev mode.
460
+
461
+ Generate migrations without `--dev` flag.
462
+ """ )
463
+
464
+ exit ( { :shutdown , 1 } )
465
+ else
466
+ remove_dev_migrations_and_snapshots ( opts , tenant? , repo , snapshots )
467
+ end
468
+ end
469
+
455
470
if opts . check do
456
471
Mix . shell ( ) . error ( """
457
472
Migrations would have been generated, but the --check flag was provided.
@@ -491,6 +506,50 @@ defmodule AshPostgres.MigrationGenerator do
491
506
end )
492
507
end
493
508
509
+ defp any_dev_migrations? ( opts , repo ) do
510
+ opts
511
+ |> migration_path ( repo )
512
+ |> File . ls! ( )
513
+ |> Enum . any? ( & String . contains? ( & 1 , "_dev.exs" ) )
514
+ end
515
+
516
+ defp remove_dev_migrations_and_snapshots ( opts , tenant? , repo , snapshots ) do
517
+ opts
518
+ |> migration_path ( repo )
519
+ |> File . ls! ( )
520
+ |> Enum . filter ( & String . contains? ( & 1 , "_dev.exs" ) )
521
+ |> Enum . each ( fn migration_name ->
522
+ opts
523
+ |> migration_path ( repo )
524
+ |> Path . join ( migration_name )
525
+ |> File . rm! ( )
526
+ end )
527
+
528
+ Enum . each ( snapshots , fn snapshot ->
529
+ snapshot_folder =
530
+ if tenant? do
531
+ opts
532
+ |> snapshot_path ( snapshot . repo )
533
+ |> Path . join ( repo_name ( repo ) )
534
+ |> Path . join ( "tenants" )
535
+ |> Path . join ( snapshot . table )
536
+ else
537
+ opts
538
+ |> snapshot_path ( snapshot . repo )
539
+ |> Path . join ( repo_name ( snapshot . repo ) )
540
+ |> Path . join ( snapshot . table )
541
+ end
542
+
543
+ File . ls! ( snapshot_folder )
544
+ |> Enum . filter ( & String . contains? ( & 1 , "_dev.json" ) )
545
+ |> Enum . each ( fn snapshot_name ->
546
+ snapshot_folder
547
+ |> Path . join ( snapshot_name )
548
+ |> File . rm! ( )
549
+ end )
550
+ end )
551
+ end
552
+
494
553
defp split_into_migrations ( operations ) do
495
554
operations
496
555
|> Enum . split_with ( fn
@@ -960,7 +1019,7 @@ defmodule AshPostgres.MigrationGenerator do
960
1019
961
1020
migration_file =
962
1021
migration_path
963
- |> Path . join ( migration_name <> ".exs" )
1022
+ |> Path . join ( migration_name <> "#{ if opts . dev , do: "_dev" } .exs" )
964
1023
965
1024
module_name =
966
1025
if tenant? do
@@ -1054,20 +1113,25 @@ defmodule AshPostgres.MigrationGenerator do
1054
1113
|> Path . join ( repo_name )
1055
1114
end
1056
1115
1116
+ dev = if opts . dev , do: "_dev"
1117
+
1057
1118
snapshot_file =
1058
1119
if snapshot . schema do
1059
- Path . join ( snapshot_folder , "#{ snapshot . schema } .#{ snapshot . table } /#{ timestamp ( ) } .json" )
1120
+ Path . join (
1121
+ snapshot_folder ,
1122
+ "#{ snapshot . schema } .#{ snapshot . table } /#{ timestamp ( ) } #{ dev } .json"
1123
+ )
1060
1124
else
1061
- Path . join ( snapshot_folder , "#{ snapshot . table } /#{ timestamp ( ) } .json" )
1125
+ Path . join ( snapshot_folder , "#{ snapshot . table } /#{ timestamp ( ) } #{ dev } .json" )
1062
1126
end
1063
1127
1064
1128
File . mkdir_p ( Path . dirname ( snapshot_file ) )
1065
1129
create_file ( snapshot_file , snapshot_binary , force: true )
1066
1130
1067
- old_snapshot_folder = Path . join ( snapshot_folder , "#{ snapshot . table } .json" )
1131
+ old_snapshot_folder = Path . join ( snapshot_folder , "#{ snapshot . table } #{ dev } .json" )
1068
1132
1069
1133
if File . exists? ( old_snapshot_folder ) do
1070
- new_snapshot_folder = Path . join ( snapshot_folder , "#{ snapshot . table } /initial.json" )
1134
+ new_snapshot_folder = Path . join ( snapshot_folder , "#{ snapshot . table } /initial#{ dev } .json" )
1071
1135
File . rename ( old_snapshot_folder , new_snapshot_folder )
1072
1136
end
1073
1137
end )
@@ -2640,7 +2704,10 @@ defmodule AshPostgres.MigrationGenerator do
2640
2704
if File . exists? ( snapshot_folder ) do
2641
2705
snapshot_folder
2642
2706
|> File . ls! ( )
2643
- |> Enum . filter ( & String . match? ( & 1 , ~r/ ^\d {14}\. json$/ ) )
2707
+ |> Enum . filter (
2708
+ & ( String . match? ( & 1 , ~r/ ^\d {14}\. json$/ ) or
2709
+ ( opts . dev and String . match? ( & 1 , ~r/ ^\d {14}\_ dev.json$/ ) ) )
2710
+ )
2644
2711
|> case do
2645
2712
[ ] ->
2646
2713
get_old_snapshot ( folder , snapshot )
0 commit comments