Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cmd/frontend/internal/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,10 @@ func serviceConnections() conftypes.ServiceConnections {
}

serviceConnectionsVal = conftypes.ServiceConnections{
GitServers: gitServers(),
PostgresDSN: dbutil.PostgresDSN("", username, os.Getenv),
CodeIntelPostgresDSN: dbutil.PostgresDSN("codeintel", username, os.Getenv),
GitServers: gitServers(),
PostgresDSN: dbutil.PostgresDSN("", username, os.Getenv),
CodeIntelPostgresDSN: dbutil.PostgresDSN("codeintel", username, os.Getenv),
CodeInsightsTimescaleDSN: dbutil.PostgresDSN("codeinsights", username, os.Getenv),
}

// We set this envvar in development to disable the following check
Expand Down
35 changes: 35 additions & 0 deletions enterprise/internal/insights/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,48 @@ package insights

import (
"context"
"database/sql"
"fmt"
"log"

"github.com/sourcegraph/sourcegraph/cmd/frontend/enterprise"
"github.com/sourcegraph/sourcegraph/enterprise/internal/insights/resolvers"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/database/dbconn"
)

// Init initializes the given enterpriseServices to include the required resolvers for insights.
func Init(ctx context.Context, enterpriseServices *enterprise.Services) error {
if !conf.IsDev(conf.DeployType()) {
// Code Insights is not yet deployed to non-dev/testing instances. We don't yet have
// TimescaleDB in those deployments. https://github.com/sourcegraph/sourcegraph/issues/17218
return nil
}
_, err := initializeCodeInsightsDB()
if err != nil {
return err
}
enterpriseServices.InsightsResolver = resolvers.New()
return nil
}

// initializeCodeInsightsDB connects to and initializes the Code Insights Timescale DB, running
// database migrations before returning.
func initializeCodeInsightsDB() (*sql.DB, error) {
timescaleDSN := conf.Get().ServiceConnections.CodeInsightsTimescaleDSN
conf.Watch(func() {
if newDSN := conf.Get().ServiceConnections.CodeInsightsTimescaleDSN; timescaleDSN != newDSN {
log.Fatalf("Detected codeinsights database DSN change, restarting to take effect: %s", newDSN)
}
})

db, err := dbconn.New(timescaleDSN, "")
if err != nil {
return nil, fmt.Errorf("Failed to connect to codeinsights database: %s", err)
}

if err := dbconn.MigrateDB(db, dbconn.CodeInsights); err != nil {
return nil, fmt.Errorf("Failed to perform codeinsights database migration: %s", err)
}
return db, nil
}
5 changes: 5 additions & 0 deletions internal/conf/conftypes/conftypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type ServiceConnections struct {
// code intel database.
// eg: "postgres://sg@pgsql/sourcegraph_codeintel?sslmode=false"
CodeIntelPostgresDSN string `json:"codeIntelPostgresDSN"`

// CodeInsightsTimescaleDSN is the TimescaleDB data source name for the
// code insights database.
// eg: "postgres://sg@pgsql/sourcegraph_codeintel?sslmode=false"
CodeInsightsTimescaleDSN string `json:"codeinsightsTimescaleDSN"`
}

// RawUnified is the unparsed variant of conf.Unified.
Expand Down