Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

search contexts: remove autodefined contexts from frontend, add to normal query#44875

Merged
limitedmage merged 7 commits into
jp/contextsstarreddefaultfrom
jp/rehashautodefinedcontexts
Nov 30, 2022
Merged

search contexts: remove autodefined contexts from frontend, add to normal query#44875
limitedmage merged 7 commits into
jp/contextsstarreddefaultfrom
jp/rehashautodefinedcontexts

Conversation

@limitedmage

@limitedmage limitedmage commented Nov 28, 2022

Copy link
Copy Markdown
Contributor

Stacked on #44624

This removes autodefined contexts from the frontend and deprecates the GraphQL query for AutoDefinedSearchContexts. Instead, autodefined contexts (only global for now, since other autodefined contexts are now unused) will now be returned by the DB query like all the other contexts. However, the global context 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.

@cla-bot cla-bot Bot added the cla-signed label Nov 28, 2022
@limitedmage limitedmage requested review from a team, camdencheek and novoselrok November 28, 2022 23:20
@sourcegraph-bot

sourcegraph-bot commented Nov 28, 2022

Copy link
Copy Markdown
Contributor

Codenotify: Notifying subscribers in CODENOTIFY files for diff 742004a...c17b1ca.

Notify File(s)
@beyang internal/search/searchcontexts/search_contexts.go
internal/search/searchcontexts/search_contexts_test.go
@camdencheek internal/search/searchcontexts/search_contexts.go
internal/search/searchcontexts/search_contexts_test.go
@fkling client/search-ui/src/input/SearchBox.story.tsx
client/search-ui/src/input/SearchContextDropdown.test.tsx
client/search-ui/src/input/SearchContextDropdown.tsx
client/search-ui/src/input/SearchContextMenu.story.tsx
client/search-ui/src/input/SearchContextMenu.test.tsx
client/search-ui/src/input/SearchContextMenu.tsx
client/search-ui/src/input/snapshots/SearchContextMenu.test.tsx.snap
client/search/src/backend.ts
client/search/src/index.ts
client/web/src/search/home/SearchPage.story.tsx
client/web/src/search/home/SearchPage.test.tsx
@keegancsmith internal/search/searchcontexts/search_contexts.go
internal/search/searchcontexts/search_contexts_test.go
@philipp-spiess client/jetbrains/webview/src/search/App.tsx
client/jetbrains/webview/src/search/input/JetBrainsSearchBox.story.tsx
client/jetbrains/webview/src/search/input/JetBrainsSearchBox.tsx
@vdavid client/jetbrains/webview/src/search/App.tsx
client/jetbrains/webview/src/search/input/JetBrainsSearchBox.story.tsx
client/jetbrains/webview/src/search/input/JetBrainsSearchBox.tsx

@sg-e2e-regression-test-bob

sg-e2e-regression-test-bob commented Nov 28, 2022

Copy link
Copy Markdown

Bundle size report 📦

Initial size Total size Async size Modules
-0.03% (-0.91 kb) -0.01% (-0.95 kb) -0.00% (-0.03 kb) 0.00% (0)

Look at the Statoscope report for a full comparison between the commits c17b1ca and d61f40b or learn more.

Open explanation
  • Initial size is the size of the initial bundle (the one that is loaded when you open the page)
  • Total size is the size of the initial bundle + all the async loaded chunks
  • Async size is the size of all the async loaded chunks
  • Modules is the number of modules in the initial bundle

query,
autodefined
) VALUES (
0, -- IDs start at 1 so 0 is reserved for the global context

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"....

@camdencheek camdencheek Nov 29, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK how about this:

  • global always appears at the top of the list regardless of order. global cannot 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 orderBy param
  • Any remaining contexts appear after that, sorted by the orderBy param

WDYT @quinnkeast?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds perfect 👍 Thanks for hashing it out!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me!

@limitedmage

Copy link
Copy Markdown
Contributor Author

@camdencheek @novoselrok This is ready for review again (only backend changes have been made). Please take a look!

@camdencheek camdencheek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small comment inline, but the backend side of things LGTM

Comment thread internal/database/search_contexts.go Outdated
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 (

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things:

  1. 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.
  2. By explicitly selecting the columns, you can skip namespace_name, which has the awkwardness of being ignored by the row scanner on line 484.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can order by a field that you don't select. You will probably have to refer to it as t.namespace_name though

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it worked! Thanks!!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@limitedmage limitedmage merged commit dc9d0c4 into jp/contextsstarreddefault Nov 30, 2022
@limitedmage limitedmage deleted the jp/rehashautodefinedcontexts branch November 30, 2022 18:07
limitedmage added a commit that referenced this pull request Dec 5, 2022
* 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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants