diff --git a/src/sentry/dashboards/endpoints/organization_dashboards.py b/src/sentry/dashboards/endpoints/organization_dashboards.py index 36b0b6ff01c299..5cf436fc2f358b 100644 --- a/src/sentry/dashboards/endpoints/organization_dashboards.py +++ b/src/sentry/dashboards/endpoints/organization_dashboards.py @@ -100,7 +100,8 @@ class PrebuiltDashboard(TypedDict): def sync_prebuilt_dashboards(organization: Organization) -> None: """ Queries the database to check if prebuilt dashboards have a Dashboard record and - creates them if they don't, or deletes them if they should no longer exist. + creates them if they don't, updates titles if they've changed, or deletes them + if they should no longer exist. """ with transaction.atomic(router.db_for_write(Dashboard)): @@ -109,15 +110,14 @@ def sync_prebuilt_dashboards(organization: Organization) -> None: prebuilt_id__isnull=False, ) - saved_prebuilt_dashboard_ids = set( - saved_prebuilt_dashboards.values_list("prebuilt_id", flat=True) - ) + saved_prebuilt_dashboard_map = {d.prebuilt_id: d for d in saved_prebuilt_dashboards} - # Create prebuilt dashboards if they don't exist + # Create prebuilt dashboards if they don't exist, or update titles if changed + dashboards_to_update: list[Dashboard] = [] for prebuilt_dashboard in PREBUILT_DASHBOARDS: prebuilt_id: PrebuiltDashboardId = prebuilt_dashboard["prebuilt_id"] - if prebuilt_id not in saved_prebuilt_dashboard_ids: + if prebuilt_id not in saved_prebuilt_dashboard_map: # Create new dashboard Dashboard.objects.create( organization=organization, @@ -125,6 +125,13 @@ def sync_prebuilt_dashboards(organization: Organization) -> None: created_by_id=None, prebuilt_id=prebuilt_id, ) + elif saved_prebuilt_dashboard_map[prebuilt_id].title != prebuilt_dashboard["title"]: + # Update title if changed + saved_prebuilt_dashboard_map[prebuilt_id].title = prebuilt_dashboard["title"] + dashboards_to_update.append(saved_prebuilt_dashboard_map[prebuilt_id]) + + if dashboards_to_update: + Dashboard.objects.bulk_update(dashboards_to_update, ["title"]) # Delete old prebuilt dashboards if they should no longer exist prebuilt_ids = [d["prebuilt_id"] for d in PREBUILT_DASHBOARDS]