kipcole9 / money_sql

Money functions for the serialization of a money data type in Elixir
Other
28 stars 18 forks source link

Getting `type/0` is undefined errors when upgrading #17

Closed coladarci closed 3 years ago

coladarci commented 3 years ago

I just went to upgrade a few dependencies:

ecto 3.6.1 => 3.6.2
ex_money 5.5.1 => 5.5.3
ex_money_sql 1.3.1 => 1.4.4

And all my tests are failing w/ this:

     ** (UndefinedFunctionError) function Money.Ecto.Composite.Type.type/0 is undefined or private. Did you mean one of:

           * type/1

     stacktrace:
       (ex_money_sql 1.4.4) Money.Ecto.Composite.Type.type()
       (ecto 3.6.2) lib/ecto/type.ex:906: Ecto.Type.adapter_dump/3
       (elixir 1.12.0) lib/enum.ex:2356: Enum."-reduce/3-lists^foldl/2-0-"/3
       (elixir 1.12.0) lib/enum.ex:1675: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
       (elixir 1.12.0) lib/enum.ex:1675: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
       (elixir 1.12.0) lib/enum.ex:2356: Enum."-reduce/3-lists^foldl/2-0-"/3
       (ecto 3.6.2) lib/ecto/repo/queryable.ex:208: Ecto.Repo.Queryable.execute/4
       (ecto 3.6.2) lib/ecto/repo/queryable.ex:19: Ecto.Repo.Queryable.all/3

I remember seeing you get into a conversation w/ Jose about this, upstream, in ecto, but I can't seem to find that. I don't see any issues filed here so I wanted to check if I'm missing something..

Thanks, as always, Kip!

kipcole9 commented 3 years ago

Greg, thats a complete pain. I've checked the ecto source at the error and its not particularly illuminating.

The underlying change is that I changed the Money.Ecto.Composite.Type to a parameterised type which is supported in Ecto ~> 3.5 and therefore allows defaults which is a good thing. However it introduced an incompatibility I wasn't aware of until after I published the new release of ex_money_sql.

To my knowledge, the only area that might be causing this exception is if you are calling the type macro to cast database values in a schemaless query. Any chance that could be the issue for you? If you are using type/2 in schemaless queries there is a change to the call that needs to be made. Basically is needs to be of the form of this example from the tests:

type(sum(organization.payroll), ^Money.Ecto.Composite.Type.cast_type())

If thats not it then hopefully you can help me with a minimal failing test and I can dig into it further.

kipcole9 commented 3 years ago

BTW, the original issue filed that links to the discussion on the ecto repo is here

coladarci commented 3 years ago

Ah, I'm doing this:

where(Credit, [c], c.price < type(^value, Money.Ecto.Composite.Type))

Could this be what you are referring to?

Credit is a normal ecto schema..

coladarci commented 3 years ago

Thank you for this lead - while I couldn't get your example to work (something needs a force recompile that I can't pinpoint) doing it myself money_type = Ecto.ParameterizedType.init(Money.Ecto.Composite.Type, []) and using that DID work. Appreciate it!

kipcole9 commented 3 years ago

@coladarci, thanks for the update Greg. I see that I haven't documented this nearly well enough. I do note that you should (as best I understand it) be able to do:

where(Credit, [c], c.price < type(^value, c.price))

Which removes any requirement to understand the deeper plumbing.

coladarci commented 3 years ago

Oh wow - I had no idea you could do this... that does work...