diff --git a/engine/apps/grafana_plugin/apps.py b/engine/apps/grafana_plugin/apps.py index 3d76dfc0..995e6fd4 100644 --- a/engine/apps/grafana_plugin/apps.py +++ b/engine/apps/grafana_plugin/apps.py @@ -3,6 +3,7 @@ import sys from django.apps import AppConfig, apps from django.conf import settings +from django.db import OperationalError logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) @@ -25,13 +26,16 @@ class GrafanaPluginConfig(AppConfig): # TODO: this logic should probably be moved out to a common utility is_not_migration_script = any(startup_command in sys.argv for startup_command in STARTUP_COMMANDS) if is_not_migration_script and settings.IS_OPEN_SOURCE: - Organization = apps.get_model("user_management", "Organization") - has_existing_org = Organization.objects.first() is not None + try: + Organization = apps.get_model("user_management", "Organization") + has_existing_org = Organization.objects.first() is not None - # only enforce the following for new setups - if no organization exists in the database - # and the GRAFANA_API_URL env var is not specified, exit the application - if has_existing_org is False and settings.SELF_HOSTED_SETTINGS["GRAFANA_API_URL"] is None: - logger.error( - f"For OSS installations, GRAFANA_API_URL is a required environment variable. Please set it and restart the application." - ) - sys.exit() + # only enforce the following for new setups - if no organization exists in the database + # and the GRAFANA_API_URL env var is not specified, exit the application + if has_existing_org is False and settings.SELF_HOSTED_SETTINGS["GRAFANA_API_URL"] is None: + logger.error( + f"For OSS installations, GRAFANA_API_URL is a required environment variable. Please set it and restart the application." + ) + sys.exit() + except OperationalError: + pass diff --git a/engine/apps/integrations/mixins/alert_channel_defining_mixin.py b/engine/apps/integrations/mixins/alert_channel_defining_mixin.py index cebcee5e..fd6ac239 100644 --- a/engine/apps/integrations/mixins/alert_channel_defining_mixin.py +++ b/engine/apps/integrations/mixins/alert_channel_defining_mixin.py @@ -48,6 +48,7 @@ class AlertChannelDefiningMixin(object): if cache.get(self.CACHE_DB_FALLBACK_OBSOLETE_KEY) is None: cache.set(self.CACHE_DB_FALLBACK_OBSOLETE_KEY, True, self.CACHE_DB_FALLBACK_REFRESH_INTERVAL) self.update_alert_receive_channel_cache() + except AlertReceiveChannel.DoesNotExist: raise PermissionDenied("Integration key was not found. Permission denied.") except OperationalError: @@ -65,13 +66,13 @@ class AlertChannelDefiningMixin(object): else: logger.info("Cache is empty!") raise - - if alert_receive_channel.organization.is_moved: - raise OrganizationMovedException(alert_receive_channel.organization) - if alert_receive_channel.organization.deleted_at: - # It's better to raise OrganizarionDeletedException, but in legacy code PermissionDenied is returned when integration key not found. - # So, keep it consistent. - raise PermissionDenied("Integration key was not found. Permission denied.") + else: + if alert_receive_channel.organization.is_moved: + raise OrganizationMovedException(alert_receive_channel.organization) + if alert_receive_channel.organization.deleted_at: + # It's better to raise OrganizarionDeletedException, but in legacy code PermissionDenied is returned when integration key not found. + # So, keep it consistent. + raise PermissionDenied("Integration key was not found. Permission denied.") del kwargs["alert_channel_key"] kwargs["alert_receive_channel"] = alert_receive_channel