fix: don't group alters with creates (#22)

This commit is contained in:
Zach Daniel 2020-09-30 22:43:33 -04:00 committed by GitHub
parent ff6f0e37f1
commit 3848566ca8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -367,28 +367,35 @@ defmodule AshPostgres.MigrationGenerator do
name: name name: name
}, },
table: table table: table
} = add, } = add
%AshPostgres.MigrationGenerator.Operation.AlterAttribute{
new_attribute: %{
name: name,
references: references
},
old_attribute: %{
name: name
},
table: table
}
| rest | rest
], ],
acc acc
) ) do
when not is_nil(references) do rest
new_attribute = Map.put(add.attribute, :references, references) |> Enum.take_while(fn op ->
op.table == table
end)
|> Enum.with_index()
|> Enum.find(fn
{%Operation.AlterAttribute{
new_attribute: %{name: ^name, references: references},
old_attribute: %{name: ^name}
}, _}
when not is_nil(references) ->
true
streamline( _ ->
rest, false
[%{add | attribute: new_attribute} | acc] end)
) |> case do
nil ->
streamline(rest, [add | acc])
{alter, index} ->
new_attribute = Map.put(add.attribute, :references, alter.new_attribute.references)
streamline(List.delete_at(rest, index), [%{add | attribute: new_attribute} | acc])
end
end end
defp streamline([first | rest], acc) do defp streamline([first | rest], acc) do
@ -418,7 +425,7 @@ defmodule AshPostgres.MigrationGenerator do
defp group_into_phases( defp group_into_phases(
[%Operation.AlterAttribute{table: table} = op | rest], [%Operation.AlterAttribute{table: table} = op | rest],
%{table: table} = phase, %Phase.Alter{table: table} = phase,
acc acc
) do ) do
group_into_phases(rest, %{phase | operations: [op | phase.operations]}, acc) group_into_phases(rest, %{phase | operations: [op | phase.operations]}, acc)
@ -426,7 +433,7 @@ defmodule AshPostgres.MigrationGenerator do
defp group_into_phases( defp group_into_phases(
[%Operation.RenameAttribute{table: table} = op | rest], [%Operation.RenameAttribute{table: table} = op | rest],
%{table: table} = phase, %Phase.Alter{table: table} = phase,
acc acc
) do ) do
group_into_phases(rest, %{phase | operations: [op | phase.operations]}, acc) group_into_phases(rest, %{phase | operations: [op | phase.operations]}, acc)
@ -445,9 +452,8 @@ defmodule AshPostgres.MigrationGenerator do
end end
defp group_into_phases([operation | rest], nil, acc) do defp group_into_phases([operation | rest], nil, acc) do
group_into_phases(rest, nil, [ phase = %Phase.Alter{operations: [operation]}
%Phase.Alter{operations: [operation], table: operation.table} | acc group_into_phases(rest, phase, acc)
])
end end
defp group_into_phases(operations, phase, acc) do defp group_into_phases(operations, phase, acc) do