|
| 1 | +defmodule AshPostgres.Extensions.ImmutableRaiseError do |
| 2 | + @moduledoc """ |
| 3 | + An extension that installs an immutable version of ash_raise_error. |
| 4 | +
|
| 5 | + This can be used to improve compatibility with Postgres sharding extensions like Citus, |
| 6 | + which requires functions used in CASE or COALESCE expressions to be immutable. |
| 7 | +
|
| 8 | + The new `ash_raise_error_immutable` functions add an additional row-dependent argument to ensure |
| 9 | + the planner doesn't constant-fold error expressions. |
| 10 | +
|
| 11 | + To install, add this module to your repo's `installed_extensions` list: |
| 12 | +
|
| 13 | + ```elixir |
| 14 | + def installed_extensions do |
| 15 | + ["ash-functions", AshPostgres.Extensions.ImmutableRaiseError] |
| 16 | + end |
| 17 | + ``` |
| 18 | +
|
| 19 | + And run `mix ash_postgres.generate_migrations` to generate the migrations. |
| 20 | +
|
| 21 | + Once installed, you can control whether the immutable function is used by adding this to your |
| 22 | + repo: |
| 23 | +
|
| 24 | + ```elixir |
| 25 | + def immutable_expr_error?, do: true |
| 26 | + ``` |
| 27 | + """ |
| 28 | + |
| 29 | + use AshPostgres.CustomExtension, name: "immutable_raise_error", latest_version: 1 |
| 30 | + |
| 31 | + @impl true |
| 32 | + def install(0) do |
| 33 | + ash_raise_error_immutable() |
| 34 | + end |
| 35 | + |
| 36 | + @impl true |
| 37 | + def uninstall(_version) do |
| 38 | + "execute(\"DROP FUNCTION IF EXISTS ash_raise_error_immutable(jsonb, ANYCOMPATIBLE), ash_raise_error_immutable(jsonb, ANYELEMENT, ANYCOMPATIBLE)\")" |
| 39 | + end |
| 40 | + |
| 41 | + defp ash_raise_error_immutable do |
| 42 | + """ |
| 43 | + execute(\"\"\" |
| 44 | + CREATE OR REPLACE FUNCTION ash_raise_error_immutable(json_data jsonb, token ANYCOMPATIBLE) |
| 45 | + RETURNS BOOLEAN AS $$ |
| 46 | + BEGIN |
| 47 | + -- Raise an error with the provided JSON data. |
| 48 | + -- The JSON object is converted to text for inclusion in the error message. |
| 49 | + -- 'token' is intentionally ignored; its presence makes the call non-constant at the call site. |
| 50 | + RAISE EXCEPTION 'ash_error: %', json_data::text; |
| 51 | + RETURN NULL; |
| 52 | + END; |
| 53 | + $$ LANGUAGE plpgsql |
| 54 | + IMMUTABLE |
| 55 | + SET search_path = ''; |
| 56 | + \"\"\") |
| 57 | +
|
| 58 | + execute(\"\"\" |
| 59 | + CREATE OR REPLACE FUNCTION ash_raise_error_immutable(json_data jsonb, type_signal ANYELEMENT, token ANYCOMPATIBLE) |
| 60 | + RETURNS ANYELEMENT AS $$ |
| 61 | + BEGIN |
| 62 | + -- Raise an error with the provided JSON data. |
| 63 | + -- The JSON object is converted to text for inclusion in the error message. |
| 64 | + -- 'token' is intentionally ignored; its presence makes the call non-constant at the call site. |
| 65 | + RAISE EXCEPTION 'ash_error: %', json_data::text; |
| 66 | + RETURN NULL; |
| 67 | + END; |
| 68 | + $$ LANGUAGE plpgsql |
| 69 | + IMMUTABLE |
| 70 | + SET search_path = ''; |
| 71 | + \"\"\") |
| 72 | + """ |
| 73 | + end |
| 74 | +end |
0 commit comments