insert_event.sql 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. CREATE OR REPLACE FUNCTION event_preprocess (_project_id numeric, _sentry_key uuid, _release_name text, _environment_name text, OUT _organization_id int, OUT _has_diffs boolean, OUT _is_accepting_events boolean, OUT _should_scrub_ip_addresses boolean, OUT _release_id int, OUT _environment_id int)
  2. AS $$
  3. DECLARE
  4. _first_event timestamp with time zone;
  5. BEGIN
  6. SELECT
  7. INTO _organization_id,
  8. _first_event,
  9. _is_accepting_events,
  10. _should_scrub_ip_addresses,
  11. _has_diffs,
  12. _release_id,
  13. _environment_id --
  14. projects_project.organization_id,
  15. projects_project.first_event,
  16. is_accepting_events,
  17. projects_project.scrub_ip_addresses,
  18. EXISTS (
  19. SELECT
  20. 1
  21. FROM
  22. difs_debuginformationfile
  23. WHERE
  24. project_id = _project_id
  25. LIMIT 1) AS has_diffs,
  26. releases_release.id,
  27. environments_environment.id
  28. FROM
  29. projects_project
  30. INNER JOIN projects_projectkey ON (projects_project.id = projects_projectkey.project_id)
  31. INNER JOIN organizations_ext_organization ON (projects_project.organization_id = organizations_ext_organization.id)
  32. LEFT JOIN releases_releaseproject ON projects_project.id = releases_releaseproject.project_id
  33. LEFT JOIN releases_release ON (releases_release.id = releases_releaseproject.release_id
  34. AND releases_release.version = _release_name)
  35. LEFT JOIN environments_environmentproject ON projects_project.id = environments_environmentproject.project_id
  36. LEFT JOIN environments_environment ON (environments_environment.id = environments_environmentproject.environment_id
  37. AND environments_environment.name = _environment_name)
  38. WHERE (projects_project.id = _project_id
  39. AND projects_projectkey.public_key = _sentry_key);
  40. IF _organization_id IS NOT NULL AND _project_id::int::bool THEN
  41. -- Get release_id, if not known and it exists
  42. IF _release_name IS NOT NULL AND _release_id IS NULL THEN
  43. SELECT
  44. INTO _release_id id
  45. FROM
  46. releases_release
  47. WHERE
  48. releases_release.organization_id = _organization_id
  49. AND releases_release.version = _release_name
  50. LIMIT 1;
  51. -- If no release, create it
  52. IF _release_id IS NULL THEN
  53. INSERT INTO releases_release (version, organization_id, created, data, commit_count, deploy_count)
  54. VALUES (_release_name, _organization_id, now(), '{}'::json, 0, 0)
  55. RETURNING
  56. id INTO _release_id;
  57. END IF;
  58. -- Insert project - release relationship if it doesn't exist
  59. INSERT INTO releases_releaseproject (project_id, release_id)
  60. VALUES (_project_id, _release_id)
  61. ON CONFLICT
  62. DO NOTHING;
  63. END IF;
  64. -- Get environment_id, if it exists
  65. IF _environment_name IS NOT NULL AND _environment_id IS NULL THEN
  66. SELECT
  67. INTO _environment_id environments_environment.id
  68. FROM
  69. environments_environment
  70. LEFT JOIN environments_environmentproject ON environments_environmentproject.environment_id = environments_environment.id
  71. WHERE
  72. environments_environment.organization_id = _organization_id
  73. AND environments_environment.name = _environment_name
  74. LIMIT 1;
  75. -- If no environment, create it
  76. IF _environment_id IS NULL THEN
  77. INSERT INTO environments_environment (name, organization_id, created)
  78. VALUES (_environment_name, _organization_id, now())
  79. RETURNING
  80. id INTO _environment_id;
  81. END IF;
  82. -- Insert project-environment relationship if not exists
  83. INSERT INTO environments_environmentproject (project_id, environment_id, is_hidden, created)
  84. VALUES (_project_id, _environment_id, FALSE, now())
  85. ON CONFLICT
  86. DO NOTHING;
  87. END IF;
  88. -- If not first event, set the project first event
  89. IF _first_event IS NULL THEN
  90. UPDATE
  91. projects_project
  92. SET
  93. first_event = now()
  94. WHERE
  95. id = _project_id;
  96. END IF;
  97. END IF;
  98. END;
  99. $$
  100. LANGUAGE plpgsql;