Skip to content

Commit 755c556

Browse files
committed
improvement: use new PendingCodegen error
1 parent 3f837d1 commit 755c556

File tree

6 files changed

+118
-140
lines changed

6 files changed

+118
-140
lines changed

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
erlang 27.0.1
2-
elixir 1.18.0-otp-27
2+
elixir 1.18.4-otp-27

lib/data_layer.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ defmodule AshPostgres.DataLayer do
568568
end
569569

570570
def codegen(args) do
571+
Mix.Task.reenable("ash_postgres.generate_migrations")
571572
Mix.Task.run("ash_postgres.generate_migrations", args)
572573
end
573574

lib/migration_generator/migration_generator.ex

Lines changed: 90 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ defmodule AshPostgres.MigrationGenerator do
33

44
require Logger
55

6-
import Mix.Generator
7-
86
alias AshPostgres.MigrationGenerator.{Operation, Phase}
97

108
defstruct snapshot_path: nil,
@@ -52,12 +50,46 @@ defmodule AshPostgres.MigrationGenerator do
5250
|> Enum.concat(find_repos())
5351
|> Enum.uniq()
5452

55-
Mix.shell().info("\nExtension Migrations: ")
56-
create_extension_migrations(repos, opts)
57-
Mix.shell().info("\nGenerating Tenant Migrations: ")
58-
create_migrations(tenant_snapshots, opts, true, snapshots)
59-
Mix.shell().info("\nGenerating 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
6193
end
6294

6395
defp find_repos do
@@ -225,8 +257,7 @@ defmodule AshPostgres.MigrationGenerator do
225257
|> Enum.map(fn {_name, extension} -> extension end)
226258

227259
if Enum.empty?(to_install) do
228-
Mix.shell().info("No extensions to install")
229-
:ok
260+
[]
230261
else
231262
dev = if opts.dev, do: "_dev"
232263

@@ -378,40 +409,13 @@ defmodule AshPostgres.MigrationGenerator do
378409

379410
contents = format(migration_file, contents, opts)
380411

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+
]
413416
end
414417
end
418+
|> List.flatten()
415419
end
416420

