Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. CREATE OR REPLACE FUNCTION generate_counters()
  2. RETURNS TRIGGER AS $$
  3. DECLARE
  4. repo_url TEXT;
  5. BEGIN
  6. repo_url = NEW.webhook#>'{repository, html_url}';
  7.  
  8. -- repo counter
  9. INSERT INTO cdp_data.build_counter AS c (id, next_value) VALUES (repo_url, 1)
  10. ON CONFLICT (id) DO UPDATE SET next_value = c.next_value + 1
  11. RETURNING next_value;
  12.  
  13. -- branch counter
  14. INSERT INTO cdp_data.build_counter AS c (id, next_value)
  15. VALUES (repo_url || coalesce(NEW.webhook#>'{pull_request, base, ref}', NEW.webhook#>'{ref}', 1)
  16. ON CONFLICT (id) DO UPDATE SET next_value = c.next_value + 1
  17. RETURNING next_value;
  18.  
  19. -- pr counter
  20. INSERT INTO cdp_data.build_counter AS c (id, next_value)
  21. VALUES (repo_url || NEW.webhook#>'{pull_request, number}', 1)
  22. ON CONFLICT (id) DO UPDATE SET next_value = c.next_value + 1
  23. RETURNING next_value;
  24.  
  25. RETURN new;
  26. END;
  27. $$ LANGUAGE PLPSQL;
  28.  
  29. CREATE TRIGGER insert_counters BEFORE INSERT ON cdp_data.lifecycle
  30. FOR EACH ROW EXECURE PROCEDURE generate_counters();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement