insights: update references to TimescaleDB (#32948)

* Replacing TimescaleDB with CodeInsightsDB
* Replacing timescaleDSN with insightsDSN
* Remove or update TimescaleDB references in docs and comments
This commit is contained in:
leo 2022-03-29 11:04:47 +01:00 committed by GitHub
parent 4b1288f069
commit 89663c8d80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 205 additions and 209 deletions

View File

@ -448,9 +448,9 @@ func serviceConnections() conftypes.ServiceConnections {
}
serviceConnectionsVal = conftypes.ServiceConnections{
PostgresDSN: dsns["frontend"],
CodeIntelPostgresDSN: dsns["codeintel"],
CodeInsightsTimescaleDSN: dsns["codeinsights"],
PostgresDSN: dsns["frontend"],
CodeIntelPostgresDSN: dsns["codeintel"],
CodeInsightsDSN: dsns["codeinsights"],
}
})
@ -460,9 +460,9 @@ func serviceConnections() conftypes.ServiceConnections {
}
return conftypes.ServiceConnections{
GitServers: addrs,
PostgresDSN: serviceConnectionsVal.PostgresDSN,
CodeIntelPostgresDSN: serviceConnectionsVal.CodeIntelPostgresDSN,
CodeInsightsTimescaleDSN: serviceConnectionsVal.CodeInsightsTimescaleDSN,
GitServers: addrs,
PostgresDSN: serviceConnectionsVal.PostgresDSN,
CodeIntelPostgresDSN: serviceConnectionsVal.CodeIntelPostgresDSN,
CodeInsightsDSN: serviceConnectionsVal.CodeInsightsDSN,
}
}

View File

@ -1,12 +1,12 @@
#!/usr/bin/env bash
# Description: Code Insights TimescaleDB.
# Description: Code Insights Postgres DB.
set -euf -o pipefail
pushd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null
if [ -n "${DISABLE_CODE_INSIGHTS:-}" ]; then
echo Not starting Code Insights TimescaleDB since DISABLE_CODE_INSIGHTS is set.
echo Not starting Code Insights DB since DISABLE_CODE_INSIGHTS is set.
exit 0
fi
@ -28,9 +28,6 @@ docker inspect $CONTAINER >/dev/null 2>&1 && docker rm -f $CONTAINER
LOGS="${HOME}/.sourcegraph-dev/logs/codeinsights-db"
mkdir -p "${LOGS}"
# Now for the actual logging. TimescaleDB's output gets sent to stdout and stderr.
# We want to capture that output, but because it's fairly noisy, don't want to
# display it in the normal case.
LOG_FILE="${LOGS}/codeinsights-db.log"
# Quickly build image

View File

