zoonect-oss / ash_uuid

AshUUID: Extension for using UUID v4 and v7, with supports encoding and prefixing
https://hexdocs.pm/ash_uuid
MIT License
13 stars 7 forks source link

Ash.Type.generator/1 should return DataStream #8

Closed hwuethrich closed 4 months ago

hwuethrich commented 4 months ago

While using Ash.Generator.seed/1 I got the following error:

** (ArgumentError) expected a generator, which can be a %StreamData{} struct, an atom, or a tuple with generators in it, but got:

  "col_02wsdmCe7p61fA5nVLcFYJ"

If you want to use a term as a "constant" generator, wrap it in a call to StreamData.constant/1 instead.

I noticed that the callback Ash.Type.generator/1 should actually return an Enumerable instead of a single value:

@callback generator(constraints) :: Enumerable.t()

To fix the issue, I renamed the existing generator/1 to generate/1 and implemented the generator as expected (using Ash.Type.UUID as reference).

Heads-up: I also tried to add a test for the generator function by using Ash.Generator.seed!/1 in the existing tests (which failed, possibly because of an unrelated Ash bug). When I updated all the dependencies, I noticed that ash_postgres v2.0.10 will break your tests (v2.0.9 is fine):

  6) test AshUUID.PrefixedV4 testing oranges (AshUUIDTest)
     test/ash_uuid_test.exs:140
     ** (Ash.Error.Unknown) Unknown Error

     * ** (Postgrex.Error) ERROR 22P02 (invalid_text_representation) invalid input syntax for type uuid: "orange-smoothie_3ZhwNimx2NuodIDMu2xmwo". If you are trying to query a JSON field, the parameter may need to be interpolated. Instead of

         p.json["field"] != "value"

     do

         p.json["field"] != ^"value"

         query: SELECT o0."id", o0."inserted_at", o0."orange_smoothie_id" FROM "oranges" AS o0 WHERE (o0."orange_smoothie_id"::uuid IN ('orange-smoothie_3ZhwNimx2NuodIDMu2xmwo'))
         at oranges
       (ecto_sql 3.11.3) lib/ecto/adapters/sql.ex:1054: Ecto.Adapters.SQL.raise_sql_call_error/1
       (ecto_sql 3.11.3) lib/ecto/adapters/sql.ex:952: Ecto.Adapters.SQL.execute/6
       (ecto 3.11.2) lib/ecto/repo/queryable.ex:232: Ecto.Repo.Queryable.execute/4
       (ecto 3.11.2) lib/ecto/repo/queryable.ex:19: Ecto.Repo.Queryable.all/3
       (ash_postgres 2.0.10) lib/data_layer.ex:758: anonymous fn/3 in AshPostgres.DataLayer.run_query/2
       (ash_postgres 2.0.10) lib/data_layer.ex:756: AshPostgres.DataLayer.run_query/2
       (ash 3.0.15) lib/ash/actions/read/read.ex:2423: Ash.Actions.Read.run_query/4
       (ash 3.0.15) lib/ash/actions/read/read.ex:447: anonymous fn/5 in Ash.Actions.Read.do_read/4
       (ash 3.0.15) lib/ash/actions/read/read.ex:777: Ash.Actions.Read.maybe_in_transaction/3
       (ash 3.0.15) lib/ash/actions/read/read.ex:248: Ash.Actions.Read.do_run/3
       (ash 3.0.15) lib/ash/actions/read/read.ex:66: anonymous fn/3 in Ash.Actions.Read.run/3
moissela commented 4 months ago

Hi @hwuethrich I'll merge this as soon as #9 is resolved.

The current generator was clearly wrong until now šŸ™ˆ

Thank you for notice this bug, opening the PR and your contribution!

hwuethrich commented 4 months ago

@moissela It looks like the other issue was caused by a bug in ash_postgres 2.0.9

moissela commented 4 months ago

Thanks @hwuethrich for your contribution šŸš€

I'll release new AshUUID version to hex shortly!

moissela commented 4 months ago

Released!