417421
defp set_ash_functions(snapshot, installed_extensions) do
@@ -429,7 +433,7 @@ defmodule AshPostgres.MigrationGenerator do
429433
defp create_migrations(snapshots, opts, tenant?, non_tenant_snapshots \\ []) do
430434
snapshots
431435
|> Enum.group_by(& &1.repo)
432-
|> Enum.each(fn {repo, snapshots} ->
436+
|> Enum.flat_map(fn {repo, snapshots} ->
433437
deduped = deduplicate_snapshots(snapshots, opts, non_tenant_snapshots)
434438

435439
snapshots_with_operations =
@@ -444,18 +448,7 @@ defmodule AshPostgres.MigrationGenerator do
444448
|> Enum.uniq()
445449
|> case do
446450
[] ->
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+
[]
459452

460453
operations ->
461454
dev_migrations = get_dev_migrations(opts, tenant?, repo)
@@ -475,21 +468,12 @@ defmodule AshPostgres.MigrationGenerator do
475468
end
476469
end
477470

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
490474
operations
491475
|> split_into_migrations()
492-
|> Enum.each(fn operations ->
476+
|> Enum.map(fn operations ->
493477
run_without_transaction? =
494478
Enum.any?(operations, fn
495479
%Operation.AddCustomIndex{index: %{concurrently: true}} ->
@@ -505,11 +489,10 @@ defmodule AshPostgres.MigrationGenerator do
505489
operations
506490
|> organize_operations
507491
|> build_up_and_down()
508-
|> write_migration!(repo, opts, tenant?, run_without_transaction?)
492+
|> migration(repo, opts, tenant?, run_without_transaction?)
509493
end)
510494
end
511-
512-
create_new_snapshot(snapshots, repo_name(repo), opts, tenant?)
495+
|> Enum.concat(create_new_snapshot(snapshots, repo_name(repo), opts, tenant?))
513496
end
514497
end)
515498
end
@@ -1110,7 +1093,7 @@ defmodule AshPostgres.MigrationGenerator do
11101093
repo |> Module.split() |> List.last() |> Macro.underscore()
11111094
end
11121095

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
11141097
migration_path = migration_path(opts, repo, tenant?)
11151098

11161099
require_name!(opts)
@@ -1185,13 +1168,7 @@ defmodule AshPostgres.MigrationGenerator do
11851168
"""
11861169

11871170
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)}
11951172
rescue
11961173
exception ->
11971174
reraise(
@@ -1223,45 +1200,44 @@ defmodule AshPostgres.MigrationGenerator do
12231200
end
12241201

12251202
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)
12291205

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
12411217

1242-
dev = if opts.dev, do: "_dev"
1218+
dev = if opts.dev, do: "_dev"
12431219

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
12531229

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))
12561231

1257-
old_snapshot_folder = Path.join(snapshot_folder, "#{snapshot.table}#{dev}.json")
1232+
old_snapshot_folder = Path.join(snapshot_folder, "#{snapshot.table}#{dev}.json")
12581233

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)
12651241
end
12661242

12671243
@doc false
@@ -2961,13 +2937,13 @@ defmodule AshPostgres.MigrationGenerator do
29612937

29622938
defp renaming?(table, removing, opts) do
29632939
if opts.dev do
2940+
false
2941+
else
29642942
if opts.no_shell? do
29652943
raise "Unimplemented: cannot determine: Are you renaming #{table}.#{removing.source}? without shell input"
29662944
else
29672945
yes?(opts, "Are you renaming #{table}.#{removing.source}?")
29682946
end
2969-
else
2970-
false
29712947
end
29722948
end
29732949

mix.exs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ defmodule AshPostgres.MixProject do
166166
# Run "mix help deps" to learn about dependencies.
167167
defp deps do
168168
[
169-
{:ash, ash_version("~> 3.4 and >= 3.4.69")},
169+
# {:ash, ash_version("~> 3.4 and >= 3.4.69")},
170+
{:ash, github: "ash-project/ash", override: true},
170171
{:ash_sql, ash_sql_version("~> 0.2 and >= 0.2.72")},
171172
{:igniter, "~> 0.6", optional: true},
172173
{:ecto_sql, "~> 3.12"},
@@ -189,24 +190,24 @@ defmodule AshPostgres.MixProject do
189190
]
190191
end
191192

192-
defp ash_version(default_version) do
193-
case System.get_env("ASH_VERSION") do
194-
nil ->
195-
default_version
193+
# defp ash_version(default_version) do
194+
# case System.get_env("ASH_VERSION") do
195+
# nil ->
196+
# default_version
196197

197-
"local" ->
198-
[path: "../ash", override: true]
198+
# "local" ->
199+
# [path: "../ash", override: true]
199200

200-
"main" ->
201-
[git: "https://github.com/ash-project/ash.git", override: true]
201+
# "main" ->
202+
# [git: "https://github.com/ash-project/ash.git", override: true]
202203

203-
version when is_binary(version) ->
204-
"~> #{version}"
204+
# version when is_binary(version) ->
205+
# "~> #{version}"
205206

206-
version ->
207-
version
208-
end
209-
end
207+
# version ->
208+
# version
209+
# end
210+
# end
210211

211212
defp ash_sql_version(default_version) do
212213
case System.get_env("ASH_SQL_VERSION") do

mix.lock

Lines changed: 1 addition & 1 deletion
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", "e7b1a738644e1092ed350115ebebf5da8ac2aec1", []},
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
"benchee": {:hex, :benchee, "1.4.0", "9f1f96a30ac80bab94faad644b39a9031d5632e517416a8ab0a6b0ac4df124ce", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "299cd10dd8ce51c9ea3ddb74bb150f93d25e968f93e4c1fa31698a8e4fa5d715"},
55
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},

test/migration_generator_test.exs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,15 +1420,15 @@ defmodule AshPostgres.MigrationGeneratorTest do
14201420
[domain: Domain]
14211421
end
14221422

1423-
test "returns code(1) if snapshots and resources don't fit", %{domain: domain} do
1424-
assert catch_exit(
1425-
AshPostgres.MigrationGenerator.generate(domain,
1426-
snapshot_path: "test_snapshots_path",
1427-
migration_path: "test_migration_path",
1428-
check: true,
1429-
auto_name: true
1430-
)
1431-
) == {:shutdown, 1}
1423+
test "raises an error on pending codegen", %{domain: domain} do
1424+
assert_raise Ash.Error.Framework.PendingCodegen, fn ->
1425+
AshPostgres.MigrationGenerator.generate(domain,
1426+
snapshot_path: "test_snapshots_path",
1427+
migration_path: "test_migration_path",
1428+
check: true,
1429+
auto_name: true
1430+
)
1431+
end
14321432

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

0 commit comments

Comments
 (0)