Skip to content

feat: Ash domain layer over core's item tables (#342) - #352

Open
spaenleh wants to merge 2 commits into
mainfrom
342-ash-foundation
Open

spaenleh wants to merge 2 commits into
mainfrom
342-ash-foundation

Conversation

@spaenleh

@spaenleh spaenleh commented Dec 4, 2026

Copy link
Copy Markdown
Member

Closes #342

Summary

Ash in admin, step 1 of ADR 0003's transition. Admin.Repo becomes an AshPostgres.Repo, and the first resources sit over core's tables with migrate? false and core's constraint and index names.

 lib/admin/
+├── core/
+│   ├── items.ex                  # domain; get_item/2 with authorize_with: :error
+│   ├── items/item.ex             # effective_permission(account_id:), read policy
+│   ├── items/checks/at_least.ex  # "at least p on this item"
+│   ├── items/item_visibility.ex
+│   ├── memberships.ex
+│   ├── memberships/item_membership.ex
+│   ├── memberships/permission.ex # permission_enum, read < write < admin
+│   ├── members.ex
+│   ├── members/account.ex
+│   └── expressions/ancestor_or_self.ex   # ltree @>
 └── repo.ex                       # use AshPostgres.Repo
 priv/repo/migrations/
+├── …_install_ash_functions_and_btree_gist_extensions_1.exs  # generated
+└── …_add_item_membership_account_id_item_path_index.exs     # gist, concurrently

The item read policy, in order:

authorize_if  Effective Permission >= write      # Hidden Items included
forbid_if     Hidden Item
authorize_if  Effective Permission >= read
authorize_if  public
  • The primary read leaves out soft-deleted items, as core does.
  • mix precommit and CI run mix ash_postgres.generate_migrations --check.
  • Ecto 3.14, dropping poison and pointing ex_aws at Jason were already on main.
  • The porting recipe now names the Admin.Core.* domains.

Evidence

  • Before: no Ash resources. test/admin/core/items_test.exs doesn't exist.
    After: 12 tests pass:
    • Effective Permission: inherited, highest wins, only the given account's, nil on a public item.
    • get_item/2:
      • ancestor membership → ok
      • Hidden Item and below, for a reader → Forbidden
      • same for a writer → ok
      • public and below, for a stranger or no actor → ok
      • no permission → Forbidden (403)
      • missing id → NotFound (404)
      • soft-deleted → NotFound
    • List read: drops Hidden Items and items without access.
  • Mutation check: with forbid_if expr(is_hidden) removed, the two Hidden Item tests fail.
  • mix precommit: 477 tests, 0 failures. credo --strict is clean.
  • Admin.Release.migrate() on a fresh database runs both migrations. The index rolls back and rebuilds valid (indisvalid = t).

Merge Danger

Door: two-way, mostly. The code reverts cleanly. The migrations add functions, an extension and an index to the shared database, and a revert leaves them in place, where they do no harm. Both migrations have a down.

Blast Radius: database

  • On deploy, Admin.Release.migrate runs:
    • CREATE EXTENSION IF NOT EXISTS btree_gist, which needs the migration role to own the database;
    • Ash's SQL functions (ash_*, uuid_generate_v7), created in public;
    • a concurrent gist index build on item_membership, about 144k rows on the 2026-11-12 dump.
  • Admin.Repo changes module type. The existing Ecto code works unchanged (the full suite passes).
  • No route uses the new resources yet.
  • Admin.ItemsTest "get_descendants" is flaky on main (two items tie on the sort order). This PR doesn't touch it.

Admin.Repo becomes an AshPostgres.Repo; ash, ash_postgres and picosat_elixir
are added. A generated migration installs the ash functions and btree_gist,
and a hand-written one adds the item_membership (account_id, item_path) gist
index concurrently (ADR 0003).

Item, ItemMembership, ItemVisibility and Account resources sit over core's
tables with migrate? false and core's constraint and index names, in the
Admin.Core.Items, Memberships and Members domains. Item carries the shared
item policies: the effective_permission calculation taking account_id, the
AtLeast check, and get_item/2 with authorize_with: :error for 403 vs 404.

mix precommit and CI run ash_postgres.generate_migrations --check.
A failed concurrent index build leaves an invalid index under the same name,
which create_if_not_exists would then skip: drop it first. Account's moduledoc
no longer says core owns the schema (ADR 0002). The ~s() fixture docs closed
at their inner parenthesis.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Foundation: Ash in admin

1 participant