From 709905a8e1695a68a2a0d59f884498526efa7982 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Thu, 20 Mar 2025 16:49:17 +0200 Subject: [PATCH] Update BookId to add the reference logic. --- .../metadata/book_id.ex | 11 +- ..._update_book_id_to_add_reference_logic.exs | 42 +++++++ .../repo/book_ids/20250316152615.json | 108 ++++++++++++++++++ 3 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 priv/repo/migrations/20250316152615_update_book_id_to_add_reference_logic.exs create mode 100644 priv/resource_snapshots/repo/book_ids/20250316152615.json diff --git a/lib/decentralised_book_index/metadata/book_id.ex b/lib/decentralised_book_index/metadata/book_id.ex index eceb02a..7ffab58 100644 --- a/lib/decentralised_book_index/metadata/book_id.ex +++ b/lib/decentralised_book_index/metadata/book_id.ex @@ -9,16 +9,25 @@ defmodule DecentralisedBookIndex.Metadata.BookId do postgres do table "book_ids" repo DecentralisedBookIndex.Repo + + references do + reference :book, index?: true, on_delete: :delete + end end actions do - defaults [:read, :update, :destroy] + defaults [:read, :destroy] create :create do primary? true accept [:type, :bid, :order] end + update :update do + primary? true + accept [:type, :bid, :order] + end + read :by_id do argument :id, :uuid, allow_nil?: false get? true diff --git a/priv/repo/migrations/20250316152615_update_book_id_to_add_reference_logic.exs b/priv/repo/migrations/20250316152615_update_book_id_to_add_reference_logic.exs new file mode 100644 index 0000000..4006687 --- /dev/null +++ b/priv/repo/migrations/20250316152615_update_book_id_to_add_reference_logic.exs @@ -0,0 +1,42 @@ +defmodule DecentralisedBookIndex.Repo.Migrations.UpdateBookIdToAddReferenceLogic 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 + drop constraint(:book_ids, "book_ids_book_id_fkey") + + alter table(:book_ids) do + modify :book_id, + references(:books, + column: :id, + name: "book_ids_book_id_fkey", + type: :uuid, + prefix: "public", + on_delete: :delete_all + ) + end + + create index(:book_ids, [:book_id]) + end + + def down do + drop_if_exists index(:book_ids, [:book_id]) + + drop constraint(:book_ids, "book_ids_book_id_fkey") + + alter table(:book_ids) do + modify :book_id, + references(:books, + column: :id, + name: "book_ids_book_id_fkey", + type: :uuid, + prefix: "public" + ) + end + end +end diff --git a/priv/resource_snapshots/repo/book_ids/20250316152615.json b/priv/resource_snapshots/repo/book_ids/20250316152615.json new file mode 100644 index 0000000..cb8b0d2 --- /dev/null +++ b/priv/resource_snapshots/repo/book_ids/20250316152615.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": "type", + "type": "text" + }, + { + "allow_nil?": false, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "bid", + "type": "text" + }, + { + "allow_nil?": false, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "order", + "type": "bigint" + }, + { + "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?": true, + "match_type": null, + "match_with": null, + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "name": "book_ids_book_id_fkey", + "on_delete": "delete", + "on_update": null, + "primary_key?": true, + "schema": "public", + "table": "books" + }, + "size": null, + "source": "book_id", + "type": "uuid" + } + ], + "base_filter": null, + "check_constraints": [], + "custom_indexes": [], + "custom_statements": [], + "has_create_action": true, + "hash": "0C61931CA6A175418C2C2BAA9BA133BF7BC272119F73CFAF7E2F3A10FD0D4925", + "identities": [], + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "repo": "Elixir.DecentralisedBookIndex.Repo", + "schema": null, + "table": "book_ids" +} \ No newline at end of file