mirror of
https://github.com/ash-project/ash_postgres.git
synced 2024-09-19 21:13:19 +12:00
improvement: add migration_defaults
for customizing default values
This commit is contained in:
parent
e0d7f0d0c0
commit
26668df73d
3 changed files with 39 additions and 14 deletions
|
@ -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:
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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,22 +2211,32 @@ defmodule AshPostgres.MigrationGenerator do
|
|||
|
||||
@uuid_functions [&Ash.UUID.generate/0, &Ecto.UUID.generate/0]
|
||||
|
||||
defp default(%{default: default}, _repo) when is_function(default) do
|
||||
cond do
|
||||
default in @uuid_functions ->
|
||||
~S[fragment("gen_random_uuid()")]
|
||||
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()")]
|
||||
|
||||
default == (&DateTime.utc_now/0) ->
|
||||
~S[fragment("now()")]
|
||||
default == (&DateTime.utc_now/0) ->
|
||||
~S[fragment("now()")]
|
||||
|
||||
true ->
|
||||
"nil"
|
||||
end
|
||||
true ->
|
||||
"nil"
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue