fix: always drop constraints before modifying

fix: properly compare old references and new references
This commit is contained in:
Zach Daniel 2021-04-13 12:19:50 -04:00
parent 0a803626d9
commit 250f8b195c
2 changed files with 24 additions and 15 deletions

View file

@ -1122,8 +1122,8 @@ defmodule AshPostgres.MigrationGenerator do
else
[
%Operation.AlterAttribute{
new_attribute: new_attribute,
old_attribute: old_attribute,
new_attribute: Map.delete(new_attribute, :references),
old_attribute: Map.delete(old_attribute, :references),
table: snapshot.table
}
]
@ -1414,14 +1414,17 @@ defmodule AshPostgres.MigrationGenerator do
end
defp configured_reference(resource, attribute, relationship) do
resource
|> AshPostgres.references()
|> Enum.find(&(&1.relationship == relationship))
|> Kernel.||(%{
on_delete: nil,
on_update: nil,
name: "#{AshPostgres.table(resource)}_#{attribute}_fkey"
})
ref =
resource
|> AshPostgres.references()
|> Enum.find(&(&1.relationship == relationship))
|> Kernel.||(%{
on_delete: nil,
on_update: nil,
name: nil
})
Map.put(ref, :name, ref.name || "#{AshPostgres.table(resource)}_#{attribute}_fkey")
end
defp migration_type({:array, type}), do: {:array, migration_type(type)}
@ -1560,7 +1563,9 @@ defmodule AshPostgres.MigrationGenerator do
|> Map.update!(:destination_field, &String.to_atom/1)
|> Map.put_new(:on_delete, nil)
|> Map.put_new(:on_update, nil)
|> Map.put_new(:name, "#{table}_#{attribute.name}")
|> Map.update!(:on_delete, &(&1 && String.to_atom(&1)))
|> Map.update!(:on_update, &(&1 && String.to_atom(&1)))
|> Map.put(:name, Map.get(references, :name) || "#{table}_#{attribute.name}")
|> Map.put_new(:multitenancy, %{
attribute: nil,
strategy: nil,

View file

@ -357,17 +357,21 @@ defmodule AshPostgres.MigrationGenerator.Operation do
# We only need to drop it before altering an attribute with `references/3`
defstruct [:attribute, :table, :multitenancy, :direction, no_phase: true]
def up(%{table: table, references: reference, direction: :up}) do
def up(%{table: table, attribute: %{references: reference}, direction: :up}) do
"drop constraint(:#{table}, #{inspect(reference.name)})"
end
def up(_), do: ""
def up(_) do
""
end
def down(%{table: table, references: reference, direction: :down}) do
def down(%{table: table, attribute: %{references: reference}, direction: :down}) do
"drop constraint(:#{table}, #{inspect(reference.name)})"
end
def down(_), do: ""
def down(_) do
""
end
end
defmodule RenameAttribute do