From 43e2e24fee07fa6bc5a3ddcb450462fc41471053 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sat, 8 Mar 2025 21:40:15 +0200 Subject: [PATCH] Add the book eidtions registry to manage book editions. --- lib/decentralised_book_index/metadata.ex | 2 + lib/decentralised_book_index/metadata/book.ex | 4 + .../metadata/book_editions_registry.ex | 31 +++++ ..._registy_and_update_book_relationships.exs | 58 ++++++++++ .../20250308193833.json | 49 ++++++++ .../repo/books/20250308193833.json | 108 ++++++++++++++++++ 6 files changed, 252 insertions(+) create mode 100644 lib/decentralised_book_index/metadata/book_editions_registry.ex create mode 100644 priv/repo/migrations/20250308193833_create_book_editions_registy_and_update_book_relationships.exs create mode 100644 priv/resource_snapshots/repo/book_editions_registries/20250308193833.json create mode 100644 priv/resource_snapshots/repo/books/20250308193833.json diff --git a/lib/decentralised_book_index/metadata.ex b/lib/decentralised_book_index/metadata.ex index 3d66f2f..9cc9033 100644 --- a/lib/decentralised_book_index/metadata.ex +++ b/lib/decentralised_book_index/metadata.ex @@ -24,5 +24,7 @@ defmodule DecentralisedBookIndex.Metadata do define :update_author_alias_registry, action: :update define :destroy_author_alias_registry, action: :destroy end + + resource DecentralisedBookIndex.Metadata.BookEditionsRegistry end end diff --git a/lib/decentralised_book_index/metadata/book.ex b/lib/decentralised_book_index/metadata/book.ex index 57a72ea..068558d 100644 --- a/lib/decentralised_book_index/metadata/book.ex +++ b/lib/decentralised_book_index/metadata/book.ex @@ -33,4 +33,8 @@ defmodule DecentralisedBookIndex.Metadata.Book do timestamps() end + + relationships do + belongs_to :book_editions_registry, DecentralisedBookIndex.Metadata.BookEditionsRegistry + end end diff --git a/lib/decentralised_book_index/metadata/book_editions_registry.ex b/lib/decentralised_book_index/metadata/book_editions_registry.ex new file mode 100644 index 0000000..67d133e --- /dev/null +++ b/lib/decentralised_book_index/metadata/book_editions_registry.ex @@ -0,0 +1,31 @@ +defmodule DecentralisedBookIndex.Metadata.BookEditionsRegistry do + use Ash.Resource, + otp_app: :decentralised_book_index, + domain: DecentralisedBookIndex.Metadata, + data_layer: AshPostgres.DataLayer + + postgres do + table "book_editions_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.Book + end +end diff --git a/priv/repo/migrations/20250308193833_create_book_editions_registy_and_update_book_relationships.exs b/priv/repo/migrations/20250308193833_create_book_editions_registy_and_update_book_relationships.exs new file mode 100644 index 0000000..4b9c2f0 --- /dev/null +++ b/priv/repo/migrations/20250308193833_create_book_editions_registy_and_update_book_relationships.exs @@ -0,0 +1,58 @@ +defmodule DecentralisedBookIndex.Repo.Migrations.CreateBookEditionsRegistyAndUpdateBookRelationships 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(:books) do + add :book_editions_registry_id, :uuid + end + + create table(:book_editions_registries, primary_key: false) do + add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true + end + + alter table(:books) do + modify :book_editions_registry_id, + references(:book_editions_registries, + column: :id, + name: "books_book_editions_registry_id_fkey", + type: :uuid, + prefix: "public" + ) + end + + alter table(:book_editions_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(:book_editions_registries) do + remove :updated_at + remove :inserted_at + end + + drop constraint(:books, "books_book_editions_registry_id_fkey") + + alter table(:books) do + modify :book_editions_registry_id, :uuid + end + + drop table(:book_editions_registries) + + alter table(:books) do + remove :book_editions_registry_id + end + end +end diff --git a/priv/resource_snapshots/repo/book_editions_registries/20250308193833.json b/priv/resource_snapshots/repo/book_editions_registries/20250308193833.json new file mode 100644 index 0000000..c02e798 --- /dev/null +++ b/priv/resource_snapshots/repo/book_editions_registries/20250308193833.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": "0404DB6E78482892C260ABA34469476A75E6807C808B78413AF8BF113EFD9027", + "identities": [], + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "repo": "Elixir.DecentralisedBookIndex.Repo", + "schema": null, + "table": "book_editions_registries" +} \ No newline at end of file diff --git a/priv/resource_snapshots/repo/books/20250308193833.json b/priv/resource_snapshots/repo/books/20250308193833.json new file mode 100644 index 0000000..f0bd61c --- /dev/null +++ b/priv/resource_snapshots/repo/books/20250308193833.json @@ -0,0 +1,108 @@ +{ + "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": "title", + "type": "text" + }, + { + "allow_nil?": false, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "isbn", + "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": "books_book_editions_registry_id_fkey", + "on_delete": null, + "on_update": null, + "primary_key?": true, + "schema": "public", + "table": "book_editions_registries" + }, + "size": null, + "source": "book_editions_registry_id", + "type": "uuid" + } + ], + "base_filter": null, + "check_constraints": [], + "custom_indexes": [], + "custom_statements": [], + "has_create_action": false, + "hash": "0EB9DD72D6C15F49DDAC97FC59931F963130A9A6002C1E08F7B567FDB1B886F0", + "identities": [], + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "repo": "Elixir.DecentralisedBookIndex.Repo", + "schema": null, + "table": "books" +} \ No newline at end of file