diff --git a/lib/decentralised_book_index/metadata/author.ex b/lib/decentralised_book_index/metadata/author.ex index 9b7cb8d..545c8b6 100644 --- a/lib/decentralised_book_index/metadata/author.ex +++ b/lib/decentralised_book_index/metadata/author.ex @@ -10,7 +10,17 @@ defmodule DecentralisedBookIndex.Metadata.Author do end actions do - defaults [:read] + defaults [:read, :update, :destroy] + + update :add_author_to_alias_registry do + accept [:author_alias_registry_id] + end + + create :create do + primary? true + accept [:name, :description, :author_alias_registry_id] + + end end attributes do @@ -28,4 +38,10 @@ defmodule DecentralisedBookIndex.Metadata.Author do timestamps() end + + relationships do + belongs_to :author_alias_registry, DecentralisedBookIndex.Metadata.AuthorAliasRegistry do + attribute_writable? true + end + end end diff --git a/lib/decentralised_book_index/metadata/author_alias_registry.ex b/lib/decentralised_book_index/metadata/author_alias_registry.ex new file mode 100644 index 0000000..63781c8 --- /dev/null +++ b/lib/decentralised_book_index/metadata/author_alias_registry.ex @@ -0,0 +1,31 @@ +defmodule DecentralisedBookIndex.Metadata.AuthorAliasRegistry do + use Ash.Resource, + otp_app: :decentralised_book_index, + domain: DecentralisedBookIndex.Metadata, + data_layer: AshPostgres.DataLayer + + postgres do + table "author_alias_registries" + repo DecentralisedBookIndex.Repo + end + + actions do + defaults [:read, :create, :update, :destroy] + + read :by_id do + argument :id, :uuid, allow_nil?: false + get? true + filter expr(id == ^arg(:id)) + end + end + + attributes do + uuid_primary_key :id + + timestamps() + end + + relationships do + has_many :authors, DecentralisedBookIndex.Metadata.Author + end +end diff --git a/priv/repo/migrations/20250306194335_create_author_alias_registy_and_update_author_relationships.exs b/priv/repo/migrations/20250306194335_create_author_alias_registy_and_update_author_relationships.exs new file mode 100644 index 0000000..fbbdf8d --- /dev/null +++ b/priv/repo/migrations/20250306194335_create_author_alias_registy_and_update_author_relationships.exs @@ -0,0 +1,58 @@ +defmodule DecentralisedBookIndex.Repo.Migrations.CreateAuthorAliasRegistyAndUpdateAuthorRelationships 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(:authors) do + add :author_alias_registry_id, :uuid + end + + create table(:author_alias_registries, primary_key: false) do + add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true + end + + alter table(:authors) do + modify :author_alias_registry_id, + references(:author_alias_registries, + column: :id, + name: "authors_author_alias_registry_id_fkey", + type: :uuid, + prefix: "public" + ) + end + + alter table(:author_alias_registries) do + add :inserted_at, :utc_datetime_usec, + null: false, + default: fragment("(now() AT TIME ZONE 'utc')") + + add :updated_at, :utc_datetime_usec, + null: false, + default: fragment("(now() AT TIME ZONE 'utc')") + end + end + + def down do + alter table(:author_alias_registries) do + remove :updated_at + remove :inserted_at + end + + drop constraint(:authors, "authors_author_alias_registry_id_fkey") + + alter table(:authors) do + modify :author_alias_registry_id, :uuid + end + + drop table(:author_alias_registries) + + alter table(:authors) do + remove :author_alias_registry_id + end + end +end diff --git a/priv/resource_snapshots/repo/author_alias_registries/20250306194335.json b/priv/resource_snapshots/repo/author_alias_registries/20250306194335.json new file mode 100644 index 0000000..3ca4e82 --- /dev/null +++ b/priv/resource_snapshots/repo/author_alias_registries/20250306194335.json @@ -0,0 +1,49 @@ +{ + "attributes": [ + { + "allow_nil?": false, + "default": "fragment(\"gen_random_uuid()\")", + "generated?": false, + "primary_key?": true, + "references": null, + "size": null, + "source": "id", + "type": "uuid" + }, + { + "allow_nil?": false, + "default": "fragment(\"(now() AT TIME ZONE 'utc')\")", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "inserted_at", + "type": "utc_datetime_usec" + }, + { + "allow_nil?": false, + "default": "fragment(\"(now() AT TIME ZONE 'utc')\")", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "updated_at", + "type": "utc_datetime_usec" + } + ], + "base_filter": null, + "check_constraints": [], + "custom_indexes": [], + "custom_statements": [], + "has_create_action": true, + "hash": "B99F9DEA022478D48097625B854593389263945E08965F78B54D41A7D6D16696", + "identities": [], + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "repo": "Elixir.DecentralisedBookIndex.Repo", + "schema": null, + "table": "author_alias_registries" +} \ No newline at end of file diff --git a/priv/resource_snapshots/repo/authors/20250306194335.json b/priv/resource_snapshots/repo/authors/20250306194335.json new file mode 100644 index 0000000..7ceeb51 --- /dev/null +++ b/priv/resource_snapshots/repo/authors/20250306194335.json @@ -0,0 +1,98 @@ +{ + "attributes": [ + { + "allow_nil?": false, + "default": "fragment(\"gen_random_uuid()\")", + "generated?": false, + "primary_key?": true, + "references": null, + "size": null, + "source": "id", + "type": "uuid" + }, + { + "allow_nil?": false, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "name", + "type": "text" + }, + { + "allow_nil?": false, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "description", + "type": "text" + }, + { + "allow_nil?": false, + "default": "fragment(\"(now() AT TIME ZONE 'utc')\")", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "inserted_at", + "type": "utc_datetime_usec" + }, + { + "allow_nil?": false, + "default": "fragment(\"(now() AT TIME ZONE 'utc')\")", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "updated_at", + "type": "utc_datetime_usec" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "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": "authors_author_alias_registry_id_fkey", + "on_delete": null, + "on_update": null, + "primary_key?": true, + "schema": "public", + "table": "author_alias_registries" + }, + "size": null, + "source": "author_alias_registry_id", + "type": "uuid" + } + ], + "base_filter": null, + "check_constraints": [], + "custom_indexes": [], + "custom_statements": [], + "has_create_action": true, + "hash": "490E9E7EB08795DE4BD8A1B1A01EAF8B0CCD813B9F659BD56658CA9B069C140F", + "identities": [], + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "repo": "Elixir.DecentralisedBookIndex.Repo", + "schema": null, + "table": "authors" +} \ No newline at end of file