Skip to content

Commit 627d7c5

Browse files
committed
improvement: support new PendingCodegen error
1 parent d63453c commit 627d7c5

File tree

6 files changed

+98
-143
lines changed

6 files changed

+98
-143
lines changed

.tool-versions

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
erlang 26.0.2
2-
elixir 1.18.4
1+
erlang 27.0.1
2+
elixir 1.18.4-otp-27

lib/data_layer.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ defmodule AshSqlite.DataLayer do
391391
end
392392

393393
def codegen(args) do
394-
# TODO: take args that we care about
394+
Mix.Task.reenable("ash_sqlite.generate_migrations")
395395
Mix.Task.run("ash_sqlite.generate_migrations", args)
396396
end
397397

lib/migration_generator/migration_generator.ex

Lines changed: 82 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -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("\nExtension Migrations: ")
44-
create_extension_migrations(repos, opts)
45-
Mix.shell().info("\nGenerating 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

mix.exs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ defmodule AshSqlite.MixProject do
130130
{:ecto_sqlite3, "~> 0.12"},
131131
{:ecto, "~> 3.9"},
132132
{:jason, "~> 1.0"},
133-
{:ash, ash_version("~> 3.4 and >= 3.4.58")},
133+
# {:ash, ash_version("~> 3.4 and >= 3.4.58")},
134+
{:ash, github: "ash-project/ash", override: true},
134135
{:ash_sql, ash_sql_version("~> 0.2 and >= 0.2.20")},
135136
{:igniter, "~> 0.5 and >= 0.5.16", optional: true},
136137
{:simple_sat, ">= 0.0.0", only: [:dev, :test]},
@@ -144,25 +145,6 @@ defmodule AshSqlite.MixProject do
144145
]
145146
end
146147

147-
defp ash_version(default_version) do
148-
case System.get_env("ASH_VERSION") do
149-
nil ->
150-
default_version
151-
152-
"local" ->
153-
[path: "../ash", override: true]
154-
155-
"main" ->
156-
[git: "https://github.com/ash-project/ash.git"]
157-
158-
version when is_binary(version) ->
159-
"~> #{version}"
160-
161-
version ->
162-
version
163-
end
164-
end
165-
166148
defp ash_sql_version(default_version) do
167149
case System.get_env("ASH_SQL_VERSION") do
168150
nil ->

