Skip to content

Commit

Permalink
Update introspection and migration options for ecto_sql 3.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mbklein committed Oct 2, 2024
1 parent 4e5db36 commit 1590ea1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 12 additions & 7 deletions app/lib/meadow/data/schemas/validations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,13 @@ defmodule Meadow.Data.Schemas.Validations do
"""
def prepare_embed(%Ecto.Changeset{data: data, params: params} = change, field)
when is_atom(field) do
empty_struct = fn ->
{:parameterized, _type, field_spec} = data.__struct__.__schema__(:type, field)
field_spec.related.__struct__ |> Map.from_struct()
end

with f <- to_string(field),
current <- Enum.find([field, f], &Map.get(data, &1)) do
value =
cond do
Map.has_key?(params, f) and is_nil(Map.get(params, f)) -> empty_struct.()
Map.has_key?(params, f) and is_nil(Map.get(params, f)) -> empty_struct(data, field)
Map.has_key?(params, f) -> Map.get(params, f)
is_nil(current) -> empty_struct.()
is_nil(current) -> empty_struct(data, field)
true -> nil
end

Expand All @@ -35,4 +30,14 @@ defmodule Meadow.Data.Schemas.Validations do
end
end
end

defp empty_struct(data, field) do
field_spec =
case data.__struct__.__schema__(:type, field) do
{:parameterized, _type, f} -> f
{:parameterized, {_type, f}} -> f
end

field_spec.related.__struct__ |> Map.from_struct()
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Meadow.Repo.Migrations.CreateFileSets do
add(:extracted_metadata, :map, default: %{})
add(:structural_metadata, :map, default: %{})
add(:derivatives, :map, default: %{})
add(:work_id, references(:works, null: false, on_delete: :delete_all))
add(:work_id, references(:works, on_delete: :delete_all))
add(:rank, :integer)
add(:poster_offset, :integer)

Expand Down

0 comments on commit 1590ea1

Please sign in to comment.