Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dbconn.Global #28251

Merged
merged 11 commits into from Nov 29, 2021
Merged

Remove dbconn.Global #28251

merged 11 commits into from Nov 29, 2021

Conversation

camdencheek
Copy link
Member

@camdencheek camdencheek commented Nov 28, 2021

The time has come.

Each commit has a message describing what it is doing, but the gist of this PR is:

  • Pass in a db anywhere dbconn.Global is currently being used
  • Stop setting dbconn.Global during initialization
  • Remove dbconn.Global

@cla-bot cla-bot bot added the cla-signed label Nov 28, 2021
@camdencheek camdencheek changed the title Cc/remove dbconn global Remove dbconn.Global Nov 28, 2021
@camdencheek camdencheek force-pushed the cc/remove-dbconn-global branch 2 times, most recently from 94c1589 to 775828e Compare November 28, 2021 16:19
This removes dbconn.Global references from the codeintel GraphQL
resolvers by passing down a database handle through parent resolvers.
This inlines checkCurrentUserIsSiteAdmin because its only purpose was to
call backend.CheckCurrentUserIsSiteAdmin with the global db connection.
Instead, this inlines the function and passes in a db handle directly.
This replaces references to dbconn.Global in GetOrAssignUserCustomerID
with a passed-in database handle. It also modifies it to rely on
`tx.Done` for its error handling.
Now that we are passing a database.DB into GetOrAssignUserCustomerID,
we can stop wrapping a `dbutil.DB` with `database.NewDB`.
This makes InitDB not set `dbconn.Global`, instead keeping the
connection in a local variable and returning it.
Now that we don't reference dbconn.Global anywhere in the resolvers, we
can get rid of this safety check.
This stops the worker db init function from setting dbconn.Global,
instead creating and returning the connection the caller.
This stops the precise-code-intel-worker from initializing
dbconn.Global. It creates the connection and returns it instead.
There is no need to set dbconn.Global anymore since it's not referenced.
The dbtesting package previously would set `dbconn.Global`, then return
it from `GetDB`. Instead, this creates a global variable just for the
`dbtesting` package so it will continue to reuse the same connection
across all calls to `GetDB` (no change in behavior), except it will no
longer set `dbconn.Global`. This package is deprecated, and will soon be
removed, so I didn't feel the need to refactor this further.
The time has come.
@camdencheek camdencheek marked this pull request as ready for review November 28, 2021 16:43
@sourcegraph-bot
Copy link
Contributor

Notifying subscribers in CODENOTIFY files for diff 2ada491...ed9c82f.

Notify File(s)
@beyang enterprise/cmd/precise-code-intel-worker/main.go
@efritz cmd/worker/workerdb/db.go
enterprise/cmd/frontend/internal/codeintel/resolvers/graphql/configuration_connection.go
enterprise/cmd/frontend/internal/codeintel/resolvers/graphql/configuration_policy.go
enterprise/cmd/frontend/internal/codeintel/resolvers/graphql/index.go
enterprise/cmd/frontend/internal/codeintel/resolvers/graphql/index_connection.go
enterprise/cmd/frontend/internal/codeintel/resolvers/graphql/index_steps.go
enterprise/cmd/frontend/internal/codeintel/resolvers/graphql/index_steps_index.go
enterprise/cmd/frontend/internal/codeintel/resolvers/graphql/index_steps_preindex.go
enterprise/cmd/frontend/internal/codeintel/resolvers/graphql/resolver.go
enterprise/cmd/frontend/internal/codeintel/resolvers/graphql/resolver_test.go
enterprise/cmd/frontend/internal/codeintel/resolvers/graphql/upload.go
enterprise/cmd/frontend/internal/codeintel/resolvers/graphql/upload_connection.go
enterprise/cmd/precise-code-intel-worker/main.go
internal/database/dbconn/global.go
internal/database/dbtesting/dbtesting.go
@indradhanush enterprise/cmd/repo-updater/main.go
@unknwon enterprise/cmd/repo-updater/main.go

connectOnce sync.Once
connectErr error
connectOnce sync.Once
globalTestDB *sql.DB
Copy link
Member

Choose a reason for hiding this comment

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

Any reason why this is still global?

Copy link
Member Author

Choose a reason for hiding this comment

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

To preserve the behavior of GetDB. All calls to GetDB for a process will return the same handle right now, and not doing that might break some tests. I'm currently working on removing the dbtesting package entirely, so I didn't bother trying to make that behavior better

Copy link
Member Author

Choose a reason for hiding this comment

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

The important part here is that globalTestDB is only accessible through GetDB, so it can't be referenced directly by other packages. So, when we replace GetDB, we can get rid of this.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense! Thanks for the explanation 👍

Comment on lines -64 to -65
// TODO(jchen): This is an unfortunate compromise to not rewrite ossDB.ExternalServices for now.
dbconn.Global = db
Copy link
Member

Choose a reason for hiding this comment

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

Finally!

@camdencheek camdencheek merged commit 595877f into main Nov 29, 2021
@camdencheek camdencheek deleted the cc/remove-dbconn-global branch November 29, 2021 15:07
@ryanslade
Copy link
Contributor

Nice!! Thanks you so much for driving this to completion!

@asdine
Copy link
Contributor

asdine commented Nov 29, 2021

Thanks! I can't believe we finally did it!

@camdencheek camdencheek mentioned this pull request Nov 29, 2021
10 tasks
@bobheadxi bobheadxi added dx Issues and PRs related to developer experience concerns dx-announce Tag PRs with this label to include it in the monthly DX email update labels Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed dx Issues and PRs related to developer experience concerns dx-announce Tag PRs with this label to include it in the monthly DX email update
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants