Skip to content

Commit c303e34

Browse files
committed
improvement: improve dry_run logic and fix priv path setup
1 parent e7890be commit c303e34

File tree

1 file changed

+62
-11
lines changed

1 file changed

+62
-11
lines changed

lib/migration_generator/migration_generator.ex

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,7 @@ defmodule AshSqlite.MigrationGenerator do
122122
end
123123

124124
defp opts(opts) do
125-
case struct(__MODULE__, opts) do
126-
%{check: true} = opts ->
127-
%{opts | dry_run: true}
128-
129-
opts ->
130-
opts
131-
end
125+
struct(__MODULE__, opts)
132126
end
133127

134128
defp snapshot_path(%{snapshot_path: snapshot_path}, _) when not is_nil(snapshot_path),
@@ -142,10 +136,17 @@ defmodule AshSqlite.MigrationGenerator do
142136
snapshot_path
143137
else
144138
priv =
145-
config[:priv] || "priv/#{repo |> Module.split() |> List.last() |> Macro.underscore()}"
139+
config[:priv] || "priv/"
146140

147141
app = Keyword.fetch!(config, :otp_app)
148-
Application.app_dir(app, Path.join(priv, "resource_snapshots"))
142+
143+
Application.app_dir(
144+
app,
145+
Path.join([
146+
priv,
147+
"resource_snapshots"
148+
])
149+
)
149150
end
150151
end
151152

@@ -272,8 +273,36 @@ defmodule AshSqlite.MigrationGenerator do
272273
)
273274

274275
contents = format(contents, opts)
275-
create_file(snapshot_file, snapshot_contents, force: true)
276-
create_file(migration_file, contents)
276+
277+
if opts.dry_run do
278+
Mix.shell().info(snapshot_contents)
279+
Mix.shell().info(contents)
280+
281+
if opts.check do
282+
Mix.shell().error("""
283+
Migrations would have been generated, but the --check flag was provided.
284+
285+
To see what migration would have been generated, run with the `--dry-run`
286+
option instead. To generate those migrations, run without either flag.
287+
""")
288+
289+
exit({:shutdown, 1})
290+
end
291+
else
292+
if opts.check do
293+
Mix.shell().error("""
294+
Migrations would have been generated, but the --check flag was provided.
295+
296+
To see what migration would have been generated, run with the `--dry-run`
297+
option instead. To generate those migrations, run without either flag.
298+
""")
299+
300+
exit({:shutdown, 1})
301+
end
302+
303+
create_file(snapshot_file, snapshot_contents, force: true)
304+
create_file(migration_file, contents)
305+
end
277306
end
278307
end
279308
end
@@ -743,7 +772,29 @@ defmodule AshSqlite.MigrationGenerator do
743772

744773
if opts.dry_run do
745774
Mix.shell().info(contents)
775+
776+
if opts.check do
777+
Mix.shell().error("""
778+
Migrations would have been generated, but the --check flag was provided.
779+
780+
To see what migration would have been generated, run with the `--dry-run`
781+
option instead. To generate those migrations, run without either flag.
782+
""")
783+
784+
exit({:shutdown, 1})
785+
end
746786
else
787+
if opts.check do
788+
Mix.shell().error("""
789+
Migrations would have been generated, but the --check flag was provided.
790+
791+
To see what migration would have been generated, run with the `--dry-run`
792+
option instead. To generate those migrations, run without either flag.
793+
""")
794+
795+
exit({:shutdown, 1})
796+
end
797+
747798
create_file(migration_file, contents)
748799
end
749800
rescue

0 commit comments

Comments
 (0)