mix.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
%{
2-
"ash": {:hex, :ash, "3.5.12", "435a6916d47e4ed6eabce886de2443d17af9dd9ca9765a29c73d9e02507b86b3", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:igniter, "~> 0.6", [hex: :igniter, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:owl, "~> 0.11", [hex: :owl, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: true]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:reactor, "~> 0.11", [hex: :reactor, repo: "hexpm", optional: false]}, {:simple_sat, ">= 0.1.1 and < 1.0.0-0", [hex: :simple_sat, repo: "hexpm", optional: true]}, {:spark, ">= 2.2.60 and < 3.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, ">= 0.2.6 and < 1.0.0-0", [hex: :splode, repo: "hexpm", optional: false]}, {:stream_data, "~> 1.0", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "503492989c56e33c300d731b3717d1df9eeebba2b9018e5dd9f330db727edb57"},
2+
"ash": {:git, "https://github.com/ash-project/ash.git", "e497a90e44f1aa43dde39c7b0f2ef5cb2826da61", []},
33
"ash_sql": {:hex, :ash_sql, "0.2.76", "4afac3284194f3d7820b7edc1263ac1eb232a25734406ad7b2284683b9384205", [:mix], [{:ash, "~> 3.5", [hex: :ash, repo: "hexpm", optional: false]}, {:ecto, "~> 3.9", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "~> 3.9", [hex: :ecto_sql, repo: "hexpm", optional: false]}], "hexpm", "f6bc02d8c4cba3f8f9a532e6ff73eaf8a4f7685257646a58fe7a320cfaf10c39"},
44
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
55
"cc_precompiler": {:hex, :cc_precompiler, "0.1.10", "47c9c08d8869cf09b41da36538f62bc1abd3e19e41701c2cea2675b53c704258", [:mix], [{:elixir_make, "~> 0.7", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "f6e046254e53cd6b41c6bacd70ae728011aa82b2742a80d6e2214855c6e06b22"},
@@ -43,7 +43,7 @@
4343
"simple_sat": {:hex, :simple_sat, "0.1.3", "f650fc3c184a5fe741868b5ac56dc77fdbb428468f6dbf1978e14d0334497578", [:mix], [], "hexpm", "a54305066a356b7194dc81db2a89232bacdc0b3edaef68ed9aba28dcbc34887b"},
4444
"sobelow": {:hex, :sobelow, "0.14.0", "dd82aae8f72503f924fe9dd97ffe4ca694d2f17ec463dcfd365987c9752af6ee", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "7ecf91e298acfd9b24f5d761f19e8f6e6ac585b9387fb6301023f1f2cd5eed5f"},
4545
"sourceror": {:hex, :sourceror, "1.10.0", "38397dedbbc286966ec48c7af13e228b171332be1ad731974438c77791945ce9", [:mix], [], "hexpm", "29dbdfc92e04569c9d8e6efdc422fc1d815f4bd0055dc7c51b8800fb75c4b3f1"},
46-
"spark": {:hex, :spark, "2.2.61", "64745581832caf136cbd654c1ecef98d291f3d1b347a14411b7d0dc726e3822b", [:mix], [{:igniter, ">= 0.3.64 and < 1.0.0-0", [hex: :igniter, repo: "hexpm", optional: true]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: true]}, {:sourceror, "~> 1.2", [hex: :sourceror, repo: "hexpm", optional: true]}], "hexpm", "cb10300e17f74b41848954d61630fb627f4e0d5acf9aacd7d9f312d5b119d50f"},
46+
"spark": {:hex, :spark, "2.2.62", "610502559834465edce437de712bf7e6d59713ad48050789dbef69a798e71a3c", [:mix], [{:igniter, ">= 0.3.64 and < 1.0.0-0", [hex: :igniter, repo: "hexpm", optional: true]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: true]}, {:sourceror, "~> 1.2", [hex: :sourceror, repo: "hexpm", optional: true]}], "hexpm", "726df72e1b9c17401584b4657e75e08a27a1cf6a6effa2486bf1c074da6176a7"},
4747
"spitfire": {:hex, :spitfire, "0.2.0", "0de1f519a23f65bde40d316adad53c07a9563f25cc68915d639d8a509a0aad8a", [:mix], [], "hexpm", "743daaee2d81a0d8095431729f478ce49b47ea8943c7d770de86704975cb7775"},
4848
"splode": {:hex, :splode, "0.2.9", "3a2776e187c82f42f5226b33b1220ccbff74f4bcc523dd4039c804caaa3ffdc7", [:mix], [], "hexpm", "8002b00c6e24f8bd1bcced3fbaa5c33346048047bb7e13d2f3ad428babbd95c3"},
4949
"stream_data": {:hex, :stream_data, "1.2.0", "58dd3f9e88afe27dc38bef26fce0c84a9e7a96772b2925c7b32cd2435697a52b", [:mix], [], "hexpm", "eb5c546ee3466920314643edf68943a5b14b32d1da9fe01698dc92b73f89a9ed"},

test/migration_generator_test.exs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -443,15 +443,15 @@ defmodule AshSqlite.MigrationGeneratorTest do
443443
[domain: Domain]
444444
end
445445

446-
test "returns code(1) if snapshots and resources don't fit", %{domain: domain} do
447-
assert catch_exit(
448-
AshSqlite.MigrationGenerator.generate(domain,
449-
snapshot_path: "test_snapshot_path",
450-
migration_path: "test_migration_path",
451-
check: true,
452-
auto_name: true
453-
)
454-
) == {:shutdown, 1}
446+
test "raises an error on pending codegen", %{domain: domain} do
447+
assert_raise Ash.Error.Framework.PendingCodegen, fn ->
448+
AshSqlite.MigrationGenerator.generate(domain,
449+
snapshot_path: "test_snapshot_path",
450+
migration_path: "test_migration_path",
451+
check: true,
452+
auto_name: true
453+
)
454+
end
455455

456456
refute File.exists?(Path.wildcard("test_migration_path2/**/*_migrate_resources*.exs"))
457457
refute File.exists?(Path.wildcard("test_snapshots_path2/test_repo/posts/*.json"))

0 commit comments

Comments
 (0)