triggers.py 698 B

123456789101112131415161718192021
  1. # TODO actually run this on migration
  2. INCREMENT_PROJECT_COUNTER_TRIGGER = """
  3. DROP TRIGGER IF EXISTS increment_project_counter on issue_events_issue;
  4. CREATE OR REPLACE FUNCTION increment_project_counter() RETURNS trigger AS $$
  5. DECLARE
  6. counter_value int;
  7. BEGIN
  8. INSERT INTO projects_projectcounter (value, project_id)
  9. VALUES (0, NEW.project_id)
  10. ON CONFLICT (project_id) DO UPDATE SET value = projects_projectcounter.value + 1
  11. RETURNING value into counter_value;
  12. NEW.short_id=counter_value;
  13. RETURN NEW;
  14. END;
  15. $$ LANGUAGE plpgsql;;
  16. CREATE TRIGGER increment_project_counter BEFORE INSERT
  17. ON issue_events_issue FOR EACH ROW EXECUTE PROCEDURE
  18. increment_project_counter();
  19. """