search contexts: remove autodefined contexts from frontend, add to normal query#44875
Conversation
|
Codenotify: Notifying subscribers in CODENOTIFY files for diff 742004a...c17b1ca.
|
Bundle size report 📦
Look at the Statoscope report for a full comparison between the commits c17b1ca and d61f40b or learn more. Open explanation
|
| query, | ||
| autodefined | ||
| ) VALUES ( | ||
| 0, -- IDs start at 1 so 0 is reserved for the global context |
There was a problem hiding this comment.
I'm not so sure this is a good idea. We usually reserve ID: 0 to indicate that an entity does not exist. This change would break that assumption. I'll let @camdencheek confirm that.
Since auto-defined contexts are not dynamic anymore, could we hardcode the single global context here: https://github.com/sourcegraph/sourcegraph/pull/44875/files#diff-2e0375121e614c40c04d23b1b05fc1a53d8281d83f0b02babdfc699e32098a31L164
That way, we can eliminate the entire concept of auto-defined contexts, and we treat global as a single special case.
There was a problem hiding this comment.
IMO returning the global context from the backend makes the most sense, instead of doing this in the front-end. This would ensure the global context is always included in all cases including pagination, sorting, etc. The easiest way to do this is by adding it to the database. This also ensures that if users set their default context to "global" explicitly, or star the global context, there isn't a violation of foreign keys; otherwise, the constraints probably can't be handled on the DB side from what I understand. What alternative would you propose here?
There was a problem hiding this comment.
(I was initially thinking of using the auto-incrementing ID here, but elsewhere in the backend we were explicitly checking if the ID is 0 for auto-defined contexts, so I thought this would simplify it and be ok; I can go back to the autoincrementing implementation).
There was a problem hiding this comment.
I forgot about starring and defaulting to the global context, which complicates things.
We could use an auto-incremented ID for auto-defined contexts since you're already changing the check from "ID == 0" to "Autodefined == true". We would also have to prevent side admins from being able to edit auto-defined contexts.
Additionally, the global context won't be listed at the top anymore in the search context menu, right?
There was a problem hiding this comment.
We usually reserve ID: 0 to indicate that an entity does not exist.
I don't think that really applies to DB interactions. In general, if a field is optional, it should be represented as a nullable type.
I'm struggling to decide whether or not the global context is actually a context, or whether it's the absence of a context. I think this matters for the implementation here. In practice, context:global means "ignore me." If the "global context" is not a context, and instead represents "no context", then it would feel weird to put it in the database because it's application logic rather than data. If global context is a context, and we add a context to every search query, then it seems like it should go in the database.
It does feel a little weird to insert data in a migration since the existence of the global context is implied. Especially since we depend on the ID being zero in some places, I'd feel better about it being added to the list at query time rather than inserting it into the db. We could even just create it in the query with something like UNION SELECT 0, "global", "All repositories on Sourcegraph"....
There was a problem hiding this comment.
I also think it would be weird for the global context to not be always towards the top of the list. Probably even above starred contexts. I wouldn't want the global context to be paged out because a user starred 10 contexts or something
There was a problem hiding this comment.
OK how about this:
globalalways appears at the top of the list regardless of order.globalcannot be starred. Setting global as default is done by removing the default for the user; it will show as default if the user doesn't have another default.- If an explicit default is set, it appears second in the list
- If any contexts are starred, they appear after these, sorted by the
orderByparam - Any remaining contexts appear after that, sorted by the
orderByparam
WDYT @quinnkeast?
There was a problem hiding this comment.
That sounds perfect 👍 Thanks for hashing it out!
There was a problem hiding this comment.
I've updated this PR to remove this insertion in the migration, and instead use a dummy row with UNION ALL. I'll update the sorting in #44876
|
@camdencheek @novoselrok This is ready for review again (only backend changes have been made). Please take a look! |
camdencheek
left a comment
There was a problem hiding this comment.
One small comment inline, but the backend side of things LGTM
| FROM search_contexts sc | ||
| LEFT JOIN users u on sc.namespace_user_id = u.id | ||
| LEFT JOIN orgs o on sc.namespace_org_id = o.id | ||
| SELECT * FROM ( |
There was a problem hiding this comment.
Two things:
- IMO,
SELECT *should generally be avoided. It makes it difficult to match up the number and order of the resulting columns with the destination variables in go. And the few extra lines is worth the avoided headaches later. - By explicitly selecting the columns, you can skip
namespace_name, which has the awkwardness of being ignored by the row scanner on line 484.
There was a problem hiding this comment.
I think namespace_name would still be required for the ORDER BY clause. I had to add it because apparently you can't use COALESCE() or other functions in a UNION. (See new line 191)
There was a problem hiding this comment.
You can order by a field that you don't select. You will probably have to refer to it as t.namespace_name though
There was a problem hiding this comment.
Yes, it worked! Thanks!!
There was a problem hiding this comment.
I'll make this change on the ordering PR (#44876) to avoid merge conflicts.
…then others (#44876) * search contexts: order contexts list by default first, then starred, then others * generate mocks * Add test for new order * explicit select and remove unused scan var * simplify getting authenticated user * fix go lint
* db schema and graphql schema updates * default contexts backend * query for stars * backend for settings stars and defaults * sg generate * fixes? * add foreign keys * idempotent constraints * again * search contexts: remove autodefined contexts from frontend, add to normal query (#44875) * search contexts: remove autodefined contexts from frontend, add to normal query * generate * remove real global context, use union with dummy row instead * fix some tests * fix CountSearchContexts * add test for CountSearchContexts * search contexts: order contexts list by default first, then starred, then others (#44876) * search contexts: order contexts list by default first, then starred, then others * generate mocks * Add test for new order * explicit select and remove unused scan var * simplify getting authenticated user * fix go lint * unit test for default contexts * tests for starred contexts * change graphql api to take user as param, add tests for this * update db schema to make contraints prettier * clean up db keys * search contexts: update header in list page (#44966) * search contexts: update header * add back tab * fix spacing * fix action button width * search contexts: table view in management page (#45001) * search contexts: update header * search contexts: table view in management page * add menu * fix build failure * add back tab * fix spacing * fix action button width * address minor issues from figma * search contexts: card view for mobile (#45040) * search contexts: card view for mobile * fix build failure * give up on sr-only mixin
Stacked on #44624
This removes autodefined contexts from the frontend and deprecates the GraphQL query for
AutoDefinedSearchContexts. Instead, autodefined contexts (onlyglobalfor now, since other autodefined contexts are now unused) will now be returned by the DB query like all the other contexts. However, theglobalcontext won't actually be present in the DB; it will be inserted as a dummy row in the query.Test plan
Verify all existing context tests now pass. Verify DB migration correctly adds/removed the global context from the database.