2022-04-19 16:06:02 +12:00
|
|
|
defprotocol EctoMigrationDefault do
|
2022-08-24 11:56:46 +12:00
|
|
|
@moduledoc """
|
|
|
|
Allows configuring how values are translated to default values in migrations.
|
|
|
|
|
|
|
|
Still a work in progress, but covers most standard values aside from maps.
|
|
|
|
"""
|
2022-05-25 04:11:32 +12:00
|
|
|
@fallback_to_any true
|
2022-08-24 11:56:46 +12:00
|
|
|
@doc "Returns the text (elixir code) that will be placed into a migration as the default value"
|
2022-04-19 16:06:02 +12:00
|
|
|
def to_default(value)
|
|
|
|
end
|
|
|
|
|
2022-05-25 04:11:32 +12:00
|
|
|
defimpl EctoMigrationDefault, for: Any do
|
2022-08-24 11:56:46 +12:00
|
|
|
require Logger
|
|
|
|
|
2022-05-25 04:11:32 +12:00
|
|
|
def to_default(value) do
|
2023-06-23 09:37:57 +12:00
|
|
|
Logger.warning("""
|
2022-05-25 04:11:32 +12:00
|
|
|
You have specified a default value for a type that cannot be explicitly
|
|
|
|
converted to an Ecto default:
|
|
|
|
|
|
|
|
`#{inspect(value)}`
|
|
|
|
|
|
|
|
The default value in the migration will be set to `nil` and you can edit
|
|
|
|
your migration accordingly.
|
|
|
|
|
|
|
|
To prevent this warning, implement the `EctoMigrationDefault` protocol
|
2022-11-21 20:39:57 +13:00
|
|
|
for the appropriate Elixir type in your Ash project, or configure its
|
|
|
|
default value in `migration_defaults` in the postgres section. Use `\\\"nil\\\"`
|
|
|
|
for no default.
|
2022-05-25 04:11:32 +12:00
|
|
|
""")
|
|
|
|
|
|
|
|
"nil"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-19 16:06:02 +12:00
|
|
|
defimpl EctoMigrationDefault, for: Integer do
|
|
|
|
def to_default(value) do
|
|
|
|
to_string(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-28 09:10:51 +12:00
|
|
|
defimpl EctoMigrationDefault, for: Float do
|
|
|
|
def to_default(value) do
|
|
|
|
to_string(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-29 02:48:44 +12:00
|
|
|
defimpl EctoMigrationDefault, for: Decimal do
|
|
|
|
def to_default(value) do
|
|
|
|
~s["#{value}"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-19 16:06:02 +12:00
|
|
|
defimpl EctoMigrationDefault, for: BitString do
|
|
|
|
def to_default(value) do
|
|
|
|
inspect(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defimpl EctoMigrationDefault, for: DateTime do
|
|
|
|
def to_default(value) do
|
|
|
|
~s[fragment("'#{to_string(value)}'")]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defimpl EctoMigrationDefault, for: NaiveDateTime do
|
|
|
|
def to_default(value) do
|
|
|
|
~s[fragment("'#{to_string(value)}'")]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defimpl EctoMigrationDefault, for: Date do
|
|
|
|
def to_default(value) do
|
|
|
|
~s[fragment("'#{to_string(value)}'")]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defimpl EctoMigrationDefault, for: Time do
|
|
|
|
def to_default(value) do
|
|
|
|
~s[fragment("'#{to_string(value)}'")]
|
|
|
|
end
|
|
|
|
end
|
2022-04-22 02:36:34 +12:00
|
|
|
|
|
|
|
defimpl EctoMigrationDefault, for: Atom do
|
|
|
|
def to_default(value) when value in [nil, true, false], do: inspect(value)
|
|
|
|
|
|
|
|
def to_default(value) do
|
|
|
|
inspect(to_string(value))
|
|
|
|
end
|
|
|
|
end
|