mirror of
https://github.com/ash-project/ash_postgres.git
synced 2024-09-20 05:23:18 +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:
|
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."
|
"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: [
|
base_filter_sql: [
|
||||||
type: :string,
|
type: :string,
|
||||||
doc:
|
doc:
|
||||||
|
|
|
@ -25,7 +25,12 @@ defmodule AshPostgres.DataLayer.Info do
|
||||||
|
|
||||||
@doc "A keyword list of customized migration types"
|
@doc "A keyword list of customized migration types"
|
||||||
def migration_types(resource) do
|
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
|
end
|
||||||
|
|
||||||
@doc "The configured check_constraints for a resource"
|
@doc "The configured check_constraints for a resource"
|
||||||
|
|
|
@ -1909,6 +1909,7 @@ defmodule AshPostgres.MigrationGenerator do
|
||||||
destination_attribute_default:
|
destination_attribute_default:
|
||||||
default(
|
default(
|
||||||
source_attribute,
|
source_attribute,
|
||||||
|
relationship.destination,
|
||||||
AshPostgres.DataLayer.Info.repo(relationship.destination)
|
AshPostgres.DataLayer.Info.repo(relationship.destination)
|
||||||
),
|
),
|
||||||
destination_attribute_generated: source_attribute.generated?,
|
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?])
|
&Map.take(&1, [:name, :source, :type, :default, :allow_nil?, :generated?, :primary_key?])
|
||||||
)
|
)
|
||||||
|> Enum.map(fn attribute ->
|
|> Enum.map(fn attribute ->
|
||||||
default = default(attribute, repo)
|
default = default(attribute, resource, repo)
|
||||||
|
|
||||||
type =
|
type =
|
||||||
AshPostgres.DataLayer.Info.migration_types(resource)[attribute.name] ||
|
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]
|
@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
|
||||||
cond do
|
configured_default(resource, name) ||
|
||||||
default in @uuid_functions ->
|
cond do
|
||||||
~S[fragment("gen_random_uuid()")]
|
default in @uuid_functions ->
|
||||||
|
~S[fragment("gen_random_uuid()")]
|
||||||
|
|
||||||
default == (&DateTime.utc_now/0) ->
|
default == (&DateTime.utc_now/0) ->
|
||||||
~S[fragment("now()")]
|
~S[fragment("now()")]
|
||||||
|
|
||||||
true ->
|
true ->
|
||||||
"nil"
|
"nil"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp default(%{default: {_, _, _}}, _), do: "nil"
|
defp default(%{name: name, default: {_, _, _}}, resource, _),
|
||||||
defp default(%{default: nil}, _), do: "nil"
|
do: configured_default(resource, name) || "nil"
|
||||||
defp default(%{default: value}, _), do: EctoMigrationDefault.to_default(value)
|
|
||||||
|
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
|
defp snapshot_to_binary(snapshot) do
|
||||||
snapshot
|
snapshot
|
||||||
|
|
Loading…
Reference in a new issue