Skip to content

Commit db9d341

Browse files
test: building error payloads with immutable functions
1 parent 8e446ab commit db9d341

File tree

13 files changed

+659
-1
lines changed

13 files changed

+659
-1
lines changed

priv/resource_snapshots/test_repo/extensions.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"pg_trgm",
77
"citext",
88
"demo-functions_v1",
9+
"immutable_raise_error_v2",
910
"ltree"
1011
]
1112
}
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
{
2+
"attributes": [
3+
{
4+
"allow_nil?": false,
5+
"default": "fragment(\"gen_random_uuid()\")",
6+
"generated?": false,
7+
"precision": null,
8+
"primary_key?": true,
9+
"references": null,
10+
"scale": null,
11+
"size": null,
12+
"source": "id",
13+
"type": "uuid"
14+
},
15+
{
16+
"allow_nil?": false,
17+
"default": "nil",
18+
"generated?": false,
19+
"precision": null,
20+
"primary_key?": false,
21+
"references": null,
22+
"scale": null,
23+
"size": null,
24+
"source": "atom_value",
25+
"type": "text"
26+
},
27+
{
28+
"allow_nil?": false,
29+
"default": "nil",
30+
"generated?": false,
31+
"precision": null,
32+
"primary_key?": false,
33+
"references": null,
34+
"scale": null,
35+
"size": null,
36+
"source": "string_value",
37+
"type": "text"
38+
},
39+
{
40+
"allow_nil?": false,
41+
"default": "nil",
42+
"generated?": false,
43+
"precision": null,
44+
"primary_key?": false,
45+
"references": null,
46+
"scale": null,
47+
"size": null,
48+
"source": "integer_value",
49+
"type": "bigint"
50+
},
51+
{
52+
"allow_nil?": false,
53+
"default": "nil",
54+
"generated?": false,
55+
"precision": null,
56+
"primary_key?": false,
57+
"references": null,
58+
"scale": null,
59+
"size": null,
60+
"source": "float_value",
61+
"type": "float"
62+
},
63+
{
64+
"allow_nil?": false,
65+
"default": "nil",
66+
"generated?": false,
67+
"precision": null,
68+
"primary_key?": false,
69+
"references": null,
70+
"scale": null,
71+
"size": null,
72+
"source": "boolean_value",
73+
"type": "boolean"
74+
},
75+
{
76+
"allow_nil?": false,
77+
"default": "nil",
78+
"generated?": false,
79+
"precision": null,
80+
"primary_key?": false,
81+
"references": null,
82+
"scale": null,
83+
"size": null,
84+
"source": "struct_value",
85+
"type": "map"
86+
},
87+
{
88+
"allow_nil?": false,
89+
"default": "nil",
90+
"generated?": false,
91+
"precision": null,
92+
"primary_key?": false,
93+
"references": null,
94+
"scale": null,
95+
"size": null,
96+
"source": "uuid_value",
97+
"type": "uuid"
98+
},
99+
{
100+
"allow_nil?": false,
101+
"default": "nil",
102+
"generated?": false,
103+
"precision": null,
104+
"primary_key?": false,
105+
"references": null,
106+
"scale": null,
107+
"size": null,
108+
"source": "date_value",
109+
"type": "date"
110+
},
111+
{
112+
"allow_nil?": false,
113+
"default": "nil",
114+
"generated?": false,
115+
"precision": null,
116+
"primary_key?": false,
117+
"references": null,
118+
"scale": null,
119+
"size": null,
120+
"source": "time_value",
121+
"type": "time"
122+
},
123+
{
124+
"allow_nil?": false,
125+
"default": "nil",
126+
"generated?": false,
127+
"precision": null,
128+
"primary_key?": false,
129+
"references": null,
130+
"scale": null,
131+
"size": null,
132+
"source": "ci_string_value",
133+
"type": "citext"
134+
},
135+
{
136+
"allow_nil?": false,
137+
"default": "nil",
138+
"generated?": false,
139+
"precision": null,
140+
"primary_key?": false,
141+
"references": null,
142+
"scale": null,
143+
"size": null,
144+
"source": "naive_datetime_value",
145+
"type": "naive_datetime"
146+
},
147+
{
148+
"allow_nil?": false,
149+
"default": "nil",
150+
"generated?": false,
151+
"precision": null,
152+
"primary_key?": false,
153+
"references": null,
154+
"scale": null,
155+
"size": null,
156+
"source": "utc_datetime_value",
157+
"type": "utc_datetime"
158+
},
159+
{
160+
"allow_nil?": false,
161+
"default": "nil",
162+
"generated?": false,
163+
"precision": null,
164+
"primary_key?": false,
165+
"references": null,
166+
"scale": null,
167+
"size": null,
168+
"source": "timestamptz_value",
169+
"type": "timestamptz"
170+
},
171+
{
172+
"allow_nil?": false,
173+
"default": "nil",
174+
"generated?": false,
175+
"precision": null,
176+
"primary_key?": false,
177+
"references": null,
178+
"scale": null,
179+
"size": null,
180+
"source": "string_array_value",
181+
"type": [
182+
"array",
183+
"text"
184+
]
185+
},
186+
{
187+
"allow_nil?": false,
188+
"default": "nil",
189+
"generated?": false,
190+
"precision": null,
191+
"primary_key?": false,
192+
"references": null,
193+
"scale": null,
194+
"size": null,
195+
"source": "response_value",
196+
"type": "integer"
197+
},
198+
{
199+
"allow_nil?": true,
200+
"default": "nil",
201+
"generated?": false,
202+
"precision": null,
203+
"primary_key?": false,
204+
"references": null,
205+
"scale": null,
206+
"size": null,
207+
"source": "nullable_string_value",
208+
"type": "text"
209+
}
210+
],
211+
"base_filter": null,
212+
"check_constraints": [],
213+
"custom_indexes": [],
214+
"custom_statements": [],
215+
"has_create_action": true,
216+
"hash": "BB177B855B058F50CA20AFCF6F624072C1395BFDF3FB0B2F60BDC14A15A053F1",
217+
"identities": [],
218+
"multitenancy": {
219+
"attribute": null,
220+
"global": null,
221+
"strategy": null
222+
},
223+
"repo": "Elixir.AshPostgres.TestRepo",
224+
"schema": null,
225+
"table": "immutable_error_testers"
226+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
defmodule AshPostgres.TestRepo.Migrations.InstallImmutableRaiseErrorV220251015134237 do
2+
@moduledoc """
3+
Installs any extensions that are mentioned in the repo's `installed_extensions/0` callback
4+
5+
This file was autogenerated with `mix ash_postgres.generate_migrations`
6+
"""
7+
8+
use Ecto.Migration
9+
10+
def up do
11+
execute("""
12+
CREATE OR REPLACE FUNCTION ash_raise_error_immutable(json_data jsonb, token anycompatible)
13+
RETURNS boolean AS $$
14+
BEGIN
15+
-- Raise an error with the provided JSON data.
16+
-- The JSON object is converted to text for inclusion in the error message.
17+
-- 'token' is intentionally ignored; its presence makes the call non-constant at the call site.
18+
RAISE EXCEPTION 'ash_error: %', json_data::text;
19+
RETURN NULL;
20+
END;
21+
$$ LANGUAGE plpgsql
22+
IMMUTABLE
23+
SET search_path = '';
24+
""")
25+
26+
execute("""
27+
CREATE OR REPLACE FUNCTION ash_raise_error_immutable(json_data jsonb, type_signal anyelement, token anycompatible)
28+
RETURNS anyelement AS $$
29+
BEGIN
30+
-- Raise an error with the provided JSON data.
31+
-- The JSON object is converted to text for inclusion in the error message.
32+
-- 'token' is intentionally ignored; its presence makes the call non-constant at the call site.
33+
RAISE EXCEPTION 'ash_error: %', json_data::text;
34+
RETURN NULL;
35+
END;
36+
$$ LANGUAGE plpgsql
37+
IMMUTABLE
38+
SET search_path = '';
39+
""")
40+
41+
execute("""
42+
CREATE OR REPLACE FUNCTION ash_to_jsonb_immutable(value anyelement)
43+
RETURNS jsonb
44+
LANGUAGE plpgsql
45+
IMMUTABLE
46+
SET search_path TO 'pg_catalog'
47+
SET "TimeZone" TO 'UTC'
48+
SET "DateStyle" TO 'ISO, YMD'
49+
SET "IntervalStyle" TO 'iso_8601'
50+
SET extra_float_digits TO '0'
51+
SET bytea_output TO 'hex'
52+
AS $function$
53+
BEGIN
54+
RETURN COALESCE(to_jsonb(value), 'null'::jsonb);
55+
END;
56+
$function$
57+
""")
58+
end
59+
60+
def down do
61+
# Uncomment this if you actually want to uninstall the extensions
62+
# when this migration is rolled back:
63+
execute("DROP FUNCTION IF EXISTS ash_to_jsonb_immutable(anyelement)")
64+
end
65+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
defmodule AshPostgres.TestRepo.Migrations.MigrateResources63 do
2+
@moduledoc """
3+
Updates resources based on their most recent snapshots.
4+
5+
This file was autogenerated with `mix ash_postgres.generate_migrations`
6+
"""
7+
8+
use Ecto.Migration
9+
10+
def up do
11+
create table(:immutable_error_testers, primary_key: false) do
12+
add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true)
13+
add(:atom_value, :text, null: false)
14+
add(:string_value, :text, null: false)
15+
add(:integer_value, :bigint, null: false)
16+
add(:float_value, :float, null: false)
17+
add(:boolean_value, :boolean, null: false)
18+
add(:struct_value, :map, null: false)
19+
add(:uuid_value, :uuid, null: false)
20+
add(:date_value, :date, null: false)
21+
add(:time_value, :time, null: false)
22+
add(:ci_string_value, :citext, null: false)
23+
add(:naive_datetime_value, :naive_datetime, null: false)
24+
add(:utc_datetime_value, :utc_datetime, null: false)
25+
add(:timestamptz_value, :timestamptz, null: false)
26+
add(:string_array_value, {:array, :text}, null: false)
27+
add(:response_value, :integer, null: false)
28+
add(:nullable_string_value, :text)
29+
end
30+
end
31+
32+
def down do
33+
drop(table(:immutable_error_testers))
34+
end
35+
end

0 commit comments

Comments
 (0)