functions.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. GENERATE_ISSUE_TSVECTOR = """
  2. CREATE OR REPLACE FUNCTION generate_issue_tsvector(jsonb) RETURNS tsvector AS $$
  3. BEGIN
  4. RETURN strip(jsonb_to_tsvector($1, '["string"]'));
  5. EXCEPTION WHEN program_limit_exceeded THEN
  6. RETURN null;
  7. END;
  8. $$ LANGUAGE plpgsql;;
  9. """
  10. UPDATE_ISSUE_INDEX = """
  11. DROP PROCEDURE IF EXISTS update_issue_index;
  12. CREATE OR REPLACE PROCEDURE update_issue_index(update_issue_id integer)
  13. LANGUAGE SQL
  14. AS $$
  15. WITH event_agg as (
  16. SELECT COUNT(events_event.event_id) as new_count,
  17. MAX(events_event.created) as new_last_seen,
  18. MAX(events_event.level) as new_level
  19. FROM events_event
  20. WHERE events_event.issue_id=update_issue_id
  21. ), event_vector as (
  22. SELECT strip(COALESCE(generate_issue_tsvector(data), '') || COALESCE(issues_issue.search_vector, '')) as vector
  23. FROM events_event
  24. LEFT JOIN issues_issue on issues_issue.id = events_event.issue_id
  25. WHERE events_event.issue_id=update_issue_id
  26. limit 1
  27. ), event_tags as (
  28. SELECT jsonb_object_agg(y.key, y.values) as new_tags FROM (
  29. SELECT (a).key, array_agg(distinct(a).value) as values
  30. FROM (
  31. SELECT each(tags) as a
  32. FROM events_event
  33. WHERE events_event.issue_id=update_issue_id
  34. ) t GROUP by key
  35. ) y
  36. )
  37. UPDATE issues_issue
  38. SET
  39. count = event_agg.new_count,
  40. last_seen = event_agg.new_last_seen,
  41. level = event_agg.new_level,
  42. search_vector = CASE WHEN search_vector is null or length(search_vector) < 100000 THEN event_vector.vector ELSE search_vector END,
  43. tags = CASE WHEN event_Tags.new_tags is not null THEN event_tags.new_tags ELSE tags END
  44. FROM event_agg, event_vector, event_tags
  45. WHERE issues_issue.id = update_issue_id;
  46. $$;
  47. """