Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace upsert field with source in EXCLUDED fragment #187

Merged
merged 4 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/data_layer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,7 @@ defmodule AshPostgres.DataLayer do
[],
fragment(
"COALESCE(EXCLUDED.?, ?)",
literal(^to_string(upsert_field)),
literal(^to_string(get_source_for_upsert_field(upsert_field, resource))),
^default
)
)}
Expand All @@ -1377,12 +1377,27 @@ defmodule AshPostgres.DataLayer do
{upsert_field,
Ecto.Query.dynamic(
[],
fragment("EXCLUDED.?", literal(^to_string(upsert_field)))
fragment(
"EXCLUDED.?",
literal(^to_string(get_source_for_upsert_field(upsert_field, resource)))
)
)}
end
end)
end

defp get_source_for_upsert_field(field, resource) do
case resource
|> Ash.Resource.Info.attributes()
|> Enum.find(&(&1.name == field)) do
%{source: source} when not is_nil(source) ->
source

_ ->
field
end
end
zachdaniel marked this conversation as resolved.
Show resolved Hide resolved

@impl true
def create(resource, changeset) do
changeset = %{
Expand Down
303 changes: 303 additions & 0 deletions priv/resource_snapshots/test_repo/posts/20231219132807.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,303 @@
{
"attributes": [
{
"default": "fragment(\"uuid_generate_v4()\")",
"size": null,
"type": "uuid",
"source": "id",
"references": null,
"allow_nil?": false,
"generated?": false,
"primary_key?": true
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "title_column",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "bigint",
"source": "score",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "boolean",
"source": "public",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "citext",
"source": "category",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "\"sponsored\"",
"size": null,
"type": "text",
"source": "type",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "bigint",
"source": "price",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "\"0\"",
"size": null,
"type": "decimal",
"source": "decimal",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "status",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "status",
"source": "status_enum",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": [
"array",
"float"
],
"source": "point",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "custom_point",
"source": "composite_point",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "map",
"source": "stuff",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "uniq_one",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "uniq_two",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "uniq_custom_one",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "uniq_custom_two",
"references": null,
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "fragment(\"now()\")",
"size": null,
"type": "utc_datetime_usec",
"source": "created_at",
"references": null,
"allow_nil?": false,
"generated?": false,
"primary_key?": false
},
{
"default": "fragment(\"now()\")",
"size": null,
"type": "utc_datetime_usec",
"source": "updated_at",
"references": null,
"allow_nil?": false,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "uuid",
"source": "organization_id",
"references": {
"name": "posts_organization_id_fkey",
"table": "orgs",
"schema": "public",
"on_delete": null,
"multitenancy": {
"global": null,
"strategy": null,
"attribute": null
},
"primary_key?": true,
"destination_attribute": "id",
"deferrable": false,
"match_type": null,
"match_with": null,
"on_update": null,
"destination_attribute_default": null,
"destination_attribute_generated": null
},
"allow_nil?": true,
"generated?": false,
"primary_key?": false
},
{
"default": "nil",
"size": null,
"type": "uuid",
"source": "author_id",
"references": {
"name": "posts_author_id_fkey",
"table": "authors",
"schema": "public",
"on_delete": null,
"multitenancy": {
"global": null,
"strategy": null,
"attribute": null
},
"primary_key?": true,
"destination_attribute": "id",
"deferrable": false,
"match_type": null,
"match_with": null,
"on_update": null,
"destination_attribute_default": null,
"destination_attribute_generated": null
},
"allow_nil?": true,
"generated?": false,
"primary_key?": false
}
],
"table": "posts",
"hash": "528A5F10049859ED0555DBAB43B0BA1D87A36F1EDB06FF9D9108F8E27F054898",
"repo": "Elixir.AshPostgres.TestRepo",
"identities": [
{
"name": "uniq_one_and_two",
"keys": [
"uniq_one",
"uniq_two"
],
"base_filter": "type = 'sponsored'",
"index_name": "posts_uniq_one_and_two_index"
}
],
"schema": null,
"check_constraints": [
{
"name": "price_must_be_positive",
"check": "price > 0",
"attribute": [
"price"
],
"base_filter": "type = 'sponsored'"
}
],
"custom_indexes": [
{
"message": "dude what the heck",
"name": null,
"table": null,
"include": null,
"prefix": null,
"where": null,
"fields": [
"uniq_custom_one",
"uniq_custom_two"
],
"unique": true,
"concurrently": true,
"using": null
}
],
"base_filter": "type = 'sponsored'",
"multitenancy": {
"global": null,
"strategy": null,
"attribute": null
},
"custom_statements": [],
"has_create_action": true
}
17 changes: 17 additions & 0 deletions priv/test_repo/migrations/20231219132807_migrate_resources13.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule AshPostgres.TestRepo.Migrations.MigrateResources13 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
rename table(:posts), :title, to: :title_column
end

def down do
rename table(:posts), :title_column, to: :title
end
end
4 changes: 3 additions & 1 deletion test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ defmodule AshPostgres.Test.Post do

attributes do
uuid_primary_key(:id, writable?: true)
attribute(:title, :string)
attribute(:title, :string) do
source :title_column
end
attribute(:score, :integer)
attribute(:public, :boolean)
attribute(:category, :ci_string)
Expand Down
Loading