@@ -3,8 +3,6 @@ defmodule AshSqlite.MigrationGenerator do
33
44 require Logger
55
6- import Mix.Generator
7-
86 alias AshSqlite.MigrationGenerator . { Operation , Phase }
97
108 defstruct snapshot_path: nil ,
@@ -40,10 +38,45 @@ defmodule AshSqlite.MigrationGenerator do
4038 |> Enum . map ( & & 1 . repo )
4139 |> Enum . uniq ( )
4240
43- Mix . shell ( ) . info ( "\n Extension Migrations: " )
44- create_extension_migrations ( repos , opts )
45- Mix . shell ( ) . info ( "\n Generating Migrations:" )
46- create_migrations ( snapshots , opts )
41+ extension_migration_files =
42+ create_extension_migrations ( repos , opts )
43+
44+ migration_files =
45+ create_migrations ( snapshots , opts )
46+
47+ files = extension_migration_files ++ migration_files
48+
49+ case files do
50+ [ ] ->
51+ if ! opts . check || opts . dry_run do
52+ Mix . shell ( ) . info (
53+ "No changes detected, so no migrations or snapshots have been created."
54+ )
55+ end
56+
57+ :ok
58+
59+ files ->
60+ cond do
61+ opts . check ->
62+ raise Ash.Error.Framework.PendingCodegen ,
63+ diff: files
64+
65+ opts . dry_run ->
66+ Mix . shell ( ) . info (
67+ files
68+ |> Enum . sort_by ( & elem ( & 1 , 0 ) )
69+ |> Enum . map_join ( "\n \n " , fn { file , contents } ->
70+ "#{ file } \n #{ contents } "
71+ end )
72+ )
73+
74+ true ->
75+ Enum . each ( files , fn { file , contents } ->
76+ Mix.Generator . create_file ( file , contents , force?: true )
77+ end )
78+ end
79+ end
4780 end
4881
4982 @ doc """
@@ -195,8 +228,7 @@ defmodule AshSqlite.MigrationGenerator do
195228 |> Enum . map ( fn { _name , extension } -> extension end )
196229
197230 if Enum . empty? ( to_install ) do
198- Mix . shell ( ) . info ( "No extensions to install" )
199- :ok
231+ [ ]
200232 else
201233 { module , migration_name } =
202234 case to_install do
@@ -276,43 +308,19 @@ defmodule AshSqlite.MigrationGenerator do
276308
277309 contents = format ( contents , opts )
278310
279- if opts . dry_run do
280- Mix . shell ( ) . info ( snapshot_contents )
281- Mix . shell ( ) . info ( contents )
282-
283- if opts . check do
284- Mix . shell ( ) . error ( """
285- Migrations would have been generated, but the --check flag was provided.
286-
287- To see what migration would have been generated, run with the `--dry-run`
288- option instead. To generate those migrations, run without either flag.
289- """ )
290-
291- exit ( { :shutdown , 1 } )
292- end
293- else
294- if opts . check do
295- Mix . shell ( ) . error ( """
296- Migrations would have been generated, but the --check flag was provided.
297-
298- To see what migration would have been generated, run with the `--dry-run`
299- option instead. To generate those migrations, run without either flag.
300- """ )
301-
302- exit ( { :shutdown , 1 } )
303- end
304-
305- create_file ( snapshot_file , snapshot_contents , force: true )
306- create_file ( migration_file , contents )
307- end
311+ [
312+ { snapshot_file , snapshot_contents } ,
313+ { migration_file , contents }
314+ ]
308315 end
309316 end
317+ |> List . flatten ( )
310318 end
311319
312320 defp create_migrations ( snapshots , opts ) do
313321 snapshots
314322 |> Enum . group_by ( & & 1 . repo )
315- |> Enum . each ( fn { repo , snapshots } ->
323+ |> Enum . flat_map ( fn { repo , snapshots } ->
316324 deduped = deduplicate_snapshots ( snapshots , opts )
317325
318326 snapshots_with_operations =
@@ -327,11 +335,7 @@ defmodule AshSqlite.MigrationGenerator do
327335 |> Enum . uniq ( )
328336 |> case do
329337 [ ] ->
330- Mix . shell ( ) . info (
331- "No changes detected, so no migrations or snapshots have been created."
332- )
333-
334- :ok
338+ [ ]
335339
336340 operations ->
337341 dev_migrations = get_dev_migrations ( opts , repo )
@@ -350,23 +354,15 @@ defmodule AshSqlite.MigrationGenerator do
350354 end
351355 end
352356
353- if opts . check do
354- IO . puts ( """
355- Migrations would have been generated, but the --check flag was provided.
357+ migration_files =
358+ operations
359+ |> organize_operations
360+ |> build_up_and_down ( )
361+ |> migration ( repo , opts )
356362
357- To see what migration would have been generated, run with the `--dry-run`
358- option instead. To generate those migrations, run without either flag.
359- """ )
363+ snapshot_files = create_new_snapshot ( snapshots , repo_name ( repo ) , opts )
360364
361- exit ( { :shutdown , 1 } )
362- end
363-
364- operations
365- |> organize_operations
366- |> build_up_and_down ( )
367- |> write_migration! ( repo , opts )
368-
369- create_new_snapshot ( snapshots , repo_name ( repo ) , opts )
365+ [ migration_files ] ++ snapshot_files
370366 end
371367 end )
372368 end
@@ -844,7 +840,7 @@ defmodule AshSqlite.MigrationGenerator do
844840 repo |> Module . split ( ) |> List . last ( ) |> Macro . underscore ( )
845841 end
846842
847- defp write_migration! ( { up , down } , repo , opts ) do
843+ defp migration ( { up , down } , repo , opts ) do
848844 migration_path = migration_path ( opts , repo )
849845
850846 require_name! ( opts )
@@ -905,35 +901,7 @@ defmodule AshSqlite.MigrationGenerator do
905901 """
906902
907903 try do
908- contents = format ( contents , opts )
909-
910- if opts . dry_run do
911- Mix . shell ( ) . info ( contents )
912-
913- if opts . check do
914- Mix . shell ( ) . error ( """
915- Migrations would have been generated, but the --check flag was provided.
916-
917- To see what migration would have been generated, run with the `--dry-run`
918- option instead. To generate those migrations, run without either flag.
919- """ )
920-
921- exit ( { :shutdown , 1 } )
922- end
923- else
924- if opts . check do
925- Mix . shell ( ) . error ( """
926- Migrations would have been generated, but the --check flag was provided.
927-
928- To see what migration would have been generated, run with the `--dry-run`
929- option instead. To generate those migrations, run without either flag.
930- """ )
931-
932- exit ( { :shutdown , 1 } )
933- end
934-
935- create_file ( migration_file , contents )
936- end
904+ { migration_file , format ( contents , opts ) }
937905 rescue
938906 exception ->
939907 reraise (
@@ -979,29 +947,30 @@ defmodule AshSqlite.MigrationGenerator do
979947 end
980948
981949 defp create_new_snapshot ( snapshots , repo_name , opts ) do
982- unless opts . dry_run do
983- Enum . each ( snapshots , fn snapshot ->
984- snapshot_binary = snapshot_to_binary ( snapshot )
950+ Enum . map ( snapshots , fn snapshot ->
951+ snapshot_binary = snapshot_to_binary ( snapshot )
985952
986- snapshot_folder =
987- opts
988- |> snapshot_path ( snapshot . repo )
989- |> Path . join ( repo_name )
953+ snapshot_folder =
954+ opts
955+ |> snapshot_path ( snapshot . repo )
956+ |> Path . join ( repo_name )
990957
991- dev = if opts . dev , do: "_dev"
992- snapshot_file = Path . join ( snapshot_folder , "#{ snapshot . table } /#{ timestamp ( ) } #{ dev } .json" )
958+ dev = if opts . dev , do: "_dev"
993959
994- File . mkdir_p ( Path . dirname ( snapshot_file ) )
995- File . write! ( snapshot_file , snapshot_binary , [ ] )
960+ snapshot_file =
961+ Path . join ( snapshot_folder , " #{ snapshot . table } / #{ timestamp ( ) } #{ dev } .json" )
996962
997- old_snapshot_folder = Path . join ( snapshot_folder , "#{ snapshot . table } #{ dev } .json" )
963+ old_snapshot_folder = Path . join ( snapshot_folder , "#{ snapshot . table } #{ dev } .json" )
998964
999- if File . exists? ( old_snapshot_folder ) do
1000- new_snapshot_folder = Path . join ( snapshot_folder , "#{ snapshot . table } /initial#{ dev } .json" )
1001- File . rename ( old_snapshot_folder , new_snapshot_folder )
1002- end
1003- end )
1004- end
965+ if File . exists? ( old_snapshot_folder ) do
966+ new_snapshot_folder = Path . join ( snapshot_folder , "#{ snapshot . table } /initial#{ dev } .json" )
967+ File . rename ( old_snapshot_folder , new_snapshot_folder )
968+ end
969+
970+ File . mkdir_p ( Path . dirname ( snapshot_file ) )
971+
972+ { snapshot_file , snapshot_binary }
973+ end )
1005974 end
1006975
1007976 @ doc false
@@ -2108,10 +2077,14 @@ defmodule AshSqlite.MigrationGenerator do
21082077 end
21092078
21102079 defp renaming? ( table , removing , opts ) do
2111- if opts . no_shell? do
2112- raise "Unimplemented: cannot determine: Are you renaming #{ table } . #{ removing . source } ? without shell input"
2080+ if opts . dev do
2081+ false
21132082 else
2114- Mix . shell ( ) . yes? ( "Are you renaming #{ table } .#{ removing . source } ?" )
2083+ if opts . no_shell? do
2084+ raise "Unimplemented: cannot determine: Are you renaming #{ table } .#{ removing . source } ? without shell input"
2085+ else
2086+ Mix . shell ( ) . yes? ( "Are you renaming #{ table } .#{ removing . source } ?" )
2087+ end
21152088 end
21162089 end
21172090
0 commit comments