diff --git a/cmd/syntactic-code-intel-worker/shared/indexing_worker_test.go b/cmd/syntactic-code-intel-worker/shared/indexing_worker_test.go index 58eb7bf6e2c..fd2fc75f1c1 100644 --- a/cmd/syntactic-code-intel-worker/shared/indexing_worker_test.go +++ b/cmd/syntactic-code-intel-worker/shared/indexing_worker_test.go @@ -9,13 +9,13 @@ import ( "os" "testing" - "github.com/sourcegraph/log/logtest" - "github.com/stretchr/testify/require" - "github.com/bazelbuild/rules_go/go/runfiles" + "github.com/sourcegraph/log/logtest" "github.com/sourcegraph/scip/bindings/go/scip" + "github.com/stretchr/testify/require" + "google.golang.org/protobuf/proto" + codeintelshared "github.com/sourcegraph/sourcegraph/internal/codeintel/shared" - stores "github.com/sourcegraph/sourcegraph/internal/codeintel/shared" "github.com/sourcegraph/sourcegraph/internal/codeintel/syntactic_indexing/jobstore" testutils "github.com/sourcegraph/sourcegraph/internal/codeintel/syntactic_indexing/testkit" "github.com/sourcegraph/sourcegraph/internal/codeintel/uploads" @@ -24,9 +24,7 @@ import ( "github.com/sourcegraph/sourcegraph/internal/database/dbtest" "github.com/sourcegraph/sourcegraph/internal/gitserver" "github.com/sourcegraph/sourcegraph/internal/observation" - "github.com/sourcegraph/sourcegraph/lib/iterator" - "google.golang.org/protobuf/proto" ) func TestIndexingWorker(t *testing.T) { @@ -54,7 +52,7 @@ func TestIndexingWorker(t *testing.T) { config := IndexingWorkerConfig{} - jobStore, err := jobstore.NewStoreWithDB(observationCtx, sqlDB) + jobStore, err := jobstore.NewStoreWithDB(observationCtx, db) require.NoError(t, err) gitserverClient := gitserver.NewMockClient() @@ -78,7 +76,7 @@ func TestIndexingWorker(t *testing.T) { jobStore, config, db, - stores.NewCodeIntelDB(logger, sqlDB), + codeintelDB, gitserverClient, uploadStore, ) diff --git a/cmd/syntactic-code-intel-worker/shared/shared.go b/cmd/syntactic-code-intel-worker/shared/shared.go index 55c375e0dd0..857ea2b0065 100644 --- a/cmd/syntactic-code-intel-worker/shared/shared.go +++ b/cmd/syntactic-code-intel-worker/shared/shared.go @@ -36,13 +36,12 @@ func Main(ctx context.Context, observationCtx *observation.Context, ready servic name := "syntactic-codeintel-worker" - frontendSqlDB, err := initDB(observationCtx, name) + db, err := initDB(observationCtx, name) if err != nil { return errors.Wrap(err, "initializing frontend db") } - db := database.NewDB(logger, frontendSqlDB) - jobStore, err := jobstore.NewStoreWithDB(observationCtx, frontendSqlDB) + jobStore, err := jobstore.NewStoreWithDB(observationCtx, db) if err != nil { return errors.Wrap(err, "initializing worker store") } @@ -92,7 +91,7 @@ func initCodeintelDB(observationCtx *observation.Context, name string) (*sql.DB, return sqlDB, nil } -func initDB(observationCtx *observation.Context, name string) (*sql.DB, error) { +func initDB(observationCtx *observation.Context, name string) (database.DB, error) { dsn := conf.GetServiceConnectionValueAndRestartOnChange(func(serviceConnections conftypes.ServiceConnections) string { return serviceConnections.PostgresDSN }) @@ -104,5 +103,5 @@ func initDB(observationCtx *observation.Context, name string) (*sql.DB, error) { return nil, err } - return sqlDB, nil + return database.NewDB(observationCtx.Logger, sqlDB), nil } diff --git a/cmd/worker/shared/init/codeintel/db.go b/cmd/worker/shared/init/codeintel/db.go index fa357e5cb53..8f2fcb4874b 100644 --- a/cmd/worker/shared/init/codeintel/db.go +++ b/cmd/worker/shared/init/codeintel/db.go @@ -12,13 +12,8 @@ import ( "github.com/sourcegraph/sourcegraph/lib/errors" ) -// InitRawDB initializes and returns a connection to the codeintel db. -func InitRawDB(observationCtx *observation.Context) (*sql.DB, error) { - return initDBMemo.Init(observationCtx) -} - func InitDB(observationCtx *observation.Context) (codeintelshared.CodeIntelDB, error) { - rawDB, err := InitRawDB(observationCtx) + rawDB, err := initDBMemo.Init(observationCtx) if err != nil { return nil, err } diff --git a/cmd/worker/shared/init/db/db.go b/cmd/worker/shared/init/db/db.go index 5081ea0c613..fb1484ab59c 100644 --- a/cmd/worker/shared/init/db/db.go +++ b/cmd/worker/shared/init/db/db.go @@ -13,7 +13,7 @@ import ( ) func InitDB(observationCtx *observation.Context) (database.DB, error) { - rawDB, err := InitRawDB(observationCtx) + rawDB, err := initDatabaseMemo.Init(observationCtx) if err != nil { return nil, err } @@ -21,10 +21,6 @@ func InitDB(observationCtx *observation.Context) (database.DB, error) { return database.NewDB(observationCtx.Logger, rawDB), nil } -func InitRawDB(observationCtx *observation.Context) (*sql.DB, error) { - return initDatabaseMemo.Init(observationCtx) -} - var initDatabaseMemo = memo.NewMemoizedConstructorWithArg(func(observationCtx *observation.Context) (*sql.DB, error) { dsn := conf.GetServiceConnectionValueAndRestartOnChange(func(serviceConnections conftypes.ServiceConnections) string { return serviceConnections.PostgresDSN diff --git a/internal/codeintel/syntactic_indexing/enqueuer_test.go b/internal/codeintel/syntactic_indexing/enqueuer_test.go index 95ca0038653..3155397ad40 100644 --- a/internal/codeintel/syntactic_indexing/enqueuer_test.go +++ b/internal/codeintel/syntactic_indexing/enqueuer_test.go @@ -22,11 +22,10 @@ func TestSyntacticIndexingEnqueuer(t *testing.T) { are valid from the point of view of the DB worker interface */ observationCtx := observation.TestContextTB(t) - sqlDB := dbtest.NewDB(t) - db := database.NewDB(observationCtx.Logger, sqlDB) + db := database.NewDB(observationCtx.Logger, dbtest.NewDB(t)) ctx := context.Background() - jobStore, err := jobstore.NewStoreWithDB(observationCtx, sqlDB) + jobStore, err := jobstore.NewStoreWithDB(observationCtx, db) require.NoError(t, err, "unexpected error creating dbworker stores") repoSchedulingStore := reposcheduler.NewSyntacticStore(observationCtx, db) diff --git a/internal/codeintel/syntactic_indexing/jobstore/store.go b/internal/codeintel/syntactic_indexing/jobstore/store.go index 147c9d7e353..ad2b2f8eecb 100644 --- a/internal/codeintel/syntactic_indexing/jobstore/store.go +++ b/internal/codeintel/syntactic_indexing/jobstore/store.go @@ -2,7 +2,6 @@ package jobstore import ( "context" - "database/sql" "github.com/keegancsmith/sqlf" "go.opentelemetry.io/otel/attribute" @@ -37,7 +36,7 @@ func (s *syntacticIndexingJobStoreImpl) DBWorkerStore() dbworkerstore.Store[*Syn return s.store } -func NewStoreWithDB(observationCtx *observation.Context, db *sql.DB) (SyntacticIndexingJobStore, error) { +func NewStoreWithDB(observationCtx *observation.Context, db database.DB) (SyntacticIndexingJobStore, error) { // Make sure this is in sync with the columns of the // syntactic_scip_indexing_jobs_with_repository_name view var columnExpressions = []*sqlf.Query{ @@ -67,10 +66,9 @@ func NewStoreWithDB(observationCtx *observation.Context, db *sql.DB) (SyntacticI Scan: dbworkerstore.BuildWorkerScan(ScanSyntacticIndexRecord), } - handle := basestore.NewHandleWithDB(observationCtx.Logger, db, sql.TxOptions{}) return &syntacticIndexingJobStoreImpl{ - store: dbworkerstore.New(observationCtx, handle, storeOptions), - db: basestore.NewWithHandle(handle), + store: dbworkerstore.New(observationCtx, db.Handle(), storeOptions), + db: basestore.NewWithHandle(db.Handle()), operations: newOperations(observationCtx), logger: observationCtx.Logger.Scoped("syntactic_indexing.store"), }, nil diff --git a/internal/codeintel/syntactic_indexing/scheduler.go b/internal/codeintel/syntactic_indexing/scheduler.go index 637ff44d103..24ec012018c 100644 --- a/internal/codeintel/syntactic_indexing/scheduler.go +++ b/internal/codeintel/syntactic_indexing/scheduler.go @@ -2,7 +2,6 @@ package syntactic_indexing import ( "context" - "database/sql" "time" "github.com/sourcegraph/sourcegraph/internal/api" @@ -51,13 +50,9 @@ func NewSyntacticJobScheduler(repoSchedulingSvc reposcheduler.RepositoryScheduli }, nil } -func BootstrapSyntacticJobScheduler(observationCtx *observation.Context, frontendSQLDB *sql.DB, codeintelSQLDB *sql.DB) (SyntacticJobScheduler, error) { - frontendDB := database.NewDB(observationCtx.Logger, frontendSQLDB) - +func BootstrapSyntacticJobScheduler(observationCtx *observation.Context, frontendDB database.DB, codeIntelDB codeintelshared.CodeIntelDB) (SyntacticJobScheduler, error) { gitserverClient := gitserver.NewClient("codeintel-syntactic-indexing") - codeIntelDB := codeintelshared.NewCodeIntelDB(observationCtx.Logger, codeintelSQLDB) - uploadsSvc := uploads.NewService(observationCtx, frontendDB, codeIntelDB, gitserverClient.Scoped("uploads")) policiesSvc := policies.NewService(observationCtx, frontendDB, uploadsSvc, gitserverClient.Scoped("policies")) @@ -71,7 +66,7 @@ func BootstrapSyntacticJobScheduler(observationCtx *observation.Context, fronten repoSchedulingStore := reposcheduler.NewSyntacticStore(observationCtx, frontendDB) repoSchedulingSvc := reposcheduler.NewService(repoSchedulingStore) - jobStore, err := jobstore.NewStoreWithDB(observationCtx, frontendSQLDB) + jobStore, err := jobstore.NewStoreWithDB(observationCtx, frontendDB) if err != nil { return nil, err } diff --git a/internal/codeintel/syntactic_indexing/scheduler_job.go b/internal/codeintel/syntactic_indexing/scheduler_job.go index c0ee9d619b7..ef4084e7e8d 100644 --- a/internal/codeintel/syntactic_indexing/scheduler_job.go +++ b/internal/codeintel/syntactic_indexing/scheduler_job.go @@ -36,12 +36,12 @@ func (job *syntacticIndexingSchedulerJob) Config() []env.Config { } func (job *syntacticIndexingSchedulerJob) Routines(_ context.Context, observationCtx *observation.Context) ([]goroutine.BackgroundRoutine, error) { - frontendDB, err := workerdb.InitRawDB(observationCtx) + frontendDB, err := workerdb.InitDB(observationCtx) if err != nil { return nil, err } - codeintelDB, err := codeinteldb.InitRawDB(observationCtx) + codeintelDB, err := codeinteldb.InitDB(observationCtx) if err != nil { return nil, err } diff --git a/internal/codeintel/syntactic_indexing/scheduler_test.go b/internal/codeintel/syntactic_indexing/scheduler_test.go index 2277dea0ae0..fea4d3d4576 100644 --- a/internal/codeintel/syntactic_indexing/scheduler_test.go +++ b/internal/codeintel/syntactic_indexing/scheduler_test.go @@ -144,7 +144,7 @@ func bootstrapScheduler(t *testing.T, observationCtx *observation.Context, ) repoSchedulingStore := reposcheduler.NewSyntacticStore(observationCtx, frontendDB) repoSchedulingSvc := reposcheduler.NewService(repoSchedulingStore) - jobStore := unwrap(jobstore.NewStoreWithDB(observationCtx, frontendRawDB))(t) + jobStore := unwrap(jobstore.NewStoreWithDB(observationCtx, frontendDB))(t) repoStore := frontendDB.Repos() enqueuer := NewIndexEnqueuer(observationCtx, jobStore, repoSchedulingStore, repoStore) diff --git a/internal/codeintel/syntactic_indexing/store_test.go b/internal/codeintel/syntactic_indexing/store_test.go index a767b33dafc..ce411e7a3ed 100644 --- a/internal/codeintel/syntactic_indexing/store_test.go +++ b/internal/codeintel/syntactic_indexing/store_test.go @@ -30,10 +30,9 @@ func TestSyntacticIndexingStoreDequeue(t *testing.T) { schema/implementation drift. */ observationContext := observation.TestContextTB(t) - sqlDB := dbtest.NewDB(t) - db := database.NewDB(observationContext.Logger, sqlDB) + db := database.NewDB(observationContext.Logger, dbtest.NewDB(t)) - jobStore, err := jobstore.NewStoreWithDB(observationContext, sqlDB) + jobStore, err := jobstore.NewStoreWithDB(observationContext, db) require.NoError(t, err, "unexpected error creating dbworker stores") store := jobStore.DBWorkerStore() @@ -117,11 +116,10 @@ func TestSyntacticIndexingStoreEnqueue(t *testing.T) { are valid from the point of view of the DB worker interface */ observationContext := observation.TestContextTB(t) - sqlDB := dbtest.NewDB(t) - db := database.NewDB(observationContext.Logger, sqlDB) + db := database.NewDB(observationContext.Logger, dbtest.NewDB(t)) ctx := context.Background() - jobStore, err := jobstore.NewStoreWithDB(observationContext, sqlDB) + jobStore, err := jobstore.NewStoreWithDB(observationContext, db) require.NoError(t, err, "unexpected error creating dbworker stores") store := jobStore.DBWorkerStore() diff --git a/internal/codeintel/uploads/BUILD.bazel b/internal/codeintel/uploads/BUILD.bazel index 62570b8f362..68b0fe5f322 100644 --- a/internal/codeintel/uploads/BUILD.bazel +++ b/internal/codeintel/uploads/BUILD.bazel @@ -15,7 +15,6 @@ go_library( tags = [TAG_PLATFORM_GRAPH], visibility = ["//:__subpackages__"], deps = [ - "//cmd/frontend/backend", "//internal/api", "//internal/codeintel/codegraph", "//internal/codeintel/shared", diff --git a/internal/codeintel/uploads/init.go b/internal/codeintel/uploads/init.go index 04d7bc55592..6b199a9d9de 100644 --- a/internal/codeintel/uploads/init.go +++ b/internal/codeintel/uploads/init.go @@ -3,7 +3,6 @@ package uploads import ( "time" - "github.com/sourcegraph/sourcegraph/cmd/frontend/backend" lsifstore "github.com/sourcegraph/sourcegraph/internal/codeintel/codegraph" codeintelshared "github.com/sourcegraph/sourcegraph/internal/codeintel/shared" "github.com/sourcegraph/sourcegraph/internal/codeintel/uploads/internal/background" @@ -27,13 +26,12 @@ func NewService( gitserverClient gitserver.Client, ) *Service { store := uploadsstore.New(scopedContext("uploadsstore", observationCtx), db) - repoStore := backend.NewRepos(scopedContext("repos", observationCtx).Logger, db, gitserverClient) lsifStore := lsifstore.New(scopedContext("lsifstore", observationCtx), codeIntelDB) svc := newService( scopedContext("service", observationCtx), store, - repoStore, + db.Repos(), lsifStore, gitserverClient, ) diff --git a/internal/codeintel/uploads/internal/background/processor/BUILD.bazel b/internal/codeintel/uploads/internal/background/processor/BUILD.bazel index 3eea47b7cd7..060a16c97ed 100644 --- a/internal/codeintel/uploads/internal/background/processor/BUILD.bazel +++ b/internal/codeintel/uploads/internal/background/processor/BUILD.bazel @@ -56,7 +56,6 @@ go_test( embed = [":processor"], tags = [TAG_PLATFORM_GRAPH], deps = [ - "//cmd/frontend/backend", "//internal/api", "//internal/codeintel/codegraph", "//internal/codeintel/codegraph/codegraphmocks", diff --git a/internal/codeintel/uploads/internal/background/processor/job_worker_handler_test.go b/internal/codeintel/uploads/internal/background/processor/job_worker_handler_test.go index 4324f7482bf..70f829394b7 100644 --- a/internal/codeintel/uploads/internal/background/processor/job_worker_handler_test.go +++ b/internal/codeintel/uploads/internal/background/processor/job_worker_handler_test.go @@ -18,7 +18,6 @@ import ( "github.com/sourcegraph/scip/bindings/go/scip" "google.golang.org/protobuf/proto" - "github.com/sourcegraph/sourcegraph/cmd/frontend/backend" "github.com/sourcegraph/sourcegraph/internal/api" "github.com/sourcegraph/sourcegraph/internal/codeintel/codegraph" "github.com/sourcegraph/sourcegraph/internal/codeintel/codegraph/codegraphmocks" @@ -39,8 +38,6 @@ import ( ) func TestHandle(t *testing.T) { - setupRepoMocks(t) - upload := shared.Upload{ ID: 42, Root: "", @@ -56,6 +53,12 @@ func TestHandle(t *testing.T) { mockLSIFStore := codegraphmocks.NewMockDataStore() mockUploadStore := objectmocks.NewMockStorage() gitserverClient := gitserver.NewMockClient() + gitserverClient.ResolveRevisionFunc.SetDefaultHook(func(_ context.Context, _ api.RepoName, commit string, _ gitserver.ResolveRevisionOptions) (api.CommitID, error) { + if commit != "deadbeef" { + t.Errorf("unexpected commit. want=%s have=%s", "deadbeef", commit) + } + return "", nil + }) // Set default transaction behavior mockDBStore.WithTransactionFunc.SetDefaultHook(func(ctx context.Context, f func(s store.Store) error) error { return f(mockDBStore) }) @@ -288,8 +291,6 @@ func TestHandle(t *testing.T) { } func TestHandleError(t *testing.T) { - setupRepoMocks(t) - upload := shared.Upload{ ID: 42, Root: "root/", @@ -432,27 +433,6 @@ var scipDirectoryChildren = map[string][]string{ }, } -func setupRepoMocks(t *testing.T) { - t.Cleanup(func() { - backend.Mocks.Repos.Get = nil - backend.Mocks.Repos.ResolveRev = nil - }) - - backend.Mocks.Repos.Get = func(ctx context.Context, repoID api.RepoID) (*types.Repo, error) { - if repoID != api.RepoID(50) { - t.Errorf("unexpected repository name. want=%d have=%d", 50, repoID) - } - return &types.Repo{ID: repoID}, nil - } - - backend.Mocks.Repos.ResolveRev = func(ctx context.Context, repo api.RepoName, rev string) (api.CommitID, error) { - if rev != "deadbeef" { - t.Errorf("unexpected commit. want=%s have=%s", "deadbeef", rev) - } - return "", nil - } -} - func defaultMockRepoStore() *dbmocks.MockRepoStore { repoStore := dbmocks.NewMockRepoStore() repoStore.GetFunc.SetDefaultHook(func(ctx context.Context, id api.RepoID) (*internaltypes.Repo, error) { diff --git a/internal/codeintel/uploads/transport/http/BUILD.bazel b/internal/codeintel/uploads/transport/http/BUILD.bazel index 78b0a036c2c..4c0f64c1da0 100644 --- a/internal/codeintel/uploads/transport/http/BUILD.bazel +++ b/internal/codeintel/uploads/transport/http/BUILD.bazel @@ -15,7 +15,6 @@ go_library( tags = [TAG_PLATFORM_GRAPH], visibility = ["//:__subpackages__"], deps = [ - "//cmd/frontend/backend", "//internal/api", "//internal/codeintel/uploads", "//internal/codeintel/uploads/transport/http/auth", @@ -48,7 +47,6 @@ go_test( "requires-network", ], deps = [ - "//cmd/frontend/backend", "//internal/actor", "//internal/api", "//internal/codeintel/uploads", @@ -65,6 +63,7 @@ go_test( "//schema", "@com_github_keegancsmith_sqlf//:sqlf", "@com_github_sourcegraph_log//logtest", + "@com_github_stretchr_testify//require", ], ) diff --git a/internal/codeintel/uploads/transport/http/auth/BUILD.bazel b/internal/codeintel/uploads/transport/http/auth/BUILD.bazel index c20cf31503d..922888ca783 100644 --- a/internal/codeintel/uploads/transport/http/auth/BUILD.bazel +++ b/internal/codeintel/uploads/transport/http/auth/BUILD.bazel @@ -16,7 +16,6 @@ go_library( tags = [TAG_PLATFORM_GRAPH], visibility = ["//:__subpackages__"], deps = [ - "//cmd/frontend/backend", "//internal/actor", "//internal/api", "//internal/conf", @@ -48,7 +47,6 @@ go_test( embed = [":auth"], tags = [TAG_PLATFORM_GRAPH], deps = [ - "//cmd/frontend/backend", "//internal/actor", "//internal/conf", "//internal/database", diff --git a/internal/codeintel/uploads/transport/http/auth/middleware.go b/internal/codeintel/uploads/transport/http/auth/middleware.go index 0feaa306c30..e198396f622 100644 --- a/internal/codeintel/uploads/transport/http/auth/middleware.go +++ b/internal/codeintel/uploads/transport/http/auth/middleware.go @@ -10,7 +10,6 @@ import ( sglog "github.com/sourcegraph/log" "go.opentelemetry.io/otel/attribute" - "github.com/sourcegraph/sourcegraph/cmd/frontend/backend" "github.com/sourcegraph/sourcegraph/internal/actor" "github.com/sourcegraph/sourcegraph/internal/api" "github.com/sourcegraph/sourcegraph/internal/conf" @@ -24,6 +23,9 @@ import ( type ( AuthValidator func(context.Context, url.Values, string) (int, error) AuthValidatorMap = map[string]AuthValidator + RepoStore interface { + GetByName(context.Context, api.RepoName) (*types.Repo, error) + } ) var DefaultValidatorByCodeHost = AuthValidatorMap{ @@ -45,7 +47,7 @@ var errVerificationNotSupported = errors.New(strings.Join([]string{ func AuthMiddleware( next http.Handler, userStore UserStore, - repoStore backend.ReposService, + repoStore RepoStore, authValidators AuthValidatorMap, operation *observation.Operation, ) http.Handler { @@ -128,7 +130,7 @@ func isLoggedIn(ctx context.Context, userStore UserStore, trace observation.Trac func (u *loggedInUserDoNotCreateThisTypeDirectly) canAccessRepository( ctx context.Context, - repoStore backend.ReposService, + repoStore RepoStore, repositoryName string, trace observation.TraceLogger, ) (int, error) { diff --git a/internal/codeintel/uploads/transport/http/auth/middleware_test.go b/internal/codeintel/uploads/transport/http/auth/middleware_test.go index e4a2f09b07a..6eb11e65f41 100644 --- a/internal/codeintel/uploads/transport/http/auth/middleware_test.go +++ b/internal/codeintel/uploads/transport/http/auth/middleware_test.go @@ -5,7 +5,6 @@ import ( "net/http/httptest" "testing" - "github.com/sourcegraph/sourcegraph/cmd/frontend/backend" "github.com/sourcegraph/sourcegraph/internal/actor" "github.com/sourcegraph/sourcegraph/internal/conf" "github.com/sourcegraph/sourcegraph/internal/database" @@ -36,7 +35,6 @@ func TestUploadAuthMiddleware(t *testing.T) { } userStore := dbmocks.NewMockUserStore() - repoStore := backend.NewMockReposService() authValidators := map[string]AuthValidator{} operation := observation.TestContext.Operation(observation.Op{}) @@ -116,6 +114,7 @@ func TestUploadAuthMiddleware(t *testing.T) { userStore.GetByCurrentAuthUserFunc.SetDefaultReturn(nil, database.ErrNoCurrentUser) } + repoStore := dbmocks.NewMockRepoStore() if testCase.hasRepoAccess { repoStore.GetByNameFunc.SetDefaultReturn(nil, nil) } else { diff --git a/internal/codeintel/uploads/transport/http/handler.go b/internal/codeintel/uploads/transport/http/handler.go index 82005ab953c..4d95a598ca7 100644 --- a/internal/codeintel/uploads/transport/http/handler.go +++ b/internal/codeintel/uploads/transport/http/handler.go @@ -9,6 +9,7 @@ import ( "github.com/sourcegraph/sourcegraph/internal/api" "github.com/sourcegraph/sourcegraph/internal/codeintel/uploads" "github.com/sourcegraph/sourcegraph/internal/errcode" + "github.com/sourcegraph/sourcegraph/internal/gitserver" "github.com/sourcegraph/sourcegraph/internal/gitserver/gitdomain" "github.com/sourcegraph/sourcegraph/internal/lazyregexp" "github.com/sourcegraph/sourcegraph/internal/object" @@ -22,6 +23,7 @@ var revhashPattern = lazyregexp.New(`^[a-z0-9]{40}$`) func newHandler( observationCtx *observation.Context, repoStore RepoStore, + gitserverClient gitserver.Client, uploadStore object.Storage, dbStore uploadhandler.DBStore[uploads.UploadMetadata], operations *uploadhandler.Operations, @@ -36,7 +38,7 @@ func newHandler( // Ensure that the repository and commit given in the request are resolvable. repositoryName := getQuery(r, "repository") - repositoryID, statusCode, err := ensureRepoAndCommitExist(ctx, repoStore, repositoryName, commit, logger) + repositoryID, statusCode, err := ensureRepoAndCommitExist(ctx, repoStore, gitserverClient, repositoryName, commit, logger) if err != nil { return uploads.UploadMetadata{}, statusCode, err } @@ -69,7 +71,7 @@ func newHandler( return handler } -func ensureRepoAndCommitExist(ctx context.Context, repoStore RepoStore, repoName, commit string, logger log.Logger) (int, int, error) { +func ensureRepoAndCommitExist(ctx context.Context, repoStore RepoStore, gitserverClient gitserver.Client, repoName, commit string, logger log.Logger) (int, int, error) { // // 1. Resolve repository @@ -85,7 +87,7 @@ func ensureRepoAndCommitExist(ctx context.Context, repoStore RepoStore, repoName // // 2. Resolve commit - if _, err := repoStore.ResolveRev(ctx, repo.Name, commit); err != nil { + if _, err := gitserverClient.ResolveRevision(ctx, repo.Name, commit, gitserver.ResolveRevisionOptions{EnsureRevision: true}); err != nil { var reason string if errors.HasType[*gitdomain.RevisionNotFoundError](err) { reason = "commit not found" diff --git a/internal/codeintel/uploads/transport/http/handler_test.go b/internal/codeintel/uploads/transport/http/handler_test.go index 4ef8a55562f..936f9a15119 100644 --- a/internal/codeintel/uploads/transport/http/handler_test.go +++ b/internal/codeintel/uploads/transport/http/handler_test.go @@ -10,8 +10,8 @@ import ( "github.com/keegancsmith/sqlf" "github.com/sourcegraph/log/logtest" + "github.com/stretchr/testify/require" - "github.com/sourcegraph/sourcegraph/cmd/frontend/backend" "github.com/sourcegraph/sourcegraph/internal/actor" "github.com/sourcegraph/sourcegraph/internal/api" "github.com/sourcegraph/sourcegraph/internal/codeintel/uploads" @@ -31,13 +31,17 @@ import ( const testCommit = "deadbeef01deadbeef02deadbeef03deadbeef04" func TestHandleEnqueueAuth(t *testing.T) { - setupRepoMocks(t) - logger := logtest.Scoped(t) db := database.NewDB(logger, dbtest.NewDB(t)) - repoStore := backend.NewRepos(logger, db, gitserver.NewMockClient()) mockDBStore := NewMockDBStore[uploads.UploadMetadata]() mockUploadStore := objectmocks.NewMockStorage() + gitserverClient := gitserver.NewMockClient() + gitserverClient.ResolveRevisionFunc.SetDefaultHook(func(_ context.Context, _ api.RepoName, commit string, _ gitserver.ResolveRevisionOptions) (api.CommitID, error) { + if commit != testCommit { + t.Errorf("unexpected commit. want=%s have=%s", testCommit, commit) + } + return "", nil + }) conf.Mock(&conf.Unified{ SiteConfiguration: schema.SiteConfiguration{ @@ -46,6 +50,8 @@ func TestHandleEnqueueAuth(t *testing.T) { }) t.Cleanup(func() { conf.Mock(nil) }) + require.NoError(t, db.Repos().Create(context.Background(), &types.Repo{Name: "github.com/test/test"})) + mockDBStore.WithTransactionFunc.SetDefaultHook(func(ctx context.Context, f func(tx uploadhandler.DBStore[uploads.UploadMetadata]) error) error { return f(mockDBStore) }) @@ -118,13 +124,14 @@ func TestHandleEnqueueAuth(t *testing.T) { auth.AuthMiddleware( newHandler( observation.TestContextTB(t), - repoStore, + db.Repos(), + gitserverClient, mockUploadStore, mockDBStore, uploadhandler.NewOperations(observation.TestContextTB(t), "test"), ), db.Users(), - repoStore, + db.Repos(), authValidators, newOperations(observation.TestContextTB(t)).authMiddleware, ).ServeHTTP(w, r) @@ -135,27 +142,6 @@ func TestHandleEnqueueAuth(t *testing.T) { } } -func setupRepoMocks(t testing.TB) { - t.Cleanup(func() { - backend.Mocks.Repos.GetByName = nil - backend.Mocks.Repos.ResolveRev = nil - }) - - backend.Mocks.Repos.GetByName = func(ctx context.Context, name api.RepoName) (*types.Repo, error) { - if name != "github.com/test/test" { - t.Errorf("unexpected repository name. want=%s have=%s", "github.com/test/test", name) - } - return &types.Repo{ID: 50}, nil - } - - backend.Mocks.Repos.ResolveRev = func(ctx context.Context, repo api.RepoName, rev string) (api.CommitID, error) { - if rev != testCommit { - t.Errorf("unexpected commit. want=%s have=%s", testCommit, rev) - } - return "", nil - } -} - func insertTestUser(t *testing.T, db database.DB, name string, isAdmin bool) (userID int32) { t.Helper() diff --git a/internal/codeintel/uploads/transport/http/iface.go b/internal/codeintel/uploads/transport/http/iface.go index c8e1c143b7e..4604dce747b 100644 --- a/internal/codeintel/uploads/transport/http/iface.go +++ b/internal/codeintel/uploads/transport/http/iface.go @@ -9,5 +9,4 @@ import ( type RepoStore interface { GetByName(ctx context.Context, name api.RepoName) (*types.Repo, error) - ResolveRev(ctx context.Context, repo api.RepoName, rev string) (api.CommitID, error) } diff --git a/internal/codeintel/uploads/transport/http/init.go b/internal/codeintel/uploads/transport/http/init.go index c55a3a937fc..e40b01e7d86 100644 --- a/internal/codeintel/uploads/transport/http/init.go +++ b/internal/codeintel/uploads/transport/http/init.go @@ -6,7 +6,6 @@ import ( "github.com/sourcegraph/log" - "github.com/sourcegraph/sourcegraph/cmd/frontend/backend" "github.com/sourcegraph/sourcegraph/internal/codeintel/uploads" "github.com/sourcegraph/sourcegraph/internal/codeintel/uploads/transport/http/auth" "github.com/sourcegraph/sourcegraph/internal/database" @@ -34,11 +33,11 @@ func GetHandler(svc *uploads.Service, db database.DB, gitserverClient gitserver. uploadHandlerOperations := uploadhandler.NewOperations(observationCtx, "codeintel") userStore := db.Users() - repoStore := backend.NewRepos(logger, db, gitserverClient) + repoStore := db.Repos() // Construct base handler, used in internal routes and as internal handler wrapped // in the auth middleware defined on the next few lines - handler = newHandler(observationCtx, repoStore, uploadStore, svc.UploadHandlerStore(), uploadHandlerOperations) + handler = newHandler(observationCtx, repoStore, gitserverClient, uploadStore, svc.UploadHandlerStore(), uploadHandlerOperations) // 🚨 SECURITY: Non-internal installations of this handler will require a user/repo // visibility check with the remote code host (if enabled via site configuration). diff --git a/internal/oobmigration/migrations/register/BUILD.bazel b/internal/oobmigration/migrations/register/BUILD.bazel index 6461ebd1c54..24047f1a22a 100644 --- a/internal/oobmigration/migrations/register/BUILD.bazel +++ b/internal/oobmigration/migrations/register/BUILD.bazel @@ -7,11 +7,11 @@ go_library( tags = [TAG_INFRA_RELEASE], visibility = ["//:__subpackages__"], deps = [ - "//cmd/worker/shared/init/codeinsights", - "//cmd/worker/shared/init/codeintel", + "//internal/conf", "//internal/conf/conftypes", "//internal/database", "//internal/database/basestore", + "//internal/database/connections/live", "//internal/encryption/keyring", "//internal/insights", "//internal/observation", @@ -23,6 +23,7 @@ go_library( "//internal/oobmigration/migrations/insights", "//internal/oobmigration/migrations/insights/backfillv2", "//internal/oobmigration/migrations/insights/recording_times", + "//lib/errors", "@com_github_derision_test_glock//:glock", "@com_github_sourcegraph_log//:log", ], diff --git a/internal/oobmigration/migrations/register/register.go b/internal/oobmigration/migrations/register/register.go index 2e1874892be..d5d27f18c9c 100644 --- a/internal/oobmigration/migrations/register/register.go +++ b/internal/oobmigration/migrations/register/register.go @@ -8,11 +8,11 @@ import ( "github.com/derision-test/glock" "github.com/sourcegraph/log" - workerCodeInsights "github.com/sourcegraph/sourcegraph/cmd/worker/shared/init/codeinsights" - workerCodeIntel "github.com/sourcegraph/sourcegraph/cmd/worker/shared/init/codeintel" + "github.com/sourcegraph/sourcegraph/internal/conf" "github.com/sourcegraph/sourcegraph/internal/conf/conftypes" "github.com/sourcegraph/sourcegraph/internal/database" "github.com/sourcegraph/sourcegraph/internal/database/basestore" + connections "github.com/sourcegraph/sourcegraph/internal/database/connections/live" "github.com/sourcegraph/sourcegraph/internal/encryption/keyring" internalinsights "github.com/sourcegraph/sourcegraph/internal/insights" "github.com/sourcegraph/sourcegraph/internal/observation" @@ -24,6 +24,7 @@ import ( "github.com/sourcegraph/sourcegraph/internal/oobmigration/migrations/insights" insightsBackfiller "github.com/sourcegraph/sourcegraph/internal/oobmigration/migrations/insights/backfillv2" insightsrecordingtimes "github.com/sourcegraph/sourcegraph/internal/oobmigration/migrations/insights/recording_times" + "github.com/sourcegraph/sourcegraph/lib/errors" ) func RegisterOSSMigrators(ctx context.Context, db database.DB, runner *oobmigration.Runner) error { @@ -90,14 +91,14 @@ func RegisterAll(runner *oobmigration.Runner, noDelay bool, migrators []TaggedMi } func RegisterEnterpriseMigrators(ctx context.Context, db database.DB, runner *oobmigration.Runner) error { - codeIntelDB, err := workerCodeIntel.InitRawDB(&observation.TestContext) + codeIntelDB, err := initCodeintelDB(&observation.TestContext) if err != nil { return err } var insightsStore *basestore.Store if internalinsights.IsEnabled() { - insightsDB, err := workerCodeInsights.InitRawDB(&observation.TestContext) + insightsDB, err := initCodeInsightsDB(&observation.TestContext) if err != nil { return err } @@ -177,3 +178,29 @@ func registerEnterpriseMigrators(runner *oobmigration.Runner, noDelay bool, deps } return RegisterAll(runner, noDelay, migrators) } + +func initCodeintelDB(observationCtx *observation.Context) (*sql.DB, error) { + dsn := conf.GetServiceConnectionValueAndRestartOnChange(func(serviceConnections conftypes.ServiceConnections) string { + return serviceConnections.CodeIntelPostgresDSN + }) + + db, err := connections.EnsureNewCodeIntelDB(observationCtx, dsn, "oobmigration") + if err != nil { + return nil, errors.Errorf("failed to connect to codeintel database: %s", err) + } + + return db, nil +} + +func initCodeInsightsDB(observationCtx *observation.Context) (*sql.DB, error) { + dsn := conf.GetServiceConnectionValueAndRestartOnChange(func(serviceConnections conftypes.ServiceConnections) string { + return serviceConnections.CodeInsightsDSN + }) + + db, err := connections.EnsureNewCodeInsightsDB(observationCtx, dsn, "oobmigration") + if err != nil { + return nil, errors.Errorf("failed to connect to codeinsights database: %s", err) + } + + return db, nil +}