improvement: fix datetime migration type discovery

This commit is contained in:
Zach Daniel 2024-01-12 11:18:12 -05:00
parent 55bdce140f
commit 75ffda1076
4 changed files with 385 additions and 10 deletions

View file

@ -2198,24 +2198,18 @@ defmodule AshSqlite.MigrationGenerator do
defp migration_type({:array, type}, constraints),
do: {:array, migration_type(type, constraints)}
defp migration_type(AshSqlite.Type.CiString, _), do: :string
defp migration_type(Ash.Type.CiString, _), do: :citext
defp migration_type(Ash.Type.UUID, _), do: :uuid
defp migration_type(Ash.Type.Integer, _), do: :bigint
defp migration_type(other, constraints) do
type = Ash.Type.get_type(other)
if Ash.Type.NewType.new_type?(type) do
migration_type(
Ash.Type.NewType.subtype_of(type),
Ash.Type.NewType.constraints(type, constraints)
)
else
migration_type_from_storage_type(Ash.Type.storage_type(other, constraints))
end
migration_type_from_storage_type(Ash.Type.storage_type(type, constraints))
end
defp migration_type_from_storage_type(:string), do: :text
defp migration_type_from_storage_type(:ci_string), do: :citext
defp migration_type_from_storage_type(storage_type), do: storage_type
defp foreign_key?(relationship) do
@ -2294,7 +2288,7 @@ defmodule AshSqlite.MigrationGenerator do
do: configured_default(resource, name) || "%{}"
defp default(%{name: name, default: value, type: type} = attr, resource, _) do
case configured_default(resource, name) do
case configured_default(resource, name) |> IO.inspect() do
nil ->
case migration_default(type, Map.get(attr, :constraints, []), value) do
{:ok, default} ->

View file

@ -0,0 +1,87 @@
{
"attributes": [
{
"default": "nil",
"size": null,
"type": "text",
"source": "state",
"references": null,
"primary_key?": false,
"allow_nil?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "uuid",
"source": "source_post_id",
"references": {
"name": "post_links_source_post_id_fkey",
"table": "posts",
"primary_key?": true,
"destination_attribute": "id",
"deferrable": false,
"destination_attribute_default": null,
"destination_attribute_generated": null,
"multitenancy": {
"global": null,
"attribute": null,
"strategy": null
},
"on_delete": null,
"on_update": null
},
"primary_key?": true,
"allow_nil?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "uuid",
"source": "destination_post_id",
"references": {
"name": "post_links_destination_post_id_fkey",
"table": "posts",
"primary_key?": true,
"destination_attribute": "id",
"deferrable": false,
"destination_attribute_default": null,
"destination_attribute_generated": null,
"multitenancy": {
"global": null,
"attribute": null,
"strategy": null
},
"on_delete": null,
"on_update": null
},
"primary_key?": true,
"allow_nil?": false,
"generated?": false
}
],
"table": "post_links",
"hash": "870ACE4A2EB4F54E61458810247D3AC4259B8B5C8EEFA187F7907C58F773B063",
"repo": "Elixir.AshSqlite.TestRepo",
"identities": [
{
"name": "unique_link",
"keys": [
"source_post_id",
"destination_post_id"
],
"base_filter": null,
"index_name": "post_links_unique_link_index"
}
],
"has_create_action": true,
"custom_indexes": [],
"custom_statements": [],
"base_filter": null,
"multitenancy": {
"global": null,
"attribute": null,
"strategy": null
}
}

View file

@ -0,0 +1,261 @@
{
"attributes": [
{
"default": "nil",
"size": null,
"type": "uuid",
"source": "id",
"references": null,
"primary_key?": true,
"allow_nil?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "title",
"references": null,
"primary_key?": false,
"allow_nil?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "bigint",
"source": "score",
"references": null,
"primary_key?": false,
"allow_nil?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "boolean",
"source": "public",
"references": null,
"primary_key?": false,
"allow_nil?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "citext",
"source": "category",
"references": null,
"primary_key?": false,
"allow_nil?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "type",
"references": null,
"primary_key?": false,
"allow_nil?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "bigint",
"source": "price",
"references": null,
"primary_key?": false,
"allow_nil?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "decimal",
"source": "decimal",
"references": null,
"primary_key?": false,
"allow_nil?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "status",
"references": null,
"primary_key?": false,
"allow_nil?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "status",
"source": "status_enum",
"references": null,
"primary_key?": false,
"allow_nil?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "map",
"source": "stuff",
"references": null,
"primary_key?": false,
"allow_nil?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "uniq_one",
"references": null,
"primary_key?": false,
"allow_nil?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "uniq_two",
"references": null,
"primary_key?": false,
"allow_nil?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "uniq_custom_one",
"references": null,
"primary_key?": false,
"allow_nil?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "uniq_custom_two",
"references": null,
"primary_key?": false,
"allow_nil?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "utc_datetime_usec",
"source": "created_at",
"references": null,
"primary_key?": false,
"allow_nil?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "utc_datetime_usec",
"source": "updated_at",
"references": null,
"primary_key?": false,
"allow_nil?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "uuid",
"source": "organization_id",
"references": {
"name": "posts_organization_id_fkey",
"table": "orgs",
"primary_key?": true,
"destination_attribute": "id",
"deferrable": false,
"destination_attribute_default": null,
"destination_attribute_generated": null,
"multitenancy": {
"global": null,
"attribute": null,
"strategy": null
},
"on_delete": null,
"on_update": null
},
"primary_key?": false,
"allow_nil?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "uuid",
"source": "author_id",
"references": {
"name": "posts_author_id_fkey",
"table": "authors",
"primary_key?": true,
"destination_attribute": "id",
"deferrable": false,
"destination_attribute_default": null,
"destination_attribute_generated": null,
"multitenancy": {
"global": null,
"attribute": null,
"strategy": null
},
"on_delete": null,
"on_update": null
},
"primary_key?": false,
"allow_nil?": true,
"generated?": false
}
],
"table": "posts",
"hash": "10A86C829A8D72D7C539C9041E229D375821CAB03D7819ED2FAA572F6A093195",
"repo": "Elixir.AshSqlite.TestRepo",
"identities": [
{
"name": "uniq_one_and_two",
"keys": [
"uniq_one",
"uniq_two"
],
"base_filter": "type = 'sponsored'",
"index_name": "posts_uniq_one_and_two_index"
}
],
"has_create_action": true,
"custom_indexes": [
{
"message": "dude what the heck",
"name": null,
"table": null,
"include": null,
"fields": [
"uniq_custom_one",
"uniq_custom_two"
],
"where": null,
"unique": true,
"using": null
}
],
"custom_statements": [],
"base_filter": "type = 'sponsored'",
"multitenancy": {
"global": null,
"attribute": null,
"strategy": null
}
}

View file

@ -0,0 +1,33 @@
defmodule AshSqlite.TestRepo.Migrations.MigrateResources2 do
@moduledoc """
Updates resources based on their most recent snapshots.
This file was autogenerated with `mix ash_sqlite.generate_migrations`
"""
use Ecto.Migration
def up do
alter table(:posts) do
modify :decimal, :decimal, default: nil
modify :type, :text, default: nil
modify :category, :citext
end
alter table(:post_links) do
modify :state, :text, default: nil
end
end
def down do
alter table(:post_links) do
modify :state, :text, default: "active"
end
alter table(:posts) do
modify :category, :ci_string
modify :type, :text, default: "sponsored"
modify :decimal, :decimal, default: "0"
end
end
end