@ -34,7 +34,7 @@ func GetBackgroundJobs(ctx context.Context, mainAppDB *sql.DB, insightsDB *sql.D
insightsStore := store.New(insightsDB, insightPermStore)
// Create a base store to be used for storing worker state. We store this in the main app Postgres
// DB, not the TimescaleDB (which we use only for storing insights data.)
// DB, not the insights DB (which we use only for storing insights data.)
workerBaseStore := basestore.NewWithDB(mainAppDB, sql.TxOptions{})
// Create basic metrics for recording information about background jobs.
@ -89,7 +89,7 @@ func GetBackgroundQueryRunnerJob(ctx context.Context, mainAppDB *sql.DB, insight
insightsStore := store.New(insightsDB, insightPermStore)
// Create a base store to be used for storing worker state. We store this in the main app Postgres
// DB, not the TimescaleDB (which we use only for storing insights data.)
// DB, not the insights DB (which we use only for storing insights data.)
workerBaseStore := basestore.NewWithDB(mainAppDB, sql.TxOptions{})
// Create basic metrics for recording information about background jobs.
@ -104,7 +104,7 @@ func GetBackgroundQueryRunnerJob(ctx context.Context, mainAppDB *sql.DB, insight
return []goroutine.BackgroundRoutine{
// Register the query-runner worker and resetter, which executes search queries and records
// results to TimescaleDB.
// results to the insights DB.
queryrunner.NewWorker(ctx, workerStore, insightsStore, queryRunnerWorkerMetrics),
queryrunner.NewResetter(ctx, workerStore, queryRunnerResetterMetrics),
queryrunner.NewCleaner(ctx, workerBaseStore, observationContext),

View File

@ -28,12 +28,12 @@ func TestPerformPurge(t *testing.T) {
ctx := context.Background()
clock := timeutil.Now
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
postgres := dbtest.NewDB(t)
permStore := store.NewInsightPermissionStore(postgres)
timeseriesStore := store.NewWithClock(timescale, permStore, clock)
insightStore := store.NewInsightStore(timescale)
timeseriesStore := store.NewWithClock(insightsDB, permStore, clock)
insightStore := store.NewInsightStore(insightsDB)
workerBaseStore := basestore.NewWithDB(postgres, sql.TxOptions{})
getTimeSeriesCountForSeries := func(ctx context.Context, seriesId string) int {
@ -164,7 +164,7 @@ func TestPerformPurge(t *testing.T) {
t.Fatal(err)
}
err = performPurge(ctx, postgres, timescale, time.Now())
err = performPurge(ctx, postgres, insightsDB, time.Now())
if err != nil {
t.Fatal(err)
}

View File

@ -22,7 +22,7 @@ func TestCheckAndEnforceLicense(t *testing.T) {
}
ctx := context.Background()
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
defer func() {
@ -40,13 +40,13 @@ func TestCheckAndEnforceLicense(t *testing.T) {
}
getNumFrozenInsights := func() (int, error) {
return basestore.ScanInt(timescale.QueryRow(`SELECT COUNT(*) FROM insight_view WHERE is_frozen = TRUE`))
return basestore.ScanInt(insightsDB.QueryRow(`SELECT COUNT(*) FROM insight_view WHERE is_frozen = TRUE`))
}
getLAMDashboardCount := func() (int, error) {
return basestore.ScanInt(timescale.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM dashboard WHERE type = '%s'", store.LimitedAccessMode)))
return basestore.ScanInt(insightsDB.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM dashboard WHERE type = '%s'", store.LimitedAccessMode)))
}
_, err := timescale.Exec(`INSERT INTO insight_view (id, title, description, unique_id, is_frozen)
_, err := insightsDB.Exec(`INSERT INTO insight_view (id, title, description, unique_id, is_frozen)
VALUES (1, 'unattached insight', 'test description', 'unique-1', true),
(2, 'private insight 2', 'test description', 'unique-2', true),
(3, 'org insight 1', 'test description', 'unique-3', true),
@ -56,7 +56,7 @@ func TestCheckAndEnforceLicense(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO dashboard (title)
_, err = insightsDB.Exec(`INSERT INTO dashboard (title)
VALUES ('private dashboard 1'),
('org dashboard 1'),
('global dashboard 1'),
@ -64,7 +64,7 @@ func TestCheckAndEnforceLicense(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO dashboard_insight_view (dashboard_id, insight_view_id)
_, err = insightsDB.Exec(`INSERT INTO dashboard_insight_view (dashboard_id, insight_view_id)
VALUES (1, 2),
(2, 3),
(3, 4),
@ -73,7 +73,7 @@ func TestCheckAndEnforceLicense(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO dashboard_grants (dashboard_id, user_id, org_id, global)
_, err = insightsDB.Exec(`INSERT INTO dashboard_grants (dashboard_id, user_id, org_id, global)
VALUES (1, 1, NULL, NULL),
(2, NULL, 1, NULL),
(3, NULL, NULL, TRUE),
@ -90,7 +90,7 @@ func TestCheckAndEnforceLicense(t *testing.T) {
autogold.Want("NumFrozen", numFrozen).Equal(t, 4)
setMockLicenseCheck(true)
checkAndEnforceLicense(ctx, timescale)
checkAndEnforceLicense(ctx, insightsDB)
numFrozen, err = getNumFrozenInsights()
if err != nil {
t.Fatal(err)
@ -106,7 +106,7 @@ func TestCheckAndEnforceLicense(t *testing.T) {
autogold.Want("NumFrozen", numFrozen).Equal(t, 0)
setMockLicenseCheck(false)
checkAndEnforceLicense(ctx, timescale)
checkAndEnforceLicense(ctx, insightsDB)
numFrozen, err = getNumFrozenInsights()
if err != nil {
t.Fatal(err)
@ -128,7 +128,7 @@ func TestCheckAndEnforceLicense(t *testing.T) {
autogold.Want("NumFrozen", numFrozen).Equal(t, 4)
setMockLicenseCheck(false)
checkAndEnforceLicense(ctx, timescale)
checkAndEnforceLicense(ctx, insightsDB)
numFrozen, err = getNumFrozenInsights()
if err != nil {
t.Fatal(err)

View File

@ -26,7 +26,7 @@ import (
var _ workerutil.Handler = &workHandler{}
// workHandler implements the dbworker.Handler interface by executing search queries and
// inserting insights about them to the insights Timescale database.
// inserting insights about them to the insights database.
type workHandler struct {
baseWorkerStore *basestore.Store
insightsStore *store.Store

View File

@ -438,10 +438,10 @@ func mockComputeSearch(results []computeSearch) func(context.Context, string) ([
}
func TestGetSeries(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Date(2021, 12, 1, 0, 0, 0, 0, time.UTC).Truncate(time.Microsecond).Round(0)
metadataStore := store.NewInsightStore(timescale)
metadataStore := store.NewInsightStore(insightsDB)
metadataStore.Now = func() time.Time {
return now
}

View File

@ -13,16 +13,16 @@ import (
"github.com/sourcegraph/sourcegraph/internal/database/postgresdsn"
)
// TimescaleDB returns a handle to the Code Insights TimescaleDB instance.
// CodeInsightsDB returns a handle to the Code Insights Postgres instance.
//
// The returned DB handle is initialized with a unique database just for the specified test, with
// all migrations applied.
func TimescaleDB(t testing.TB) (db *sql.DB, cleanup func()) {
func CodeInsightsDB(t testing.TB) (db *sql.DB, cleanup func()) {
if testing.Short() {
t.Skip()
}
// Setup TimescaleDB for testing.
// Setup postgres DB for testing.
if os.Getenv("CODEINSIGHTS_PGDATASOURCE") == "" {
os.Setenv("CODEINSIGHTS_PGDATASOURCE", "postgres://postgres:password@127.0.0.1:5435/postgres")
}
@ -31,11 +31,11 @@ func TimescaleDB(t testing.TB) (db *sql.DB, cleanup func()) {
username = user.Username
}
timescaleDSN := postgresdsn.New("codeinsights", username, os.Getenv)
initConn, err := connections.NewTestDB(timescaleDSN)
insightsDSN := postgresdsn.New("codeinsights", username, os.Getenv)
initConn, err := connections.NewTestDB(insightsDSN)
if err != nil {
t.Log("")
t.Log("README: To run these tests you need to have the codeinsights TimescaleDB running:")
t.Log("README: To run these tests you need to have the codeinsights db running:")
t.Log("")
t.Log("$ ./dev/codeinsights-db.sh &")
t.Log("")
@ -61,13 +61,13 @@ func TimescaleDB(t testing.TB) (db *sql.DB, cleanup func()) {
}
// Connect to the new DB.
u, err := url.Parse(timescaleDSN)
u, err := url.Parse(insightsDSN)
if err != nil {
t.Fatal("parsing Timescale DSN", err)
t.Fatal("parsing insights DSN", err)
}
u.Path = dbname
timescaleDSN = u.String()
db, err = connections.NewTestDB(timescaleDSN, schemas.CodeInsights)
insightsDSN = u.String()
db, err = connections.NewTestDB(insightsDSN, schemas.CodeInsights)
if err != nil {
t.Fatal(err)
}

View File

@ -48,22 +48,22 @@ func Init(ctx context.Context, postgres database.DB, _ conftypes.UnifiedWatchabl
}
return nil
}
timescale, err := InitializeCodeInsightsDB("frontend")
db, err := InitializeCodeInsightsDB("frontend")
if err != nil {
return err
}
enterpriseServices.InsightsResolver = resolvers.New(timescale, postgres)
enterpriseServices.InsightsResolver = resolvers.New(db, postgres)
return nil
}
// InitializeCodeInsightsDB connects to and initializes the Code Insights Timescale DB, running
// InitializeCodeInsightsDB connects to and initializes the Code Insights Postgres DB, running
// database migrations before returning. It is safe to call from multiple services/containers (in
// which case, one's migration will win and the other caller will receive an error and should exit
// and restart until the other finishes.)
func InitializeCodeInsightsDB(app string) (*sql.DB, error) {
dsn := conf.GetServiceConnectionValueAndRestartOnChange(func(serviceConnections conftypes.ServiceConnections) string {
return serviceConnections.CodeInsightsTimescaleDSN
return serviceConnections.CodeInsightsDSN
})
db, err := connections.EnsureNewCodeInsightsDB(dsn, app, &observation.TestContext)
if err != nil {
@ -78,12 +78,12 @@ func RegisterMigrations(db database.DB, outOfBandMigrationRunner *oobmigration.R
return nil
}
timescale, err := InitializeCodeInsightsDB("worker-oobmigrator")
insightsDB, err := InitializeCodeInsightsDB("worker-oobmigrator")
if err != nil {
return err
}
insightsMigrator := migration.NewMigrator(timescale, db)
insightsMigrator := migration.NewMigrator(insightsDB, db)
// This id (14) was defined arbitrarily in this migration file: 1528395945_settings_migration_out_of_band.up.sql.
if err := outOfBandMigrationRunner.Register(14, insightsMigrator, oobmigration.MigratorOptions{Interval: 10 * time.Second}); err != nil {

View File

@ -48,10 +48,10 @@ func TestToInsightUniqueIdQuery(t *testing.T) {
}
ctx := context.Background()
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
migrator := migrator{insightStore: store.NewInsightStore(timescale)}
migrator := migrator{insightStore: store.NewInsightStore(insightsDB)}
t.Run("match on org ID", func(t *testing.T) {
want := "myInsight-org-3"
@ -61,7 +61,7 @@ func TestToInsightUniqueIdQuery(t *testing.T) {
orgIds: []int{3},
}
_, err := timescale.Exec("insert into insight_view (unique_id) values ($1);", want)
_, err := insightsDB.Exec("insert into insight_view (unique_id) values ($1);", want)
if err != nil {
t.Fatal(err)
}
@ -85,7 +85,7 @@ func TestToInsightUniqueIdQuery(t *testing.T) {
userId: 1,
}
_, err := timescale.Exec("insert into insight_view (unique_id) values ($1);", want)
_, err := insightsDB.Exec("insert into insight_view (unique_id) values ($1);", want)
if err != nil {
t.Fatal(err)
}
@ -109,12 +109,12 @@ func TestToInsightUniqueIdQuery(t *testing.T) {
orgIds: []int{3},
}
_, err := timescale.Exec("insert into insight_view (unique_id) values ($1);", want)
_, err := insightsDB.Exec("insert into insight_view (unique_id) values ($1);", want)
if err != nil {
t.Fatal(err)
}
// this one should NOT match
_, err = timescale.Exec("insert into insight_view (unique_id) values ($1);", "myInsight3-org-5")
_, err = insightsDB.Exec("insert into insight_view (unique_id) values ($1);", "myInsight3-org-5")
if err != nil {
t.Fatal(err)
}
@ -155,12 +155,12 @@ func TestCreateSpecialCaseDashboard(t *testing.T) {
}
ctx := context.Background()
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
migrator := migrator{insightStore: store.NewInsightStore(timescale), dashboardStore: store.NewDashboardStore(timescale)}
migrator := migrator{insightStore: store.NewInsightStore(insightsDB), dashboardStore: store.NewDashboardStore(insightsDB)}
newView := func(insightId string) {
_, err := timescale.Exec("insert into insight_view (unique_id) values ($1);", insightId)
_, err := insightsDB.Exec("insert into insight_view (unique_id) values ($1);", insightId)
if err != nil {
t.Fatal(err)
}

View File

@ -32,7 +32,7 @@ func TestResolver_InsightConnection(t *testing.T) {
if testing.Short() {
t.Skip()
}
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
testSetup := func(t *testing.T) (context.Context, graphqlbackend.InsightConnectionResolver) {
@ -42,7 +42,7 @@ func TestResolver_InsightConnection(t *testing.T) {
clock := func() time.Time { return now }
postgres := dbtest.NewDB(t)
resolver := newWithClock(timescale, postgres, clock)
resolver := newWithClock(insightsDB, postgres, clock)
insightMetadataStore := store.NewMockInsightMetadataStore()
insightMetadataStore.GetMappedFunc.SetDefaultReturn([]types.Insight{
@ -123,7 +123,7 @@ func TestResolver_InsightsRepoPermissions(t *testing.T) {
if testing.Short() {
t.Skip()
}
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
postgres := dbtest.NewDB(t)
@ -188,14 +188,14 @@ func TestResolver_InsightsRepoPermissions(t *testing.T) {
}
for i := 0; i < 3; i++ {
_, err = timescale.Exec(`INSERT INTO repo_names (name) VALUES ($1);`, fmt.Sprint("ignore-me-", i))
_, err = insightsDB.Exec(`INSERT INTO repo_names (name) VALUES ($1);`, fmt.Sprint("ignore-me-", i))
if err != nil {
t.Fatal(err)
}
}
// Create some timeseries data, one row in each repository
_, err = timescale.Exec(`
_, err = insightsDB.Exec(`
INSERT INTO series_points (series_id, "time", "value", metadata_id, repo_id, repo_name_id, original_repo_name_id)
VALUES
('s:087855E6A24440837303FD8A252E9893E8ABDFECA55B61AC83DA1B521906626E', $1, 5.0, null, 1, 3, 3),
@ -207,7 +207,7 @@ func TestResolver_InsightsRepoPermissions(t *testing.T) {
setUpTest := func(ctx context.Context, t *testing.T) graphqlbackend.InsightConnectionResolver {
resolver := newWithClock(timescale, postgres, clock)
resolver := newWithClock(insightsDB, postgres, clock)
insightMetadataStore := store.NewMockInsightMetadataStore()
insightMetadataStore.GetMappedFunc.SetDefaultReturn([]types.Insight{
{

View File

@ -25,9 +25,9 @@ func TestResolver_InsightSeries(t *testing.T) {
ctx := actor.WithInternalActor(context.Background())
now := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Truncate(time.Microsecond)
clock := func() time.Time { return now }
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
postgres := dbtest.NewDB(t)
resolver := newWithClock(timescale, postgres, clock)
resolver := newWithClock(insightsDB, postgres, clock)
// Create a mock store, delegating any un-mocked methods to the DB store.
dbStore := resolver.timeSeriesStore

View File

@ -84,12 +84,12 @@ func TestFrozenInsightDataSeriesResolver(t *testing.T) {
}
})
t.Run("insight_is_not_frozen_returns_real_resolvers", func(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
base := baseInsightResolver{
insightStore: store.NewInsightStore(timescale),
dashboardStore: store.NewDashboardStore(timescale),
insightsDB: timescale,
insightStore: store.NewInsightStore(insightsDB),
dashboardStore: store.NewDashboardStore(insightsDB),
insightsDB: insightsDB,
}
series, err := base.insightStore.CreateSeries(ctx, types.InsightSeries{

View File

@ -57,15 +57,15 @@ type Resolver struct {
baseInsightResolver
}
// New returns a new Resolver whose store uses the given Timescale and Postgres DBs.
func New(timescale, postgres dbutil.DB) graphqlbackend.InsightsResolver {
return newWithClock(timescale, postgres, timeutil.Now)
// New returns a new Resolver whose store uses the given Postgres DBs.
func New(db, postgres dbutil.DB) graphqlbackend.InsightsResolver {
return newWithClock(db, postgres, timeutil.Now)
}
// newWithClock returns a new Resolver whose store uses the given Timescale and Postgres DBs, and the given
// clock for timestamps.
func newWithClock(timescale, postgres dbutil.DB, clock func() time.Time) *Resolver {
base := WithBase(timescale, postgres, clock)
// newWithClock returns a new Resolver whose store uses the given Postgres DBs and the given clock
// for timestamps.
func newWithClock(db, postgres dbutil.DB, clock func() time.Time) *Resolver {
base := WithBase(db, postgres, clock)
return &Resolver{
baseInsightResolver: *base,
timeSeriesStore: base.timeSeriesStore,

View File

@ -18,10 +18,10 @@ func TestResolver_Insights(t *testing.T) {
ctx := actor.WithInternalActor(context.Background())
now := time.Now().UTC().Truncate(time.Microsecond)
clock := func() time.Time { return now }
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
postgres := dbtest.NewDB(t)
resolver := newWithClock(timescale, postgres, clock)
resolver := newWithClock(insightsDB, postgres, clock)
insightsConnection, err := resolver.Insights(ctx, nil)
if err != nil {

View File

@ -21,7 +21,7 @@ type DBDashboardStore struct {
Now func() time.Time
}
// NewDashboardStore returns a new DBDashboardStore backed by the given Timescale db.
// NewDashboardStore returns a new DBDashboardStore backed by the given Postgres db.
func NewDashboardStore(db dbutil.DB) *DBDashboardStore {
return &DBDashboardStore{Store: basestore.NewWithDB(db, sql.TxOptions{}), Now: time.Now}
}

View File

@ -13,11 +13,11 @@ import (
)
func TestGetDashboard(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Truncate(time.Microsecond).Round(0)
_, err := timescale.Exec(`
_, err := insightsDB.Exec(`
INSERT INTO dashboard (id, title)
VALUES (1, 'test dashboard'), (2, 'private dashboard for user 3');`)
if err != nil {
@ -27,33 +27,33 @@ func TestGetDashboard(t *testing.T) {
ctx := context.Background()
// assign some global grants just so the test can immediately fetch the created dashboard
_, err = timescale.Exec(`INSERT INTO dashboard_grants (dashboard_id, global)
_, err = insightsDB.Exec(`INSERT INTO dashboard_grants (dashboard_id, global)
VALUES (1, true)`)
if err != nil {
t.Fatal(err)
}
// assign a private grant
_, err = timescale.Exec(`INSERT INTO dashboard_grants (dashboard_id, user_id)
_, err = insightsDB.Exec(`INSERT INTO dashboard_grants (dashboard_id, user_id)
VALUES (2, 3)`)
if err != nil {
t.Fatal(err)
}
// assign some global grants just so the test can immediately fetch the created dashboard
_, err = timescale.Exec(`INSERT INTO insight_view (id, title, description, unique_id)
_, err = insightsDB.Exec(`INSERT INTO insight_view (id, title, description, unique_id)
VALUES (1, 'my view', 'my description', 'unique1234')`)
if err != nil {
t.Fatal(err)
}
// assign some global grants just so the test can immediately fetch the created dashboard
_, err = timescale.Exec(`INSERT INTO dashboard_insight_view (dashboard_id, insight_view_id)
_, err = insightsDB.Exec(`INSERT INTO dashboard_insight_view (dashboard_id, insight_view_id)
VALUES (1, 1)`)
if err != nil {
t.Fatal(err)
}
store := NewDashboardStore(timescale)
store := NewDashboardStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -94,11 +94,11 @@ func TestGetDashboard(t *testing.T) {
}
func TestCreateDashboard(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Truncate(time.Microsecond).Round(0)
ctx := context.Background()
store := NewDashboardStore(timescale)
store := NewDashboardStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -143,16 +143,16 @@ func TestCreateDashboard(t *testing.T) {
}
func TestUpdateDashboard(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Truncate(time.Microsecond).Round(0)
ctx := context.Background()
store := NewDashboardStore(timescale)
store := NewDashboardStore(insightsDB)
store.Now = func() time.Time {
return now
}
_, err := timescale.Exec(`
_, err := insightsDB.Exec(`
INSERT INTO dashboard (id, title)
VALUES (1, 'test dashboard 1'), (2, 'test dashboard 2');
INSERT INTO dashboard_grants (dashboard_id, global)
@ -215,12 +215,12 @@ func TestUpdateDashboard(t *testing.T) {
}
func TestDeleteDashboard(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Truncate(time.Microsecond).Round(0)
ctx := context.Background()
_, err := timescale.Exec(`
_, err := insightsDB.Exec(`
INSERT INTO dashboard (id, title)
VALUES (1, 'test dashboard 1'), (2, 'test dashboard 2');
INSERT INTO dashboard_grants (dashboard_id, global)
@ -229,7 +229,7 @@ func TestDeleteDashboard(t *testing.T) {
t.Fatal(err)
}
store := NewDashboardStore(timescale)
store := NewDashboardStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -275,12 +275,12 @@ func TestDeleteDashboard(t *testing.T) {
}
func TestAddViewsToDashboard(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Truncate(time.Microsecond).Round(0)
ctx := context.Background()
_, err := timescale.Exec(`
_, err := insightsDB.Exec(`
INSERT INTO dashboard (id, title)
VALUES (1, 'test dashboard 1'), (2, 'test dashboard 2');
INSERT INTO dashboard_grants (dashboard_id, global)
@ -289,13 +289,13 @@ func TestAddViewsToDashboard(t *testing.T) {
t.Fatal(err)
}
store := NewDashboardStore(timescale)
store := NewDashboardStore(insightsDB)
store.Now = func() time.Time {
return now
}
t.Run("create and add view to dashboard", func(t *testing.T) {
insightStore := NewInsightStore(timescale)
insightStore := NewInsightStore(insightsDB)
view1, err := insightStore.CreateView(ctx, types.InsightView{
Title: "great view",
Description: "my view",
@ -338,17 +338,17 @@ func TestAddViewsToDashboard(t *testing.T) {
}
func TestRemoveViewsFromDashboard(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Truncate(time.Microsecond).Round(0)
ctx := context.Background()
store := NewDashboardStore(timescale)
store := NewDashboardStore(insightsDB)
store.Now = func() time.Time {
return now
}
insightStore := NewInsightStore(timescale)
insightStore := NewInsightStore(insightsDB)
view, err := insightStore.CreateView(ctx, types.InsightView{
Title: "view1",
@ -430,11 +430,11 @@ func TestRemoveViewsFromDashboard(t *testing.T) {
}
func TestHasDashboardPermission(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Date(2021, 12, 1, 0, 0, 0, 0, time.UTC).Truncate(time.Microsecond).Round(0)
ctx := context.Background()
store := NewDashboardStore(timescale)
store := NewDashboardStore(insightsDB)
store.Now = func() time.Time {
return now
}

View File

@ -21,7 +21,7 @@ type InsightStore struct {
Now func() time.Time
}
// NewInsightStore returns a new InsightStore backed by the given Timescale db.
// NewInsightStore returns a new InsightStore backed by the given Postgres db.
func NewInsightStore(db dbutil.DB) *InsightStore {
return &InsightStore{Store: basestore.NewWithDB(db, sql.TxOptions{}), Now: time.Now}
}

View File

@ -18,11 +18,11 @@ import (
)
func TestGet(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Truncate(time.Microsecond).Round(0)
_, err := timescale.Exec(`INSERT INTO insight_view (id, title, description, unique_id, is_frozen)
_, err := insightsDB.Exec(`INSERT INTO insight_view (id, title, description, unique_id, is_frozen)
VALUES (1, 'test title', 'test description', 'unique-1', false),
(2, 'test title 2', 'test description 2', 'unique-2', true)`)
if err != nil {
@ -30,14 +30,14 @@ func TestGet(t *testing.T) {
}
// assign some global grants just so the test can immediately fetch the created views
_, err = timescale.Exec(`INSERT INTO insight_view_grants (insight_view_id, global)
_, err = insightsDB.Exec(`INSERT INTO insight_view_grants (insight_view_id, global)
VALUES (1, true),
(2, true)`)
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO insight_series (series_id, query, created_at, oldest_historical_at, last_recorded_at,
_, err = insightsDB.Exec(`INSERT INTO insight_series (series_id, query, created_at, oldest_historical_at, last_recorded_at,
next_recording_after, last_snapshot_at, next_snapshot_after, deleted_at, generation_method)
VALUES ('series-id-1', 'query-1', $1, $1, $1, $1, $1, $1, null, 'search'),
('series-id-2', 'query-2', $1, $1, $1, $1, $1, $1, null, 'search'),
@ -46,7 +46,7 @@ func TestGet(t *testing.T) {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO insight_view_series (insight_view_id, insight_series_id, label, stroke)
_, err = insightsDB.Exec(`INSERT INTO insight_view_series (insight_view_id, insight_series_id, label, stroke)
VALUES (1, 1, 'label1', 'color1'),
(1, 2, 'label2', 'color2'),
(2, 2, 'second-label-2', 'second-color-2'),
@ -58,7 +58,7 @@ func TestGet(t *testing.T) {
ctx := context.Background()
t.Run("test get all", func(t *testing.T) {
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
got, err := store.Get(ctx, InsightQueryArgs{})
if err != nil {
@ -137,7 +137,7 @@ func TestGet(t *testing.T) {
})
t.Run("test get by unique ids", func(t *testing.T) {
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
got, err := store.Get(ctx, InsightQueryArgs{UniqueIDs: []string{"unique-1"}})
if err != nil {
@ -194,7 +194,7 @@ func TestGet(t *testing.T) {
}
})
t.Run("test get by unique ids", func(t *testing.T) {
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
got, err := store.Get(ctx, InsightQueryArgs{UniqueID: "unique-1"})
if err != nil {
@ -253,14 +253,14 @@ func TestGet(t *testing.T) {
}
func TestGetAll(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Truncate(time.Microsecond).Round(0)
ctx := context.Background()
// First test the method on an empty database.
t.Run("test empty database", func(t *testing.T) {
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
got, err := store.GetAll(ctx, InsightQueryArgs{})
if err != nil {
t.Fatal(err)
@ -271,7 +271,7 @@ func TestGetAll(t *testing.T) {
})
// Set up some insight views to test pagination and permissions.
_, err := timescale.Exec(`INSERT INTO insight_view (id, title, description, unique_id)
_, err := insightsDB.Exec(`INSERT INTO insight_view (id, title, description, unique_id)
VALUES (1, 'user cannot view', '', 'a'),
(2, 'user can view 1', '', 'd'),
(3, 'user can view 2', '', 'e'),
@ -280,14 +280,14 @@ func TestGetAll(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO insight_series (id, series_id, query, created_at, oldest_historical_at, last_recorded_at,
_, err = insightsDB.Exec(`INSERT INTO insight_series (id, series_id, query, created_at, oldest_historical_at, last_recorded_at,
next_recording_after, last_snapshot_at, next_snapshot_after, deleted_at, generation_method)
VALUES (1, 'series-id-1', 'query-1', $1, $1, $1, $1, $1, $1, null, 'search'),
(2, 'series-id-2', 'query-2', $1, $1, $1, $1, $1, $1, null, 'search')`, now)
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO insight_view_series (insight_view_id, insight_series_id, label, stroke)
_, err = insightsDB.Exec(`INSERT INTO insight_view_series (insight_view_id, insight_series_id, label, stroke)
VALUES (1, 1, 'label1-1', 'color'),
(2, 1, 'label2-1', 'color'),
(2, 2, 'label2-2', 'color'),
@ -299,28 +299,28 @@ func TestGetAll(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO insight_view_grants (insight_view_id, global)
_, err = insightsDB.Exec(`INSERT INTO insight_view_grants (insight_view_id, global)
VALUES (2, true), (3, true)`)
if err != nil {
t.Fatal(err)
}
// Attach one of the insights to a dashboard to test insight permission via dashboard permissions.
_, err = timescale.Exec(`INSERT INTO dashboard (id, title) VALUES (1, 'dashboard 1');`)
_, err = insightsDB.Exec(`INSERT INTO dashboard (id, title) VALUES (1, 'dashboard 1');`)
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO dashboard_insight_view (dashboard_id, insight_view_id) VALUES (1, 5)`)
_, err = insightsDB.Exec(`INSERT INTO dashboard_insight_view (dashboard_id, insight_view_id) VALUES (1, 5)`)
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO dashboard_grants (dashboard_id, global) VALUES (1, true)`)
_, err = insightsDB.Exec(`INSERT INTO dashboard_grants (dashboard_id, global) VALUES (1, true)`)
if err != nil {
t.Fatal(err)
}
t.Run("test all results", func(t *testing.T) {
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
got, err := store.GetAll(ctx, InsightQueryArgs{})
if err != nil {
t.Fatal(err)
@ -433,7 +433,7 @@ func TestGetAll(t *testing.T) {
}
})
t.Run("test first result", func(t *testing.T) {
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
got, err := store.GetAll(ctx, InsightQueryArgs{Limit: 1})
if err != nil {
t.Fatal(err)
@ -486,7 +486,7 @@ func TestGetAll(t *testing.T) {
}
})
t.Run("test second result", func(t *testing.T) {
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
got, err := store.GetAll(ctx, InsightQueryArgs{Limit: 1, After: "b"})
if err != nil {
t.Fatal(err)
@ -539,7 +539,7 @@ func TestGetAll(t *testing.T) {
}
})
t.Run("test last 2 results", func(t *testing.T) {
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
got, err := store.GetAll(ctx, InsightQueryArgs{After: "b"})
if err != nil {
t.Fatal(err)
@ -614,11 +614,11 @@ func TestGetAll(t *testing.T) {
}
func TestGetAllOnDashboard(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Truncate(time.Microsecond).Round(0)
_, err := timescale.Exec(`INSERT INTO insight_view (id, title, description, unique_id)
_, err := insightsDB.Exec(`INSERT INTO insight_view (id, title, description, unique_id)
VALUES (1, 'test title', 'test description', 'unique-1'),
(2, 'test title 2', 'test description 2', 'unique-2'),
(3, 'test title 3', 'test description 3', 'unique-3'),
@ -627,7 +627,7 @@ func TestGetAllOnDashboard(t *testing.T) {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO insight_series (series_id, query, created_at, oldest_historical_at, last_recorded_at,
_, err = insightsDB.Exec(`INSERT INTO insight_series (series_id, query, created_at, oldest_historical_at, last_recorded_at,
next_recording_after, last_snapshot_at, next_snapshot_after, deleted_at, generation_method)
VALUES ('series-id-1', 'query-1', $1, $1, $1, $1, $1, $1, null, 'search'),
('series-id-2', 'query-2', $1, $1, $1, $1, $1, $1, null, 'search'),
@ -636,7 +636,7 @@ func TestGetAllOnDashboard(t *testing.T) {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO insight_view_series (insight_view_id, insight_series_id, label, stroke)
_, err = insightsDB.Exec(`INSERT INTO insight_view_series (insight_view_id, insight_series_id, label, stroke)
VALUES (1, 1, 'label1-1', 'color1'),
(2, 2, 'label2-2', 'color2'),
(3, 1, 'label3-1', 'color3'),
@ -645,12 +645,12 @@ func TestGetAllOnDashboard(t *testing.T) {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO dashboard (id, title) VALUES (1, 'dashboard 1');`)
_, err = insightsDB.Exec(`INSERT INTO dashboard (id, title) VALUES (1, 'dashboard 1');`)
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO dashboard_insight_view (dashboard_id, insight_view_id)
_, err = insightsDB.Exec(`INSERT INTO dashboard_insight_view (dashboard_id, insight_view_id)
VALUES (1, 2),
(1, 1),
(1, 4),
@ -662,7 +662,7 @@ func TestGetAllOnDashboard(t *testing.T) {
ctx := context.Background()
t.Run("test get all on dashboard", func(t *testing.T) {
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
got, err := store.GetAllOnDashboard(ctx, InsightsOnDashboardQueryArgs{DashboardID: 1})
if err != nil {
t.Fatal(err)
@ -759,7 +759,7 @@ func TestGetAllOnDashboard(t *testing.T) {
}
})
t.Run("test get first 2 on dashboard", func(t *testing.T) {
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
got, err := store.GetAllOnDashboard(ctx, InsightsOnDashboardQueryArgs{DashboardID: 1, Limit: 2})
if err != nil {
t.Fatal(err)
@ -814,7 +814,7 @@ func TestGetAllOnDashboard(t *testing.T) {
}
})
t.Run("test get after 2 on dashboard", func(t *testing.T) {
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
got, err := store.GetAllOnDashboard(ctx, InsightsOnDashboardQueryArgs{DashboardID: 1, After: "2"})
if err != nil {
t.Fatal(err)
@ -871,11 +871,11 @@ func TestGetAllOnDashboard(t *testing.T) {
}
func TestCreateSeries(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Date(2021, 5, 1, 1, 0, 0, 0, time.UTC).Truncate(time.Microsecond).Round(0)
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -923,7 +923,7 @@ func TestCreateSeries(t *testing.T) {
}
})
t.Run("test create and get capture groups series", func(t *testing.T) {
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
sampleIntervalUnit := "MONTH"
_, err := store.CreateSeries(ctx, types.InsightSeries{
SeriesID: "capture-group-1",
@ -960,12 +960,12 @@ func TestCreateSeries(t *testing.T) {
}
func TestCreateView(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Truncate(time.Microsecond).Round(0)
ctx := context.Background()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -999,12 +999,12 @@ func TestCreateView(t *testing.T) {
}
func TestCreateGetView_WithGrants(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Truncate(time.Microsecond).Round(0)
ctx := context.Background()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -1130,12 +1130,12 @@ func TestCreateGetView_WithGrants(t *testing.T) {
}
func TestUpdateView(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Truncate(time.Microsecond).Round(0)
ctx := context.Background()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -1183,12 +1183,12 @@ func TestUpdateView(t *testing.T) {
}
func TestUpdateViewSeries(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Truncate(time.Microsecond).Round(0)
ctx := context.Background()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -1243,12 +1243,12 @@ func TestUpdateViewSeries(t *testing.T) {
}
func TestDeleteView(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Truncate(time.Microsecond).Round(0)
ctx := context.Background()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -1310,12 +1310,12 @@ func TestDeleteView(t *testing.T) {
}
func TestAttachSeriesView(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Round(0).Truncate(time.Microsecond)
ctx := context.Background()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -1389,12 +1389,12 @@ func TestAttachSeriesView(t *testing.T) {
}
func TestRemoveSeriesFromView(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Round(0).Truncate(time.Microsecond)
ctx := context.Background()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -1487,12 +1487,12 @@ func TestRemoveSeriesFromView(t *testing.T) {
}
func TestInsightStore_GetDataSeries(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Round(0).Truncate(time.Microsecond)
ctx := context.Background()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -1568,12 +1568,12 @@ func TestInsightStore_GetDataSeries(t *testing.T) {
}
func TestInsightStore_StampRecording(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Date(2020, 1, 5, 0, 0, 0, 0, time.UTC).Truncate(time.Microsecond)
ctx := context.Background()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -1612,12 +1612,12 @@ func TestInsightStore_StampRecording(t *testing.T) {
}
func TestInsightStore_StampBackfill(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Round(0).Truncate(time.Microsecond)
ctx := context.Background()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -1670,12 +1670,12 @@ func TestInsightStore_StampBackfill(t *testing.T) {
}
func TestDirtyQueries(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Round(0).Truncate(time.Microsecond)
ctx := context.Background()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -1740,12 +1740,12 @@ func TestDirtyQueries(t *testing.T) {
}
func TestDirtyQueriesAggregated(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Round(0).Truncate(time.Microsecond)
ctx := context.Background()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -1815,12 +1815,12 @@ func TestDirtyQueriesAggregated(t *testing.T) {
}
func TestSetSeriesEnabled(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Date(2021, 10, 14, 0, 0, 0, 0, time.UTC).Round(0).Truncate(time.Microsecond)
ctx := context.Background()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -1880,12 +1880,12 @@ func TestSetSeriesEnabled(t *testing.T) {
}
func TestFindMatchingSeries(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Date(2021, 10, 14, 0, 0, 0, 0, time.UTC).Round(0).Truncate(time.Microsecond)
ctx := context.Background()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -1953,12 +1953,12 @@ func TestFindMatchingSeries(t *testing.T) {
}
func TestUpdateFrontendSeries(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Date(2021, 10, 14, 0, 0, 0, 0, time.UTC).Round(0).Truncate(time.Microsecond)
ctx := context.Background()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -2034,16 +2034,16 @@ func TestUpdateFrontendSeries(t *testing.T) {
}
func TestGetReferenceCount(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Now().Truncate(time.Microsecond).Round(0)
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
_, err := timescale.Exec(`INSERT INTO insight_view (id, title, description, unique_id)
_, err := insightsDB.Exec(`INSERT INTO insight_view (id, title, description, unique_id)
VALUES (1, 'test title', 'test description', 'unique-1'),
(2, 'test title 2', 'test description 2', 'unique-2'),
(3, 'test title 3', 'test description 3', 'unique-3')`)
@ -2051,13 +2051,13 @@ func TestGetReferenceCount(t *testing.T) {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO dashboard (id, title)
_, err = insightsDB.Exec(`INSERT INTO dashboard (id, title)
VALUES (1, 'dashboard 1'), (2, 'dashboard 2'), (3, 'dashboard 3');`)
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO dashboard_insight_view (dashboard_id, insight_view_id)
_, err = insightsDB.Exec(`INSERT INTO dashboard_insight_view (dashboard_id, insight_view_id)
VALUES (1, 1),
(2, 1),
(3, 1),
@ -2092,12 +2092,12 @@ func TestGetReferenceCount(t *testing.T) {
}
func TestGetSoftDeletedSeries(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
now := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Truncate(time.Microsecond).Round(0)
ctx := context.Background()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
store.Now = func() time.Time {
return now
}
@ -2133,9 +2133,9 @@ func TestGetSoftDeletedSeries(t *testing.T) {
}
func TestGetUnfrozenInsightCount(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
ctx := context.Background()
t.Run("returns 0 if there are no insights", func(t *testing.T) {
@ -2147,7 +2147,7 @@ func TestGetUnfrozenInsightCount(t *testing.T) {
autogold.Want("TotalCount", totalCount).Equal(t, 0)
})
t.Run("returns count for unfrozen insights not attached to dashboards", func(t *testing.T) {
_, err := timescale.Exec(`INSERT INTO insight_view (id, title, description, unique_id, is_frozen)
_, err := insightsDB.Exec(`INSERT INTO insight_view (id, title, description, unique_id, is_frozen)
VALUES (1, 'unattached insight', 'test description', 'unique-1', false)`)
if err != nil {
t.Fatal(err)
@ -2161,7 +2161,7 @@ func TestGetUnfrozenInsightCount(t *testing.T) {
autogold.Want("TotalCount", totalCount).Equal(t, 1)
})
t.Run("returns correct counts for unfrozen insights", func(t *testing.T) {
_, err := timescale.Exec(`INSERT INTO insight_view (id, title, description, unique_id, is_frozen)
_, err := insightsDB.Exec(`INSERT INTO insight_view (id, title, description, unique_id, is_frozen)
VALUES (2, 'private insight 2', 'test description', 'unique-2', true),
(3, 'org insight 1', 'test description', 'unique-3', false),
(4, 'global insight 1', 'test description', 'unique-4', false),
@ -2170,7 +2170,7 @@ func TestGetUnfrozenInsightCount(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO dashboard (id, title)
_, err = insightsDB.Exec(`INSERT INTO dashboard (id, title)
VALUES (1, 'private dashboard 1'),
(2, 'org dashboard 1'),
(3, 'global dashboard 1'),
@ -2178,7 +2178,7 @@ func TestGetUnfrozenInsightCount(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO dashboard_insight_view (dashboard_id, insight_view_id)
_, err = insightsDB.Exec(`INSERT INTO dashboard_insight_view (dashboard_id, insight_view_id)
VALUES (1, 2),
(2, 3),
(3, 4),
@ -2187,7 +2187,7 @@ func TestGetUnfrozenInsightCount(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO dashboard_grants (id, dashboard_id, user_id, org_id, global)
_, err = insightsDB.Exec(`INSERT INTO dashboard_grants (id, dashboard_id, user_id, org_id, global)
VALUES (1, 1, 1, NULL, NULL),
(2, 2, NULL, 1, NULL),
(3, 3, NULL, NULL, TRUE),
@ -2206,9 +2206,9 @@ func TestGetUnfrozenInsightCount(t *testing.T) {
}
func TestUnfreezeGlobalInsights(t *testing.T) {
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
store := NewInsightStore(timescale)
store := NewInsightStore(insightsDB)
ctx := context.Background()
t.Run("does nothing if there are no insights", func(t *testing.T) {
@ -2224,14 +2224,14 @@ func TestUnfreezeGlobalInsights(t *testing.T) {
autogold.Want("TotalCount", totalCount).Equal(t, 0)
})
t.Run("does not unfreeze anything if there are no global insights", func(t *testing.T) {
_, err := timescale.Exec(`INSERT INTO insight_view (id, title, description, unique_id, is_frozen)
_, err := insightsDB.Exec(`INSERT INTO insight_view (id, title, description, unique_id, is_frozen)
VALUES (1, 'private insight 1', 'test description', 'unique-1', true),
(2, 'org insight 1', 'test description', 'unique-2', true),
(3, 'unattached insight', 'test description', 'unique-3', true);`)
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO dashboard (id, title)
_, err = insightsDB.Exec(`INSERT INTO dashboard (id, title)
VALUES (1, 'private dashboard 1'),
(2, 'org dashboard 1'),
(3, 'global dashboard 1'),
@ -2239,13 +2239,13 @@ func TestUnfreezeGlobalInsights(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO dashboard_insight_view (dashboard_id, insight_view_id)
_, err = insightsDB.Exec(`INSERT INTO dashboard_insight_view (dashboard_id, insight_view_id)
VALUES (1, 1),
(2, 2);`)
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO dashboard_grants (id, dashboard_id, user_id, org_id, global)
_, err = insightsDB.Exec(`INSERT INTO dashboard_grants (id, dashboard_id, user_id, org_id, global)
VALUES (1, 1, 1, NULL, NULL),
(2, 2, NULL, 1, NULL),
(3, 3, NULL, NULL, TRUE),
@ -2267,14 +2267,14 @@ func TestUnfreezeGlobalInsights(t *testing.T) {
autogold.Want("TotalCount", totalCount).Equal(t, 0)
})
t.Run("unfreezes 2 global insights", func(t *testing.T) {
_, err := timescale.Exec(`INSERT INTO insight_view (id, title, description, unique_id, is_frozen)
_, err := insightsDB.Exec(`INSERT INTO insight_view (id, title, description, unique_id, is_frozen)
VALUES (4, 'global insight 1', 'test description', 'unique-4', true),
(5, 'global insight 2', 'test description', 'unique-5', true),
(6, 'global insight 3', 'test description', 'unique-6', true)`)
if err != nil {
t.Fatal(err)
}
_, err = timescale.Exec(`INSERT INTO dashboard_insight_view (dashboard_id, insight_view_id)
_, err = insightsDB.Exec(`INSERT INTO dashboard_insight_view (dashboard_id, insight_view_id)
VALUES (3, 4),
(3, 5),
(4, 6);`)

View File

@ -49,7 +49,7 @@ func (s *Store) Transact(ctx context.Context) (*Store, error) {
}, nil
}
// New returns a new Store backed by the given Timescale db.
// New returns a new Store backed by the given Postgres db.
func New(db dbutil.DB, permStore InsightPermissionStore) *Store {
return NewWithClock(db, permStore, timeutil.Now)
}

View File

@ -29,12 +29,12 @@ func TestSeriesPoints(t *testing.T) {
ctx := context.Background()
clock := timeutil.Now
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
postgres := dbtest.NewDB(t)
permStore := NewInsightPermissionStore(postgres)
store := NewWithClock(timescale, permStore, clock)
store := NewWithClock(insightsDB, permStore, clock)
// Confirm we get no results initially.
points, err := store.SeriesPoints(ctx, SeriesPointsOpts{})
@ -44,7 +44,7 @@ func TestSeriesPoints(t *testing.T) {
autogold.Want("SeriesPoints", []SeriesPoint{}).Equal(t, points)
// Insert some fake data.
_, err = timescale.Exec(`
_, err = insightsDB.Exec(`
INSERT INTO repo_names(name) VALUES ('github.com/gorilla/mux-original');
INSERT INTO repo_names(name) VALUES ('github.com/gorilla/mux-renamed');
INSERT INTO metadata(metadata) VALUES ('{"hello": "world", "languages": ["Go", "Python", "Java"]}');
@ -139,11 +139,11 @@ func TestCountData(t *testing.T) {
ctx := context.Background()
clock := timeutil.Now
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
postgres := dbtest.NewDB(t)
permStore := NewInsightPermissionStore(postgres)
store := NewWithClock(timescale, permStore, clock)
store := NewWithClock(insightsDB, permStore, clock)
timeValue := func(s string) time.Time {
v, err := time.Parse(time.RFC3339, s)
@ -237,11 +237,11 @@ func TestRecordSeriesPoints(t *testing.T) {
ctx := context.Background()
clock := timeutil.Now
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
postgres := dbtest.NewDB(t)
permStore := NewInsightPermissionStore(postgres)
store := NewWithClock(timescale, permStore, clock)
store := NewWithClock(insightsDB, permStore, clock)
optionalString := func(v string) *string { return &v }
optionalRepoID := func(v api.RepoID) *api.RepoID { return &v }
@ -341,11 +341,11 @@ func TestRecordSeriesPointsSnapshotOnly(t *testing.T) {
ctx := context.Background()
clock := timeutil.Now
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
postgres := dbtest.NewDB(t)
permStore := NewInsightPermissionStore(postgres)
store := NewWithClock(timescale, permStore, clock)
store := NewWithClock(insightsDB, permStore, clock)
optionalString := func(v string) *string { return &v }
optionalRepoID := func(v api.RepoID) *api.RepoID { return &v }
@ -407,11 +407,11 @@ func TestRecordSeriesPointsRecordingOnly(t *testing.T) {
ctx := context.Background()
clock := timeutil.Now
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
postgres := dbtest.NewDB(t)
permStore := NewInsightPermissionStore(postgres)
store := NewWithClock(timescale, permStore, clock)
store := NewWithClock(insightsDB, permStore, clock)
optionalString := func(v string) *string { return &v }
optionalRepoID := func(v api.RepoID) *api.RepoID { return &v }
@ -473,11 +473,11 @@ func TestDeleteSnapshots(t *testing.T) {
ctx := context.Background()
clock := timeutil.Now
timescale, cleanup := insightsdbtesting.TimescaleDB(t)
insightsDB, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
postgres := dbtest.NewDB(t)
permStore := NewInsightPermissionStore(postgres)
store := NewWithClock(timescale, permStore, clock)
store := NewWithClock(insightsDB, permStore, clock)
optionalString := func(v string) *string { return &v }
optionalRepoID := func(v api.RepoID) *api.RepoID { return &v }
@ -555,7 +555,7 @@ func TestDelete(t *testing.T) {
ctx := context.Background()
clock := timeutil.Now
insightsdb, cleanup := insightsdbtesting.TimescaleDB(t)
insightsdb, cleanup := insightsdbtesting.CodeInsightsDB(t)
defer cleanup()
repoName := "reallygreatrepo"

View File

@ -20,10 +20,10 @@ type ServiceConnections struct {
// eg: "postgres://sg@pgsql/sourcegraph_codeintel?sslmode=false"
CodeIntelPostgresDSN string `json:"codeIntelPostgresDSN"`
// CodeInsightsTimescaleDSN is the TimescaleDB data source name for the
// CodeInsightsDSN is the PostgreSQL DB data source name for the
// code insights database.
// eg: "postgres://sg@pgsql/sourcegraph_codeintel?sslmode=false"
CodeInsightsTimescaleDSN string `json:"codeinsightsTimescaleDSN"`
CodeInsightsDSN string `json:"codeInsightsPostgresDSN"`
}
// RawUnified is the unparsed variant of conf.Unified.

View File

@ -4,7 +4,7 @@ The children of this directory contain migrations for each Postgres database ins
- `frontend` is the main database (things should go here unless there is a good reason)
- `codeintel` is a database containing only processed LSIF data (which can become extremely large)
- `codeinsights` is a TimescaleDB database, containing only Code Insights time series data
- `codeinsights` is a database containing only Code Insights time series data
The migration path for each database instance is the same and is described below. Each of the database instances described here are deployed separately, but are designed to be _overlayable_ to reduce friction during development. That is, we assume that the names in each database do not overlap so that the same connection parameters can be used for both database instances.

View File

@ -101,8 +101,7 @@ env:
# This setting will be going away soon
DISABLE_CNCF: notonmybox
# Code Insights uses a separate database, because it's easier to run TimescaleDB in
# Docker than install as a Postgres extension in dev environments.
# Code Insights uses a separate database to architecturally isolate the component from the rest of Sourcegraph.
CODEINSIGHTS_PGDATASOURCE: postgres://postgres:password@127.0.0.1:5435/postgres
DB_STARTUP_TIMEOUT: 120s # codeinsights-db needs more time to start in some instances.
DISABLE_CODE_INSIGHTS_HISTORICAL: true