Repository: getsentry/sentry
Pull request: ref(pinned-searches): Save new/updated default searches to new `groupsearchview` table
This PR augments the `.../pinned-searches` endpoint. This endpoint previously saved a new pinned (default) search to only the `sentry_savedsearches` table with `visibility="owner_pinned"`. This PR adds new logic to also save a new or updated default to the upcoming custom views table (`sentry_issueuserviews`). This is because we will want to persist user's default searches as a new tab, so we will need to start actively saving default searches to this new table. 

Problem statement:
Custom Views: Augment the `organizations/.../pinned-searches/` endpoint to save to/update new custom views database
We will need to update the `organizations/.../pinned-searches/` endpoint to also save and update entries in the new database for custom views. This is because pinned searches are being replaced by custom views, so before we migrate existing pinned searches to the custom views database, we will need to update the endpoint that currently saves pinned searches to double save to the new database as well.

Hints from issue discussion:

Commit message: Update tests

Patch to review:
diff --git a/src/sentry/api/endpoints/organization_pinned_searches.py b/src/sentry/api/endpoints/organization_pinned_searches.py
index 637d6f9273e909..11d2b981579349 100644
--- a/src/sentry/api/endpoints/organization_pinned_searches.py
+++ b/src/sentry/api/endpoints/organization_pinned_searches.py
@@ -7,6 +7,7 @@
 from sentry.api.base import region_silo_endpoint
 from sentry.api.bases.organization import OrganizationEndpoint, OrganizationPinnedSearchPermission
 from sentry.api.serializers import serialize
+from sentry.models.groupsearchview import GroupSearchView
 from sentry.models.savedsearch import SavedSearch, SortOptions, Visibility
 from sentry.models.search_common import SearchType
 
@@ -52,6 +53,17 @@ def put(self, request: Request, organization) -> Response:
             visibility=Visibility.OWNER_PINNED,
             values={"query": result["query"], "sort": result["sort"]},
         )
+
+        GroupSearchView.objects.create_or_update(
+            organizaiton=organization,
+            user_id=request.user.id,
+            values={
+                "name": "Default Search",
+                "query": result["query"],
+                "query_sort": result["sort"],
+                "position": 0,  # 0 position indicates default view
+            },
+        )
         pinned_search = SavedSearch.objects.get(
             organization=organization,
             owner_id=request.user.id,
diff --git a/tests/sentry/api/endpoints/test_organization_pinned_searches.py b/tests/sentry/api/endpoints/test_organization_pinned_searches.py
index 0f88c56b029dca..66445f76c5186b 100644
--- a/tests/sentry/api/endpoints/test_organization_pinned_searches.py
+++ b/tests/sentry/api/endpoints/test_organization_pinned_searches.py
@@ -1,6 +1,7 @@
 from functools import cached_property
 
 from sentry.api.endpoints.organization_pinned_searches import PINNED_SEARCH_NAME
+from sentry.models.groupsearchview import GroupSearchView
 from sentry.models.savedsearch import SavedSearch, SortOptions, Visibility
 from sentry.models.search_common import SearchType
 from sentry.testutils.cases import APITestCase
@@ -35,6 +36,16 @@ def test(self):
             visibility=Visibility.OWNER_PINNED,
         ).exists()
 
+        # TODO(msun): Remove this when custom views are GA'd
+        assert GroupSearchView.objects.filter(
+            organization=self.organization,
+            name="Default Search",
+            user_id=self.member.id,
+            query=query,
+            query_sort=sort,
+            position=0,
+        ).exists()
+
         query = "test_2"
         self.get_success_response(type=search_type, query=query, sort=sort, status_code=201)
         assert SavedSearch.objects.filter(


Code review comments:
diff_hunk: @@ -52,6 +53,17 @@ def put(self, request: Request, organization) -> Response:
             visibility=Visibility.OWNER_PINNED,
             values={"query": result["query"], "sort": result["sort"]},
         )
+
+        GroupSearchView.objects.create_or_update(
+            organizaiton=organization,
path: src/sentry/api/endpoints/organization_pinned_searches.py
text: @user1:
`organization`

@user2:
Let's make sure there is a test in place which would catch this, if there isn't already

@author:
🤦 
Thanks for catching this! Fixing now

diff_hunk: @@ -52,6 +53,17 @@ def put(self, request: Request, organization) -> Response:
             visibility=Visibility.OWNER_PINNED,
             values={"query": result["query"], "sort": result["sort"]},
         )
+
+        GroupSearchView.objects.create_or_update(
+            organizaiton=organization,
+            user_id=request.user.id,
+            values={
+                "name": "Default Search",
+                "query": result["query"],
+                "query_sort": result["sort"],
+                "position": 0,  # 0 position indicates default view
path: src/sentry/api/endpoints/organization_pinned_searches.py
text: @user1:
I think the position should be part of the select criteria, since the unique is `org, user, position`