Skip to content

Commit 0119fa7

Browse files
committed
fix: ensure split migrations get unique names and modules
When concurrent indexes cause migrations to be split into multiple files, the second+ files now get a suffix to avoid duplicate names and modules.
1 parent 320b93f commit 0119fa7

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

lib/migration_generator/migration_generator.ex

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,8 @@ defmodule AshPostgres.MigrationGenerator do
515515
else
516516
operations
517517
|> split_into_migrations()
518-
|> Enum.map(fn operations ->
518+
|> Enum.with_index()
519+
|> Enum.map(fn {operations, split_index} ->
519520
run_without_transaction? =
520521
Enum.any?(operations, fn
521522
%Operation.AddCustomIndex{index: %{concurrently: true}} ->
@@ -531,7 +532,7 @@ defmodule AshPostgres.MigrationGenerator do
531532
operations
532533
|> organize_operations
533534
|> build_up_and_down()
534-
|> migration(repo, opts, tenant?, run_without_transaction?)
535+
|> migration(repo, opts, tenant?, run_without_transaction?, split_index)
535536
end)
536537
end
537538
|> Enum.concat(create_new_snapshot(snapshots, repo_name(repo), opts, tenant?))
@@ -1223,14 +1224,16 @@ defmodule AshPostgres.MigrationGenerator do
12231224
repo |> Module.split() |> List.last() |> Macro.underscore()
12241225
end
12251226

1226-
defp migration({up, down}, repo, opts, tenant?, run_without_transaction?) do
1227+
defp migration({up, down}, repo, opts, tenant?, run_without_transaction?, split_index \\ 0) do
12271228
migration_path = migration_path(opts, repo, tenant?)
12281229

12291230
require_name!(opts)
12301231

1232+
split_suffix = if split_index > 0, do: "_#{split_index}", else: ""
1233+
12311234
{migration_name, last_part} =
12321235
if opts.name do
1233-
{"#{timestamp()}_#{opts.name}", "#{opts.name}"}
1236+
{"#{timestamp()}_#{opts.name}#{split_suffix}", "#{opts.name}#{split_suffix}"}
12341237
else
12351238
count =
12361239
migration_path
@@ -1253,7 +1256,7 @@ defmodule AshPostgres.MigrationGenerator do
12531256
|> Enum.max(fn -> 0 end)
12541257
|> Kernel.+(1)
12551258

1256-
{"#{timestamp()}_migrate_resources#{count}", "migrate_resources#{count}"}
1259+
{"#{timestamp()}_migrate_resources#{count}#{split_suffix}", "migrate_resources#{count}#{split_suffix}"}
12571260
end
12581261

12591262
migration_file =

0 commit comments

Comments
 (0)