improvement: add migration_defaults for customizing default values

This commit is contained in:
Zach Daniel 2022-11-21 02:38:00 -05:00
parent e0d7f0d0c0
commit 26668df73d
3 changed files with 39 additions and 14 deletions

View file

@ -293,6 +293,15 @@ defmodule AshPostgres.DataLayer do
doc:
"A keyword list of attribute names to the ecto migration type that should be used for that attribute. Only necessary if you need to override the defaults."
],
migration_defaults: [
type: :keyword_list,
default: [],
doc: """
A keyword list of attribute names to the ecto migration default that should be used for that attribute. Only necessary if you need to override the defaults.
The string you use will be placed verbatim in the migration. Use fragments like `fragment(\\\\"now()\\\\")`, or for `nil`, use `\\\\"nil\\\\"`.
"""
],
base_filter_sql: [
type: :string,
doc:

View file

@ -25,7 +25,12 @@ defmodule AshPostgres.DataLayer.Info do
@doc "A keyword list of customized migration types"
def migration_types(resource) do
Extension.get_opt(resource, [:postgres], :migration_types, nil, true)
Extension.get_opt(resource, [:postgres], :migration_types, [])
end
@doc "A keyword list of customized migration defaults"
def migration_defaults(resource) do
Extension.get_opt(resource, [:postgres], :migration_defaults, [])
end
@doc "The configured check_constraints for a resource"

View file

@ -1909,6 +1909,7 @@ defmodule AshPostgres.MigrationGenerator do
destination_attribute_default:
default(
source_attribute,
relationship.destination,
AshPostgres.DataLayer.Info.repo(relationship.destination)
),
destination_attribute_generated: source_attribute.generated?,
@ -2040,7 +2041,7 @@ defmodule AshPostgres.MigrationGenerator do
&Map.take(&1, [:name, :source, :type, :default, :allow_nil?, :generated?, :primary_key?])
)
|> Enum.map(fn attribute ->
default = default(attribute, repo)
default = default(attribute, resource, repo)
type =
AshPostgres.DataLayer.Info.migration_types(resource)[attribute.name] ||
@ -2210,7 +2211,8 @@ defmodule AshPostgres.MigrationGenerator do
@uuid_functions [&Ash.UUID.generate/0, &Ecto.UUID.generate/0]
defp default(%{default: default}, _repo) when is_function(default) do
defp default(%{name: name, default: default}, resource, _repo) when is_function(default) do
configured_default(resource, name) ||
cond do
default in @uuid_functions ->
~S[fragment("gen_random_uuid()")]
@ -2223,9 +2225,18 @@ defmodule AshPostgres.MigrationGenerator do
end
end
defp default(%{default: {_, _, _}}, _), do: "nil"
defp default(%{default: nil}, _), do: "nil"
defp default(%{default: value}, _), do: EctoMigrationDefault.to_default(value)
defp default(%{name: name, default: {_, _, _}}, resource, _),
do: configured_default(resource, name) || "nil"
defp default(%{name: name, default: nil}, resource, _),
do: configured_default(resource, name) || "nil"
defp default(%{name: name, default: value}, resource, _),
do: configured_default(resource, name) || EctoMigrationDefault.to_default(value)
defp configured_default(resource, attribute) do
AshPostgres.DataLayer.Info.migration_defaults(resource)[attribute]
end
defp snapshot_to_binary(snapshot) do
snapshot