Add a way to persist a calculation for a resource

Open
#383 2 comments 2 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
elixir, postgresql
Domain
databases

Research direction

No source files or tests are named. Start by tracing how Ash resource generated attributes become PostgreSQL/Ecto migrations, then compare the generate_with and persist? proposals with Ecto's add/3 generated-column examples. Done means one agreed resource interface emits the correct generated-column migration and covers the calculation and raw-SQL cases with tests.

Written by the indexing model from the issue text.

Description

enhancement

Is your feature request related to a problem? Please describe.
When defining generated attributes for a resource, I often see people needing to manually edit the migration to use ALTER TABLE to explicitly add the column with the SQL statement used to populate the column.

Describe the solution you'd like
A way to in an Ash resource to define an attribute that is generated, specify the raw SQL statement to populate the field, and have the generated migration use the Ecto syntax for configuring such a column shown here

Describe alternatives you've considered
Continue manually changing migrations or replace generated attribute with calculations that are not persisted in a column

Express the feature either with a change to resource syntax, or with a change to the resource interface

Idea 1 - generate_with attribute option

Using calculation
  attributes do
    attribute :generated_foo, :string, generate_with: :full_name
  end

  calculations do
    calculate :full_name, :string, expr(first_name <> " " <> last_name)
  end
Using raw SQL
  attributes do
    attribute :generated_foo, :string, generate_with: expr(fragment("? || ' ' || ?", first_name, last_name))
  end

Idea 2 - persist calculation option

  calculations do
    calculate :full_name, :string, expr(first_name <> " " <> last_name)

    persist? true
  end

Additional context
Link to the start of the conversation in the Elixir Discord server that lead to this proposal: message

Dominant language
Elixir
Stars
191
Forks
186
Avg merge
16h 4m
Merged PRs (30d)
17

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ash-project/ash_postgres

All issues in ash-project/ash_postgres

Similar issues

More Elixir issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.