fix: stringify struct defaults in migration generator

This commit is contained in:
Zach Daniel 2021-04-26 13:21:57 -04:00
parent 04190e18e2
commit 8bcc2a16eb
4 changed files with 142 additions and 5 deletions

View file

@ -1630,9 +1630,18 @@ defmodule AshPostgres.MigrationGenerator do
defp default(%{default: value, type: type}, _) do
case Ash.Type.dump_to_native(type, value) do
{:ok, value} -> inspect(value)
_ -> "nil"
{:ok, value} when is_struct(value) ->
to_string(value)
{:ok, value} ->
inspect(value)
_ ->
"nil"
end
rescue
_ ->
"nil"
end
defp snapshot_to_binary(snapshot) do

View file

@ -0,0 +1,106 @@
{
"attributes": [
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"name": "category",
"primary_key?": false,
"references": null,
"type": "citext"
},
{
"allow_nil?": true,
"default": "0",
"generated?": false,
"name": "decimal",
"primary_key?": false,
"references": null,
"type": "decimal"
},
{
"allow_nil?": false,
"default": "fragment(\"uuid_generate_v4()\")",
"generated?": false,
"name": "id",
"primary_key?": true,
"references": null,
"type": "uuid"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"name": "price",
"primary_key?": false,
"references": null,
"type": "bigint"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"name": "public",
"primary_key?": false,
"references": null,
"type": "boolean"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"name": "score",
"primary_key?": false,
"references": null,
"type": "bigint"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"name": "status",
"primary_key?": false,
"references": null,
"type": "status"
},
{
"allow_nil?": true,
"default": "nil",
"generated?": false,
"name": "title",
"primary_key?": false,
"references": null,
"type": "text"
},
{
"allow_nil?": true,
"default": "\"sponsored\"",
"generated?": false,
"name": "type",
"primary_key?": false,
"references": null,
"type": "text"
}
],
"base_filter": "type = 'sponsored'",
"check_constraints": [
{
"attribute": [
"price"
],
"base_filter": "type = 'sponsored'",
"check": "price > 0",
"name": "price_must_be_positive"
}
],
"has_create_action": true,
"hash": "42B75BE079D8BBC3AE0EC4B1517A4D0E150F381E9817F9D8FA53E1E689A80D3B",
"identities": [],
"multitenancy": {
"attribute": null,
"global": null,
"strategy": null
},
"repo": "Elixir.AshPostgres.TestRepo",
"table": "posts"
}

View file

@ -0,0 +1,21 @@
defmodule AshPostgres.TestRepo.Migrations.MigrateResources9 do
@moduledoc """
Updates resources based on their most recent snapshots.
This file was autogenerated with `mix ash_postgres.generate_migrations`
"""
use Ecto.Migration
def up do
alter table(:posts) do
add :decimal, :decimal, default: 0
end
end
def down do
alter table(:posts) do
remove :decimal
end
end
end

View file

@ -4,9 +4,9 @@ defmodule AshPostgres.Test.Post do
data_layer: AshPostgres.DataLayer
postgres do
table "posts"
repo AshPostgres.TestRepo
base_filter_sql "type = 'sponsored'"
table("posts")
repo(AshPostgres.TestRepo)
base_filter_sql("type = 'sponsored'")
check_constraints do
check_constraint(:price, "price_must_be_positive",
@ -44,6 +44,7 @@ defmodule AshPostgres.Test.Post do
attribute(:category, :ci_string)
attribute(:type, :atom, default: :sponsored, private?: true, writable?: false)
attribute(:price, :integer)
attribute(:decimal, :decimal, default: Decimal.new(0))
attribute(:status, AshPostgres.Test.Types.Status)
end