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

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews