diff --git a/priv/resource_snapshots/test_repo/records_temp_entities/20250605230457.json b/priv/resource_snapshots/test_repo/records_temp_entities/20250605230457.json new file mode 100644 index 00000000..af6e3c23 --- /dev/null +++ b/priv/resource_snapshots/test_repo/records_temp_entities/20250605230457.json @@ -0,0 +1,93 @@ +{ + "attributes": [ + { + "allow_nil?": false, + "default": "fragment(\"gen_random_uuid()\")", + "generated?": false, + "precision": null, + "primary_key?": true, + "references": null, + "scale": null, + "size": null, + "source": "id", + "type": "uuid" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "precision": null, + "primary_key?": false, + "references": { + "deferrable": false, + "destination_attribute": "id", + "destination_attribute_default": null, + "destination_attribute_generated": null, + "index?": false, + "match_type": null, + "match_with": null, + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "name": "records_temp_entities_record_id_fkey", + "on_delete": null, + "on_update": null, + "primary_key?": true, + "schema": "public", + "table": "records" + }, + "scale": null, + "size": null, + "source": "record_id", + "type": "uuid" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "precision": null, + "primary_key?": false, + "references": { + "deferrable": false, + "destination_attribute": "id", + "destination_attribute_default": null, + "destination_attribute_generated": null, + "index?": false, + "match_type": null, + "match_with": null, + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "name": "records_temp_entities_temp_entity_id_fkey", + "on_delete": null, + "on_update": null, + "primary_key?": true, + "schema": "temp", + "table": "temp_entities" + }, + "scale": null, + "size": null, + "source": "temp_entity_id", + "type": "uuid" + } + ], + "base_filter": null, + "check_constraints": [], + "custom_indexes": [], + "custom_statements": [], + "has_create_action": true, + "hash": "1F00231E86F4E276096E683FD2836B40F36C6AD617A777D947E908FD52D09FDB", + "identities": [], + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "repo": "Elixir.AshPostgres.TestRepo", + "schema": null, + "table": "records_temp_entities" +} \ No newline at end of file diff --git a/priv/test_repo/migrations/20250605230457_create_record_temp_entities_table.exs b/priv/test_repo/migrations/20250605230457_create_record_temp_entities_table.exs new file mode 100644 index 00000000..fe910c8c --- /dev/null +++ b/priv/test_repo/migrations/20250605230457_create_record_temp_entities_table.exs @@ -0,0 +1,43 @@ +defmodule AshPostgres.TestRepo.Migrations.CreateRecordTempEntitiesTable 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 + create table(:records_temp_entities, primary_key: false) do + add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true) + + add( + :record_id, + references(:records, + column: :id, + name: "records_temp_entities_record_id_fkey", + type: :uuid, + prefix: "public" + ) + ) + + add( + :temp_entity_id, + references(:temp_entities, + column: :id, + name: "records_temp_entities_temp_entity_id_fkey", + type: :uuid, + prefix: "temp" + ) + ) + end + end + + def down do + drop(constraint(:records_temp_entities, "records_temp_entities_record_id_fkey")) + + drop(constraint(:records_temp_entities, "records_temp_entities_temp_entity_id_fkey")) + + drop(table(:records_temp_entities)) + end +end diff --git a/test/calculation_test.exs b/test/calculation_test.exs index 4b6fff35..6aa911fe 100644 --- a/test/calculation_test.exs +++ b/test/calculation_test.exs @@ -1,4 +1,5 @@ defmodule AshPostgres.CalculationTest do + alias AshPostgres.Test.RecordTempEntity use AshPostgres.RepoCase, async: false alias AshPostgres.Test.{Account, Author, Comedian, Comment, Post, Record, TempEntity, User} @@ -1018,9 +1019,12 @@ defmodule AshPostgres.CalculationTest do end test "calculation references use the appropriate schema" do - Record |> Ash.Changeset.for_create(:create, %{full_name: "name"}) |> Ash.create!() + record = Record |> Ash.Changeset.for_create(:create, %{full_name: "name"}) |> Ash.create!() - TempEntity |> Ash.Changeset.for_create(:create, %{full_name: "name"}) |> Ash.create!() + temp_entity = + TempEntity |> Ash.Changeset.for_create(:create, %{full_name: "name"}) |> Ash.create!() + + Ash.Seed.seed!(RecordTempEntity, %{record_id: record.id, temp_entity_id: temp_entity.id}) full_name = Record diff --git a/test/support/domain.ex b/test/support/domain.ex index 25616374..c8c596d5 100644 --- a/test/support/domain.ex +++ b/test/support/domain.ex @@ -31,6 +31,7 @@ defmodule AshPostgres.Test.Domain do resource(AshPostgres.Test.Entity) resource(AshPostgres.Test.ContentVisibilityGroup) resource(AshPostgres.Test.TempEntity) + resource(AshPostgres.Test.RecordTempEntity) resource(AshPostgres.Test.Permalink) resource(AshPostgres.Test.Record) resource(AshPostgres.Test.PostFollower) diff --git a/test/support/resources/record.ex b/test/support/resources/record.ex index db820fca..804c981a 100644 --- a/test/support/resources/record.ex +++ b/test/support/resources/record.ex @@ -28,6 +28,12 @@ defmodule AshPostgres.Test.Record do source_attribute(:full_name) destination_attribute(:full_name) end + + many_to_many :temp_entities, AshPostgres.Test.TempEntity do + public?(true) + + through(AshPostgres.Test.RecordTempEntity) + end end postgres do @@ -36,7 +42,11 @@ defmodule AshPostgres.Test.Record do end calculations do - calculate(:temp_entity_full_name, :string, expr(temp_entity.full_name)) + calculate( + :temp_entity_full_name, + :string, + expr(fragment("coalesce(?, '')", temp_entities.full_name)) + ) end actions do diff --git a/test/support/resources/record_temp_entity.ex b/test/support/resources/record_temp_entity.ex new file mode 100644 index 00000000..7750a1df --- /dev/null +++ b/test/support/resources/record_temp_entity.ex @@ -0,0 +1,25 @@ +defmodule AshPostgres.Test.RecordTempEntity do + @moduledoc false + + use Ash.Resource, + domain: AshPostgres.Test.Domain, + data_layer: AshPostgres.DataLayer + + postgres do + table "records_temp_entities" + repo AshPostgres.TestRepo + end + + attributes do + uuid_primary_key(:id) + end + + relationships do + belongs_to(:record, AshPostgres.Test.Record, public?: true) + belongs_to(:temp_entity, AshPostgres.Test.TempEntity, public?: true) + end + + actions do + defaults([:read, :destroy, create: :*, update: :*]) + end +end