Browse Source

feat(discover-saved-query-worldmap) Added migration to convert worldmap to total period display (#53276)

For issue: https://github.com/getsentry/sentry/issues/51315
Steps outlined:
[here](https://www.notion.so/sentry/Removing-World-Map-display-from-Discover-and-Dashboards-e49eb198fe294ad0a1b4c47b9c3619c0?pvs=4#43e2c318d1eb4f93987718b82307f56f).

- Won't be merging until, frontend
[PR](https://github.com/getsentry/sentry/pull/53185) removing worldmap
display option from discover is deployed. Therefore, there won't be a
time gap.
- There are only 28 discover saved queries using the worldmap display:
[query](https://redash.getsentry.net/queries/4484)
- Migration converts worldmap display mode marked by `'display':
'worldmap'` in the query field of the `DiscoverSavedQuery` model to
`'display':'default'` which represents the total period display mode.

---------

Co-authored-by: Abdullah Khan <abdullahkhan@PG9Y57YDXQ.local>
Abdkhan14 1 year ago
parent
commit
43ea505fc0

+ 1 - 1
migrations_lockfile.txt

@@ -7,5 +7,5 @@ will then be regenerated, and you should be able to merge without conflicts.
 
 
 nodestore: 0002_nodestore_no_dictfield
 nodestore: 0002_nodestore_no_dictfield
 replays: 0003_add_size_to_recording_segment
 replays: 0003_add_size_to_recording_segment
-sentry: 0521_migrate_world_map_widgets
+sentry: 0522_migrate_discover_savedquery_worldmaps
 social_auth: 0002_default_auto_field
 social_auth: 0002_default_auto_field

+ 40 - 0
src/sentry/migrations/0522_migrate_discover_savedquery_worldmaps.py

@@ -0,0 +1,40 @@
+# Generated by Django 3.2.20 on 2023-07-25 20:01
+
+from django.db import migrations
+
+from sentry.discover.models import DiscoverSavedQuery
+from sentry.new_migrations.migrations import CheckedMigration
+
+
+def migrate_savedquery_worldmap_display_to_totalPeriod(apps, schema_editor):
+    savedQueries = DiscoverSavedQuery.objects.filter(query__contains={"display": "worldmap"})
+
+    for savedQuery in savedQueries:
+        savedQuery.query["display"] = "default"
+        savedQuery.save()
+
+
+class Migration(CheckedMigration):
+    # This flag is used to mark that a migration shouldn't be automatically run in production. For
+    # the most part, this should only be used for operations where it's safe to run the migration
+    # after your code has deployed. So this should not be used for most operations that alter the
+    # schema of a table.
+    # Here are some things that make sense to mark as dangerous:
+    # - Large data migrations. Typically we want these to be run manually by ops so that they can
+    #   be monitored and not block the deploy for a long period of time while they run.
+    # - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to
+    #   have ops run this and not block the deploy. Note that while adding an index is a schema
+    #   change, it's completely safe to run the operation after the code has deployed.
+    is_dangerous = False
+
+    dependencies = [
+        ("sentry", "0521_migrate_world_map_widgets"),
+    ]
+
+    operations = [
+        migrations.RunPython(
+            migrate_savedquery_worldmap_display_to_totalPeriod,
+            migrations.RunPython.noop,
+            hints={"tables": ["sentry_discoversavedquery"]},
+        ),
+    ]

+ 49 - 0
tests/snuba/api/endpoints/test_discover_saved_queries.py

@@ -710,3 +710,52 @@ class DiscoverSavedQueriesVersion2Test(DiscoverSavedQueryBase):
                 },
                 },
             )
             )
         assert response.status_code == 400, response.content
         assert response.status_code == 400, response.content
+
+    def test_filters_queries_with_worldmap_displays(self):
+        query = {
+            "fields": ["geo.country_code"],
+            "query": "has:geo.country_code",
+            "limit": 10,
+            "display": "worldmap",
+        }
+        DiscoverSavedQuery.objects.create(
+            organization=self.org,
+            created_by_id=self.user.id,
+            name="Test world map1",
+            query=query,
+            version=1,
+        )
+        query = {
+            "fields": ["geo.country_code"],
+            "display": "worldmap",
+            "query": "has:geo.country_code",
+            "limit": 10,
+        }
+        DiscoverSavedQuery.objects.create(
+            organization=self.org,
+            created_by_id=self.user.id,
+            name="Test world map2",
+            query=query,
+            version=1,
+        )
+        query = {
+            "fields": ["geo.country_code"],
+            "query": "has:geo.country_code",
+            "limit": 10,
+            "display": "default",
+        }
+        DiscoverSavedQuery.objects.create(
+            organization=self.org,
+            created_by_id=self.user.id,
+            name="Test default",
+            query=query,
+            version=1,
+        )
+
+        queries = DiscoverSavedQuery.objects.filter(query__contains={"display": "worldmap"})
+
+        assert len(queries) == 2
+        assert queries[0].name == "Test world map1"
+        assert queries[0].query["display"] == "worldmap"
+        assert queries[1].name == "Test world map2"
+        assert queries[1].query["display"] == "worldmap"