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

Bug: the down/0 function generated by AshMoney.AshPostgresExtension is incorrect #80

Open
simpers opened this issue Oct 9, 2024 · 0 comments

Comments

@simpers
Copy link

simpers commented Oct 9, 2024

The tail of the error logs:

14:23:43.763 [info] drop table user_groups

14:23:43.771 [info] drop table user_profiles

14:23:43.778 [info] == Migrated 20241009122254 in 0.1s

14:23:43.960 [info] == Running 20241009122253 SecretSanta.Repo.Migrations.Install5Extensions20241009122251.down/0 forward

14:23:43.961 [info] execute "DROP FUNCTION IF EXISTS uuid_generate_v7(), timestamp_from_uuid_v7(uuid), ash_raise_error(jsonb), ash_raise_error(jsonb, ANYCOMPATIBLE), ash_elixir_and(BOOLEAN, ANYCOMPATIBLE), ash_elixir_and(ANYCOMPATIBLE, ANYCOMPATIBLE), ash_elixir_or(ANYCOMPATIBLE, ANYCOMPATIBLE), ash_elixir_or(BOOLEAN, ANYCOMPATIBLE), ash_trim_whitespace(text[])"

14:23:43.974 [info] execute "DROP OPERATOR >(money_with_currency, money_with_currency);"

14:23:43.980 [info] execute "DROP OPERATOR >(money_with_currency, numeric);"

14:23:43.985 [info] execute "DROP FUNCTION IF EXISTS money_gt(money_1 money_with_currency, money_2 money_with_currency);"
** (Postgrex.Error) ERROR 2BP01 (dependent_objects_still_exist) cannot drop function money_gt(money_with_currency,money_with_currency) because other objects depend on it

    hint: Use DROP ... CASCADE to drop the dependent objects too.

operator >=(money_with_currency,money_with_currency) depends on function money_gt(money_with_currency,money_with_currency)
    (ecto_sql 3.12.1) lib/ecto/adapters/sql.ex:1096:
...

To get going, I've replaced the generated down/0 with this for now, to get going:

  def down do
    execute(
      """
      DROP FUNCTION IF EXISTS uuid_generate_v7(),
                              timestamp_from_uuid_v7(uuid),
                              ash_raise_error(jsonb),
                              ash_raise_error(jsonb, ANYCOMPATIBLE),
                              ash_elixir_and(BOOLEAN, ANYCOMPATIBLE),
                              ash_elixir_and(ANYCOMPATIBLE, ANYCOMPATIBLE),
                              ash_elixir_or(ANYCOMPATIBLE, ANYCOMPATIBLE),
                              ash_elixir_or(BOOLEAN, ANYCOMPATIBLE),
                              ash_trim_whitespace(text[]);
      """
    )

    execute("DROP EXTENSION IF EXISTS \"citext\"")
    execute("DROP EXTENSION IF EXISTS \"pg_trgm\"")
    execute("DROP EXTENSION IF EXISTS \"uuid-ossp\"")

    execute "DROP TYPE IF EXISTS public.money_with_currency CASCADE;"
  end

This works, and it reports 35 objects being removed as part of the CASCADE, and I could count 35 rows in the original version of this function, so that suggest to me that something is out of order.

So far I attempted to rearrange things so that all operators were at the top, but that didn't solve it. The next thing I'd try is the aggregates, as I assumed they'd depend on +/-?

The culprit seems to be this one:

execute """
    CREATE OR REPLACE FUNCTION money_mult_reverse(money money_with_currency, multiplicator numeric)
    RETURNS money_with_currency
    IMMUTABLE
    STRICT
    LANGUAGE plpgsql
    AS $$
    BEGIN
        RETURN money_mult(multiplicator, money);
    END;
    $$;
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant