Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error creating migration #2

Closed
gordoneliel opened this issue Jan 9, 2024 · 5 comments
Closed

Error creating migration #2

gordoneliel opened this issue Jan 9, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@gordoneliel
Copy link
Contributor

Trying to setup ash money but getting an error while running mix ash_postgres.generate_migrations

Stacktrace below:

Extension Migrations: 
** (UndefinedFunctionError) function Money.DDL.execute_each/1 is undefined (module Money.DDL is not available)
    Money.DDL.execute_each("CREATE OR REPLACE FUNCTION money_mult(multiplicator numeric, money money_with_currency)\nRETURNS money_with_currency\nIMMUTABLE\nSTRICT\nLANGUAGE plpgsql\nAS $$\n  DECLARE\n    currency varchar;\n    multiplication numeric;\n  BEGIN\n      currency := currency_code(money);\n      multiplication := amount(money) * multiplicator;\n      return row(currency, multiplication);\n  END;\n$$;\n\n\nCREATE OR REPLACE FUNCTION money_mult_reverse(money money_with_currency, multiplicator numeric)\nRETURNS money_with_currency\nIMMUTABLE\nSTRICT\nLANGUAGE plpgsql\nAS $$\nBEGIN\n    RETURN money_mult(multiplicator, money);\nEND;\n$$;\n\n\nCREATE OPERATOR * (\n    LEFTARG = numeric,\n    RIGHTARG = money_with_currency,\n    PROCEDURE = money_mult\n);\n\n\nCREATE OPERATOR * (\n    LEFTARG = money_with_currency,\n    RIGHTARG = numeric,\n    PROCEDURE = money_mult_reverse\n);\n")
    (ash_money 0.1.4) lib/ash_money/ash_postgres_extension.ex:10: AshMoney.AshPostgresExtension.install/1
    (elixir 1.15.7) lib/enum.ex:1794: anonymous fn/2 in Enum.map_join/3
    (elixir 1.15.7) lib/enum.ex:4369: Enum.map_intersperse_list/3
    (elixir 1.15.7) lib/enum.ex:4372: Enum.map_intersperse_list/3
    (elixir 1.15.7) lib/enum.ex:1794: Enum.map_join/3
    (ash_postgres 1.3.68) lib/migration_generator/migration_generator.ex:237: anonymous fn/2 in AshPostgres.MigrationGenerator.create_extension_migrations/2
    (elixir 1.15.7) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
@gordoneliel gordoneliel added the enhancement New feature or request label Jan 9, 2024
@gordoneliel
Copy link
Contributor Author

It appears to work if I add ex_money and ex_money_sql directly as deps. Not sure it that is intended or not

@gordoneliel
Copy link
Contributor Author

I think there is something wrong with the order of postgres migrations, below has an operator defined before money is defined:

  """
      #{Money.DDL.execute_each(add_money_mult())}
      #{Money.DDL.execute_each(Money.DDL.create_money_with_currency())}
      #{Money.DDL.execute_each(Money.DDL.define_plus_operator())}
      #{Money.DDL.execute_each(Money.DDL.define_minmax_functions())}
      #{Money.DDL.execute_each(Money.DDL.define_sum_function())}

@kiliancs
Copy link

kiliancs commented Feb 5, 2024

It appears to work if I add ex_money and ex_money_sql directly as deps. Not sure it that is intended or not

ex_money_sql is needed. See https://ash-hq.org/docs/guides/ash_money/latest/tutorials/get-started-with-ash-money#ashpostgres-support

However, I also run into problems with the migrations. Apparently, v2 only installs the multiplication functions and operators, but not the money_with_currency type (that is required for multiplication and for everything else). I contributed a fix here #5

@Kagemaru
Copy link

Kagemaru commented May 5, 2024

@gordoneliel @kiliancs
I have encountered this problem too and asked about it on Discord.
It is expected, that we run the ex_money_sql migrations to create the type/functions/operator.

I've added the required steps to the Readme in this PR.

Hope this helps. 😃

@zachdaniel
Copy link
Contributor

So this turns out to actually have been a bug in AshPostgres custom extensions, which is now fixed in the latest versions. Upgrade both ash_money and ash_postgres for the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants