sezaru

sezaru

Is there any global configuration I change use to change both the default Ash.UUID.generate/0 function and gen_random_uuid() functions for uuid fields?

For example, I want to use uuid_v7 by default.

I know I can at least change the default Ash.UUID.generate/0 by using the default option in each attribute, but I believe in some cases that is not sufficient (ex. with an ash_authentication user_resource)

Showing Posts 1 to 5

zachdaniel

zachdaniel

Creator of Ash

IIRC ash_authentication will only add the id attribute if you haven’t already. I don’t believe there is a way to do it globally at the moment.

sezaru

sezaru OP

Ah, that’s true. I added a separated field for the id and it worked just fine.

That actually brings me to another question.

Is it possible to configure an Ash.Type to use some specific fragment for when generating migrations?

For example, I have my own Ash.Type for UUIDV7. I want to make it that when i add this attribute to some resource, it will automatically set the default postgres value as uuid_generate_v7() the same way that adding the default uuid primary key field for Ash adds the gen_random_uuid().

Of course, I can add that using the migration_defaults function, but that means that I need to do that for basically every resource in my system (since they all use my UUIDV7 type), it would be great if I could make that be added automatically.

I saw that there is a AshPostgres.Type that adds a callback for a function called value_to_postgres_default, but that function doesn’t seem to be used to generate that default.

Maybe we could add a new callback for AshPostgres.Type called @callback migration_default/0 where the user defines what is the default migration default value for the type that implements it?

zachdaniel

zachdaniel

Creator of Ash

Ah, so TBH I don’t think you actually want all usages of that type get a default value. Otherwise belongs_to :something, SomethingElse, type: UUIDV7 would also get a default value, which would fail to insert if that relationship is optional.

What I’d suggest is adding this to your app:

defmodule MyApp.Resource do
  def macro __using__(opts) do
     quote do
       use Ash.Resource, unquote(opts)

       import MyApp.Resource
    end
  end

  def macro uuid_v7_primary_key(name, opts) do
    quote do
      attribute unquote(name), UUIDV7, unquote(Keyword.put(opts, default: &UUIDV7.generate/0))
    end
  end
end

Then you can use MyApp.Resource instead of use Ash.Resource, and have uuid_v7_primary_key available.

With all of that said, the next release of Ash will have uuidv7 built in to core, so maybe you just want to use that?

sezaru

sezaru OP

Yep, that is actually what I used. Here is my implementation:

defmodule UUIDV7.Macros do
  @moduledoc false

  defmacro uuid_v7_primary_key(name, opts \\ []) do
    default =
      quote do
        fn -> UUIDV7.Type.generator([]) end
      end

    field_opts =
      opts
      |> Keyword.put_new(:primary_key?, true)
      |> Keyword.put_new(:allow_nil?, false)
      |> Keyword.put_new(:default, default)
      |> Keyword.put_new(:writable?, false)

    quote do
      attribute unquote(name), UUIDV7.Type, unquote(field_opts)
    end
  end

  defmacro uuid_v7_attribute(name, opts \\ []) do
    default =
      quote do
        fn -> UUIDV7.Type.generator([]) end
      end

    field_opts =
      opts
      |> Keyword.put_new(:primary_key?, false)
      |> Keyword.put_new(:allow_nil?, false)
      |> Keyword.put_new(:default, default)
      |> Keyword.put_new(:writable?, false)

    quote do
      attribute unquote(name), UUIDV7.Type, unquote(field_opts)
    end
  end
end

What bothered me with that is that this would not add default as uuid_generate_v7() in the postgres layer, it will be nil but the uuid_primary_key will add gen_random_uuid() as the default in postgres layer.

Is that built-in uuidv7 implementation already in master? Can it be tested already? that would be super great!

zachdaniel

zachdaniel

Creator of Ash

Yes, it is available in ash, main, but the ash_postgres changes are waiting to be merged until ash has been released, unfortunately. But you could try the branch from the PR.
improvement: add support for `:uuid_v7` type by moissela · Pull Request #333 · ash-project/ash_postgres · GitHub

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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 & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews