Skip to content

Commit ce86ff7

Browse files
authored
Merge pull request #165 from karlproject/fix-community-id
Fix community id
2 parents 387a508 + 8be850c commit ce86ff7

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

karl/scripts/pgevolve.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,56 @@ def evolve16(self):
320320
on newt (get_community_zoid(zoid, class_name, state));
321321
"""
322322
)
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+
""")

versions.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ zc.recipe.deployment = 1.3.0
422422

423423
# Required by:
424424
# karl==4.31.1
425-
newt.db = 0.7.0
425+
newt.db = 0.8.0
426426

427427
# Added by buildout at 2017-04-25 11:03:09.112017
428428

0 commit comments

Comments
 (0)