zac

zac

Best approach: update affecting other resources (multiple changesets from one action)

I’m trying to figure out how to do this, and have come up with something that may be the completely wrong approach. :slight_smile:

Imagine we have a resource that represents a column in a table. Each column has a number so I can easily represent it; just sort :asc in order, and draw from left-to-right.

Now, someone wants to change the order of the columns. I think there are two (fundamental) approach here: 1) I could read all the columns into memory, reorder them at the UI level, and then update the entire set. Maybe that’s the best option. But I started down the path of writing a update :set_column_number that looks like this:

NOTE this is all basically pseudocode, as I haven’t got it working.

    update :set_column_number do
      argument :new_number, :integer
      change Api.Column.Preparations.SetNumber
    end

My thinking is that the preparation could reorder the necessary columns. This is where I’m heading with it:

defmodule Api.Column.Preparations.SetNumber do
  def change(changeset, _, _) do
    current_number = Ash.Changeset.get_attribute(changeset, :number)
    new_number = Ash.Changeset.get_argument(changeset, :new_number)

    # change the target column to it's new position
    Ash.Changeset.change_attribute(changeset, :column, new_number)

    # collect any other affected columns that we need to re-order
    other_columns = Api.Column
    |> Ash.Query.filter(number >= ^new_column and number != ^number)
    |> Api.read!()

    # re-order them (well, create a changeset for the lot of them anyhow)
    all_changes = for column <- other_columns do
      column
      |> Ash.Changeset.for_update(:update, [number: number + 1])
    end

    # hand-wavy magic about combining the changesets and "it just works"
    # hint: (it doesn't, I get back an list of changesets and... not sure what next)
    all_changes ++ changeset
end

In theory it seems like it should work. Main questions:

  1. Is there a better way to do it (using Ash, versus in the UI layer)
  2. And if this isn’t a really bad approach, how to fix up the last little bit (after combining the changesets)?

First 4 of 4 Posts! Switch mode

zachdaniel

zachdaniel

Creator of Ash

Hello! At the moment, Ash does not have bulk updates, so this is in fact the best way to do this if you want to use only Ash (i.e iterating each and updating its position). Ash will have bulk updates (updates with a query that execute as a single update statement), but until then, I’d suggest using Ecto directly.

defmodule Api.Column.Preparations.SetNumber do
  def change(changeset, _, _) do
    id = changeset.data.id
    current_number = Ash.Changeset.get_attribute(changeset, :number)
    new_number = Ash.Changeset.get_argument(changeset, :new_number)

    # change the target column to it's new position
    Ash.Changeset.change_attribute(changeset, :column, new_number)

    # use a before action hook
    Ash.Changeset.before_action(changeset, fn changeset -> 
       {:ok, bumping_forward_query} = 
          Api.Column
          |> Ash.Query.filter(number >= ^new_column and id != ^id)
          |> Ash.Query.data_layer_query()

       {:ok, bumping_backwards_query} = 
          Api.Column
          |> Ash.Query.filter(number <= ^new_number and number >= current_number and id != ^id)
          |> Ash.Query.data_layer_query()


      YourRepo.update(bumping_forward_query, inc: [number: 1])
      YourRepo.update(bumping_backwards_query, dec: [number: -1])

      changeset
    end)
end

You can also simplify by removing the argument and just detecting the column changing

defmodule Api.Column.Preparations.SetNumber do
  def change(changeset, _, _) do
    if Ash.Changeset.changing_attribute?(changeset, :number) do
      id = changeset.data.id
      current_number = changeset.data.number
      new_number = Ash.Changeset.get_attribute(changeset, :number)

      # use a before action hook
      Ash.Changeset.before_action(changeset, fn changeset -> 
         {:ok, bumping_forward_query} = 
            Api.Column
            |> Ash.Query.filter(number >= ^new_column and id != ^id)
            |> Ash.Query.data_layer_query()

         {:ok, bumping_backwards_query} = 
            Api.Column
            |> Ash.Query.filter(number <= ^new_number and number >= current_number and id != ^id)
            |> Ash.Query.data_layer_query()


        YourRepo.update(bumping_forward_query, inc: [number: 1])
        YourRepo.update(bumping_backwards_query, dec: [number: -1])

        changeset
      end)
  else
    changeset
  end
end

I haven’t run this code, but it should be essentially what you need.

FWIW there are strategies around sorted relationships that use sparse sets of numbers and/or floating point numbers very cleverly to avoid having to rebalance things often. I’d like to make an extension at some point that will add that capability automatically to a relationship.

zac

zac

First off, very excited to know batch updates are coming. Can’t wait!

This is much better… but, something’s missing. Tried it out and the call to Ecto (YourRepo) is giving an Unknown Error aka “giving a struct to Ecto is not supported” (FYI, actual code, so COE.Walk.ActivityStereotype == Api.Column in the pseudocode above):

     ** (Ash.Error.Unknown) Unknown Error

     Context: resolving data on commit COE.Walk.ActivityStereotype.set_order
     * Context: resolving data on commit COE.Walk.ActivityStereotype.set_order

     ** (ArgumentError) giving a struct to Ecto.Repo.update/2 is not supported. Ecto is unable to properly track changes when a struct is given, an Ecto.Changeset must be given instead
       (ecto 3.11.0) lib/ecto/repo/schema.ex:406: Ecto.Repo.Schema.update/4
       (boss_site 0.1.0) lib/coe/walk/preparations/set_order.ex:30: anonymous fn/4 in COE.Walk.ActivityStereotype.Preparations.SetOrder.change/3
...
zachdaniel

zachdaniel

Creator of Ash

Ah, right, it should be .update_all, not .update

zac

zac

One more thing; there is no dec: but there is an inc: where the argument is negative. Otherwise, looks good! Thanks, appreciate the pointer to dropping into Ecto. Hadn’t done that before / didn’t realize it was so easy to do.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement