Skip to content

Commit 8d99e9a

Browse files
committed
fix: simply storage of size/scale/precision information
1 parent 17c66b9 commit 8d99e9a

File tree

1 file changed

+31
-78
lines changed

1 file changed

+31
-78
lines changed

lib/migration_generator/migration_generator.ex

Lines changed: 31 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3199,25 +3199,7 @@ defmodule AshPostgres.MigrationGenerator do
31993199
end
32003200

32013201
{type, size, precision, scale} =
3202-
case type do
3203-
{:varchar, size} ->
3204-
{:varchar, size, nil, nil}
3205-
3206-
{:binary, size} ->
3207-
{:binary, size, nil, nil}
3208-
3209-
{:decimal, precision, scale} ->
3210-
{:decimal, nil, precision, scale}
3211-
3212-
{:decimal, precision} ->
3213-
{:decimal, nil, precision, nil}
3214-
3215-
{other, size} when is_atom(other) and is_integer(size) ->
3216-
{other, size, nil, nil}
3217-
3218-
other ->
3219-
{other, nil, nil, nil}
3220-
end
3202+
migration_type_to_type(type)
32213203

32223204
attribute
32233205
|> Map.put(:default, default)
@@ -3247,6 +3229,32 @@ defmodule AshPostgres.MigrationGenerator do
32473229
end)
32483230
end
32493231

3232+
defp migration_type_to_type(type) do
3233+
case type do
3234+
{:varchar, size} ->
3235+
{:varchar, size, nil, nil}
3236+
3237+
{:binary, size} ->
3238+
{:binary, size, nil, nil}
3239+
3240+
{:decimal, precision, scale} ->
3241+
{:decimal, nil, precision, scale}
3242+
3243+
{:decimal, precision} ->
3244+
{:decimal, nil, precision, nil}
3245+
3246+
{other, size} when is_atom(other) and is_integer(size) ->
3247+
{other, size, nil, nil}
3248+
3249+
{:array, other} ->
3250+
{nested, size, precision, scale} = migration_type_to_type(other)
3251+
{{:array, nested}, size, precision, scale}
3252+
3253+
other ->
3254+
{other, nil, nil, nil}
3255+
end
3256+
end
3257+
32503258
defp find_reference(resource, table, attribute) do
32513259
resource
32523260
|> Ash.Resource.Info.relationships()
@@ -3581,22 +3589,6 @@ defmodule AshPostgres.MigrationGenerator do
35813589
["array", sanitize_type(type, size, scale, precision)]
35823590
end
35833591

3584-
defp sanitize_type(:varchar, size, _scale, _precision) when not is_nil(size) do
3585-
["varchar", size]
3586-
end
3587-
3588-
defp sanitize_type(:binary, size, _scale, _precision) when not is_nil(size) do
3589-
["binary", size]
3590-
end
3591-
3592-
defp sanitize_type(:decimal, _size, scale, precision) do
3593-
["decimal", precision, scale]
3594-
end
3595-
3596-
defp sanitize_type(type, size, precision, decimal) when is_tuple(type) do
3597-
sanitize_type(elem(type, 0), size, precision, decimal)
3598-
end
3599-
36003592
defp sanitize_type(type, _, _, _) do
36013593
type
36023594
end
@@ -3686,27 +3678,6 @@ defmodule AshPostgres.MigrationGenerator do
36863678
defp load_attribute(attribute, table) do
36873679
type = load_type(attribute.type)
36883680

3689-
{type, size, scale, precision} =
3690-
case type do
3691-
{:varchar, size} ->
3692-
{:varchar, size, nil, nil}
3693-
3694-
{:binary, size} ->
3695-
{:binary, size, nil, nil}
3696-
3697-
{other, size} when is_atom(other) and is_integer(size) ->
3698-
{other, size, nil, nil}
3699-
3700-
{:decimal, precision} ->
3701-
{:decimal, nil, nil, precision}
3702-
3703-
{:decimal, precision, scale} ->
3704-
{:decimal, nil, precision, scale}
3705-
3706-
other ->
3707-
{other, nil, nil, nil}
3708-
end
3709-
37103681
attribute =
37113682
if Map.has_key?(attribute, :name) do
37123683
Map.put(attribute, :source, maybe_to_atom(attribute.name))
@@ -3716,9 +3687,9 @@ defmodule AshPostgres.MigrationGenerator do
37163687

37173688
attribute
37183689
|> Map.put(:type, type)
3719-
|> Map.put(:size, size)
3720-
|> Map.put(:precision, precision)
3721-
|> Map.put(:scale, scale)
3690+
|> Map.put_new(:size, nil)
3691+
|> Map.put_new(:precision, nil)
3692+
|> Map.put_new(:scale, nil)
37223693
|> Map.put_new(:default, "nil")
37233694
|> Map.update!(:default, &(&1 || "nil"))
37243695
|> Map.update!(:references, fn
@@ -3797,25 +3768,7 @@ defmodule AshPostgres.MigrationGenerator do
37973768
{:array, load_type(type)}
37983769
end
37993770

3800-
defp load_type(["varchar", size]) do
3801-
{:varchar, size}
3802-
end
3803-
3804-
defp load_type(["binary", size]) do
3805-
{:binary, size}
3806-
end
3807-
3808-
defp load_type(["decimal", precision]) do
3809-
{:decimal, precision}
3810-
end
3811-
3812-
defp load_type(["decimal", precision, scale]) do
3813-
{:decimal, precision, scale}
3814-
end
3815-
3816-
defp load_type([string, size]) when is_binary(string) and is_integer(size) do
3817-
{String.to_existing_atom(string), size}
3818-
end
3771+
defp load_type([type | _]), do: String.to_existing_atom(type)
38193772

38203773
defp load_type(type) do
38213774
maybe_to_atom(type)

0 commit comments

Comments
 (0)