Skip to content

Commit 6ee9afa

Browse files
committed
Handle dev flag in migration generator
1 parent 037b105 commit 6ee9afa

File tree

1 file changed

+73
-6
lines changed

1 file changed

+73
-6
lines changed

lib/migration_generator/migration_generator.ex

+73-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ defmodule AshPostgres.MigrationGenerator do
1919
format: true,
2020
dry_run: false,
2121
check: false,
22+
dev: false,
2223
snapshots_only: false,
2324
dont_drop_columns: false
2425

@@ -452,6 +453,20 @@ defmodule AshPostgres.MigrationGenerator do
452453
:ok
453454

454455
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+
455470
if opts.check do
456471
Mix.shell().error("""
457472
Migrations would have been generated, but the --check flag was provided.
@@ -491,6 +506,50 @@ defmodule AshPostgres.MigrationGenerator do
491506
end)
492507
end
493508

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+
494553
defp split_into_migrations(operations) do
495554
operations
496555
|> Enum.split_with(fn
@@ -960,7 +1019,7 @@ defmodule AshPostgres.MigrationGenerator do
9601019

9611020
migration_file =
9621021
migration_path
963-
|> Path.join(migration_name <> ".exs")
1022+
|> Path.join(migration_name <> "#{if opts.dev, do: "_dev"}.exs")
9641023

9651024
module_name =
9661025
if tenant? do
@@ -1054,20 +1113,25 @@ defmodule AshPostgres.MigrationGenerator do
10541113
|> Path.join(repo_name)
10551114
end
10561115

1116+
dev = if opts.dev, do: "_dev"
1117+
10571118
snapshot_file =
10581119
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+
)
10601124
else
1061-
Path.join(snapshot_folder, "#{snapshot.table}/#{timestamp()}.json")
1125+
Path.join(snapshot_folder, "#{snapshot.table}/#{timestamp()}#{dev}.json")
10621126
end
10631127

10641128
File.mkdir_p(Path.dirname(snapshot_file))
10651129
create_file(snapshot_file, snapshot_binary, force: true)
10661130

1067-
old_snapshot_folder = Path.join(snapshot_folder, "#{snapshot.table}.json")
1131+
old_snapshot_folder = Path.join(snapshot_folder, "#{snapshot.table}#{dev}.json")
10681132

10691133
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")
10711135
File.rename(old_snapshot_folder, new_snapshot_folder)
10721136
end
10731137
end)
@@ -2640,7 +2704,10 @@ defmodule AshPostgres.MigrationGenerator do
26402704
if File.exists?(snapshot_folder) do
26412705
snapshot_folder
26422706
|> 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+
)
26442711
|> case do
26452712
[] ->
26462713
get_old_snapshot(folder, snapshot)

0 commit comments

Comments
 (0)