@@ -320,3 +320,56 @@ def evolve16(self):
320
320
on newt (get_community_zoid(zoid, class_name, state));
321
321
"""
322
322
)
323
+
324
+ evolve19 = (
325
+ "Better find_community_zoid and simpler tigger function" ,
326
+ """
327
+ create or replace function find_community_zoid(
328
+ zoid_ bigint, class_name text, state jsonb)
329
+ returns bigint
330
+ as $$
331
+ declare
332
+ parent_class_name text;
333
+ parent_state jsonb;
334
+ parent_id bigint;
335
+ begin
336
+ if state is null then return null; end if;
337
+ if class_name = 'karl.models.community.Community' then
338
+ return zoid_;
339
+ end if;
340
+ if state ? 'community_zoid' then
341
+ return (state ->> 'community_zoid')::bigint;
342
+ end if;
343
+ parent_id := (state -> '__parent__' ->> '::=>')::bigint;
344
+ if parent_id is null then return null; end if;
345
+ select newt.class_name, newt.state from newt where zoid = parent_id
346
+ into parent_class_name, parent_state;
347
+
348
+ if parent_class_name is null then
349
+ return null;
350
+ end if;
351
+
352
+ return find_community_zoid(
353
+ parent_id, parent_class_name, parent_state);
354
+ end
355
+ $$ language plpgsql STABLE;
356
+
357
+ create or replace function populate_community_zoid_triggerf()
358
+ returns trigger
359
+ as $$
360
+ declare
361
+ new_zoid bigint;
362
+ zoid bigint;
363
+ begin
364
+ NEW.state :=
365
+ NEW.state || ('{"::trigger_was_here": true}')::jsonb;
366
+ zoid := find_community_zoid(
367
+ NEW.zoid, NEW.class_name, NEW.state)::text;
368
+ if zoid is not null then
369
+ NEW.state :=
370
+ NEW.state || ('{"community_zoid": ' || zoid || '}')::jsonb;
371
+ end if;
372
+ return NEW;
373
+ end
374
+ $$ language plpgsql STABLE;
375
+ """ )
0 commit comments