diff --git a/lib/decentralised_book_index/metadata/author_role.ex b/lib/decentralised_book_index/metadata/author_role.ex index 2b2df4b..c010e0a 100644 --- a/lib/decentralised_book_index/metadata/author_role.ex +++ b/lib/decentralised_book_index/metadata/author_role.ex @@ -14,6 +14,10 @@ defmodule DecentralisedBookIndex.Metadata.AuthorRole do postgres do table "author_role" repo DecentralisedBookIndex.Repo + + references do + reference :book, index?: true, on_delete: :delete + end end actions do diff --git a/priv/repo/migrations/20250408105711_update_author_role_delete_logic_to_cascade_if_no_book.exs b/priv/repo/migrations/20250408105711_update_author_role_delete_logic_to_cascade_if_no_book.exs new file mode 100644 index 0000000..3d74e5c --- /dev/null +++ b/priv/repo/migrations/20250408105711_update_author_role_delete_logic_to_cascade_if_no_book.exs @@ -0,0 +1,42 @@ +defmodule DecentralisedBookIndex.Repo.Migrations.UpdateAuthorRoleDeleteLogicToCascadeIfNoBook 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(:author_role, "author_role_book_id_fkey") + + alter table(:author_role) do + modify :book_id, + references(:books, + column: :id, + name: "author_role_book_id_fkey", + type: :uuid, + prefix: "public", + on_delete: :delete_all + ) + end + + create index(:author_role, [:book_id]) + end + + def down do + drop_if_exists index(:author_role, [:book_id]) + + drop constraint(:author_role, "author_role_book_id_fkey") + + alter table(:author_role) do + modify :book_id, + references(:books, + column: :id, + name: "author_role_book_id_fkey", + type: :uuid, + prefix: "public" + ) + end + end +end diff --git a/priv/resource_snapshots/repo/author_role/20250408105712.json b/priv/resource_snapshots/repo/author_role/20250408105712.json new file mode 100644 index 0000000..1e84db7 --- /dev/null +++ b/priv/resource_snapshots/repo/author_role/20250408105712.json @@ -0,0 +1,156 @@ +{ + "attributes": [ + { + "allow_nil?": false, + "default": "fragment(\"gen_random_uuid()\")", + "generated?": false, + "primary_key?": true, + "references": null, + "size": null, + "source": "id", + "type": "uuid" + }, + { + "allow_nil?": true, + "default": "nil", + "generated?": false, + "primary_key?": false, + "references": null, + "size": null, + "source": "role", + "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?": false, + "match_type": null, + "match_with": null, + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "name": "author_role_dbi_server_id_fkey", + "on_delete": null, + "on_update": null, + "primary_key?": true, + "schema": "public", + "table": "dbi_servers" + }, + "size": null, + "source": "dbi_server_id", + "type": "uuid" + }, + { + "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": "author_role_book_id_fkey", + "on_delete": "delete", + "on_update": null, + "primary_key?": true, + "schema": "public", + "table": "books" + }, + "size": null, + "source": "book_id", + "type": "uuid" + }, + { + "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": "author_role_author_id_fkey", + "on_delete": null, + "on_update": null, + "primary_key?": true, + "schema": "public", + "table": "authors" + }, + "size": null, + "source": "author_id", + "type": "uuid" + } + ], + "base_filter": null, + "check_constraints": [], + "custom_indexes": [], + "custom_statements": [], + "has_create_action": true, + "hash": "9E4F8AF2665BB1D2073A2FC4587C55DCB455E85298D4AADC87D221A8231771CD", + "identities": [], + "multitenancy": { + "attribute": null, + "global": null, + "strategy": null + }, + "repo": "Elixir.DecentralisedBookIndex.Repo", + "schema": null, + "table": "author_role" +} \ No newline at end of file