database: remove dbutil-based constructors (#36210)

This commit is contained in:
Joe Chen 2022-05-31 07:48:20 +08:00 committed by GitHub
parent e93dcd9d02
commit 62b66d9da9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 99 additions and 131 deletions

View File

@ -81,7 +81,7 @@ func (r *siteResolver) settingsSubject() api.SettingsSubject {
}
func (r *siteResolver) LatestSettings(ctx context.Context) (*settingsResolver, error) {
settings, err := database.Settings(r.db).GetLatest(ctx, r.settingsSubject())
settings, err := r.db.Settings().GetLatest(ctx, r.settingsSubject())
if err != nil {
return nil, err
}

View File

@ -140,7 +140,7 @@ func overrideGlobalSettings(ctx context.Context, db database.DB) error {
if path == "" {
return nil
}
settings := database.Settings(db)
settings := db.Settings()
update := func(ctx context.Context) error {
globalSettingsBytes, err := os.ReadFile(path)
if err != nil {

View File

@ -193,7 +193,7 @@ func Main(enterpriseSetupHook func(db database.DB, c conftypes.UnifiedWatchable)
// Run enterprise setup hook
enterprise := enterpriseSetupHook(db, conf.DefaultClient())
authz.DefaultSubRepoPermsChecker, err = authz.NewSubRepoPermsClient(database.SubRepoPerms(db))
authz.DefaultSubRepoPermsChecker, err = authz.NewSubRepoPermsClient(db.SubRepoPerms())
if err != nil {
return errors.Wrap(err, "Failed to create sub-repo client")
}

View File

@ -163,7 +163,7 @@ func serveSettingsGetForSubject(db database.DB) func(w http.ResponseWriter, r *h
if err := json.NewDecoder(r.Body).Decode(&subject); err != nil {
return errors.Wrap(err, "Decode")
}
settings, err := database.Settings(db).GetLatest(r.Context(), subject)
settings, err := db.Settings().GetLatest(r.Context(), subject)
if err != nil {
return errors.Wrap(err, "Settings.GetLatest")
}

View File

@ -27,7 +27,7 @@ var initDatabaseMemo = memo.NewMemoizedConstructor(func() (*sql.DB, error) {
return nil, errors.Errorf("failed to connect to frontend database: %s", err)
}
authz.DefaultSubRepoPermsChecker, err = authz.NewSubRepoPermsClient(database.SubRepoPerms(db))
authz.DefaultSubRepoPermsChecker, err = authz.NewSubRepoPermsClient(database.NewDB(db).SubRepoPerms())
if err != nil {
return nil, errors.Errorf("Failed to create sub-repo client: %v", err)
}

View File

@ -395,7 +395,7 @@ func (r *searchContextResolver) Repositories(ctx context.Context) ([]graphqlback
return []graphqlbackend.SearchContextRepositoryRevisionsResolver{}, nil
}
repoRevs, err := database.SearchContexts(r.db).GetSearchContextRepositoryRevisions(ctx, r.sc.ID)
repoRevs, err := r.db.SearchContexts().GetSearchContextRepositoryRevisions(ctx, r.sc.ID)
if err != nil {
return nil, err
}

View File

@ -88,7 +88,7 @@ func main() {
}
// Connect to databases
db := mustInitializeDB()
db := database.NewDB(mustInitializeDB())
codeIntelDB := mustInitializeCodeIntelDB()
// Migrations may take a while, but after they're done we'll immediately
@ -100,7 +100,7 @@ func main() {
dbStore := dbstore.NewWithDB(db, observationContext)
workerStore := dbstore.WorkerutilUploadStore(dbStore, makeObservationContext(observationContext, false))
lsifStore := lsifstore.NewStore(codeIntelDB, conf.Get(), observationContext)
gitserverClient := gitserver.New(database.NewDB(db), dbStore, observationContext)
gitserverClient := gitserver.New(db, dbStore, observationContext)
uploadStore, err := lsifuploadstore.New(context.Background(), config.LSIFUploadStoreConfig, observationContext)
if err != nil {
@ -111,7 +111,7 @@ func main() {
}
// Initialize sub-repo permissions client
authz.DefaultSubRepoPermsChecker, err = authz.NewSubRepoPermsClient(database.SubRepoPerms(db))
authz.DefaultSubRepoPermsChecker, err = authz.NewSubRepoPermsClient(db.SubRepoPerms())
if err != nil {
logger.Fatal("Failed to create sub-repo client", log.Error(err))
}

View File

@ -37,7 +37,7 @@ func (s *insightsJob) Routines(ctx context.Context, logger log.Logger) ([]gorout
return nil, err
}
authz.DefaultSubRepoPermsChecker, err = authz.NewSubRepoPermsClient(database.SubRepoPerms(mainAppDb))
authz.DefaultSubRepoPermsChecker, err = authz.NewSubRepoPermsClient(database.NewDB(mainAppDb).SubRepoPerms())
if err != nil {
return nil, errors.Errorf("Failed to create sub-repo client: %v", err)
}

View File

@ -51,7 +51,7 @@ func (s *insightsQueryRunnerJob) Routines(ctx context.Context, logger log.Logger
return nil, err
}
authz.DefaultSubRepoPermsChecker, err = authz.NewSubRepoPermsClient(database.SubRepoPerms(mainAppDb))
authz.DefaultSubRepoPermsChecker, err = authz.NewSubRepoPermsClient(database.NewDB(mainAppDb).SubRepoPerms())
if err != nil {
return nil, errors.Errorf("Failed to create sub-repo client: %v", err)
}

View File

@ -71,7 +71,7 @@ func GetBackgroundJobs(ctx context.Context, logger log.Logger, mainAppDB *sql.DB
enableSync, _ := strconv.ParseBool(os.Getenv("ENABLE_CODE_INSIGHTS_SETTINGS_STORAGE"))
if enableSync {
observationContext.Logger.Info("Enabling Code Insights Settings Storage - This is a deprecated functionality!")
routines = append(routines, discovery.NewMigrateSettingInsightsJob(ctx, mainAppDB, insightsDB))
routines = append(routines, discovery.NewMigrateSettingInsightsJob(ctx, database.NewDB(mainAppDB), insightsDB))
}
routines = append(
routines,

View File

@ -10,6 +10,7 @@ import (
"github.com/sourcegraph/sourcegraph/enterprise/internal/insights/store"
"github.com/sourcegraph/sourcegraph/enterprise/internal/insights/types"
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/database"
"github.com/sourcegraph/sourcegraph/internal/database/dbutil"
"github.com/sourcegraph/sourcegraph/internal/goroutine"
"github.com/sourcegraph/sourcegraph/internal/insights"
@ -124,13 +125,13 @@ func filterByIds(ids []string, insight []insights.SearchInsight) []insights.Sear
}
type settingMigrator struct {
base dbutil.DB
base database.DB
insights dbutil.DB
}
// NewMigrateSettingInsightsJob will migrate insights from settings into the database. This is a job that will be
// deprecated as soon as this functionality is available over an API.
func NewMigrateSettingInsightsJob(ctx context.Context, base dbutil.DB, insights dbutil.DB) goroutine.BackgroundRoutine {
func NewMigrateSettingInsightsJob(ctx context.Context, base database.DB, insights dbutil.DB) goroutine.BackgroundRoutine {
interval := time.Minute * 10
m := settingMigrator{
base: base,

View File

@ -45,7 +45,7 @@ func NewMigrator(insightsDB dbutil.DB, postgresDB database.DB) oobmigration.Migr
insightsDB: insightsDB,
postgresDB: postgresDB,
settingsMigrationJobsStore: store.NewSettingsMigrationJobsStore(postgresDB),
settingsStore: database.Settings(postgresDB),
settingsStore: postgresDB.Settings(),
insightStore: store.NewInsightStore(insightsDB),
dashboardStore: store.NewDashboardStore(insightsDB),
orgStore: postgresDB.Orgs(),

View File

@ -176,7 +176,7 @@ func (d *db) SubRepoPerms() SubRepoPermsStore {
}
func (d *db) TemporarySettings() TemporarySettingsStore {
return &temporarySettingsStore{Store: basestore.NewWithHandle(d.Store.Handle())}
return TemporarySettingsWith(d.Store)
}
func (d *db) UserCredentials(key encryption.Key) UserCredentialsStore {

View File

@ -73,11 +73,11 @@ func TestEventLogs_CountUsersWithSetting(t *testing.T) {
t.Skip()
}
t.Parallel()
db := dbtest.NewDB(t)
db := NewDB(dbtest.NewDB(t))
ctx := context.Background()
usersStore := Users(db)
settingsStore := TemporarySettings(db)
settingsStore := db.TemporarySettings()
eventLogsStore := &eventLogStore{Store: basestore.NewWithDB(db, sql.TxOptions{})}
for i := 0; i < 24; i++ {

View File

@ -764,7 +764,7 @@ func TestRepos_List_LastChanged(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = SearchContexts(db).CreateSearchContextWithRepositoryRevisions(ctx, &types.SearchContext{Name: name}, revs)
_, err = db.SearchContexts().CreateSearchContextWithRepositoryRevisions(ctx, &types.SearchContext{Name: name}, revs)
if err != nil {
t.Fatal(err)
}

View File

@ -9,7 +9,6 @@ import (
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/database/basestore"
"github.com/sourcegraph/sourcegraph/internal/database/dbutil"
"github.com/sourcegraph/sourcegraph/internal/trace"
"github.com/sourcegraph/sourcegraph/internal/types"
"github.com/sourcegraph/sourcegraph/lib/errors"
@ -33,12 +32,7 @@ type savedSearchStore struct {
*basestore.Store
}
// SavedSearches instantiates and returns a new SavedSearchStore with prepared statements.
func SavedSearches(db dbutil.DB) SavedSearchStore {
return &savedSearchStore{Store: basestore.NewWithDB(db, sql.TxOptions{})}
}
// NewSavedSearchStoreWithDB instantiates and returns a new SavedSearchStore using the other store handle.
// SavedSearchesWith instantiates and returns a new SavedSearchStore using the other store handle.
func SavedSearchesWith(other basestore.ShareableStore) SavedSearchStore {
return &savedSearchStore{Store: basestore.NewWithHandle(other.Handle())}
}

View File

@ -20,7 +20,7 @@ func TestSavedSearchesIsEmpty(t *testing.T) {
t.Parallel()
db := NewDB(dbtest.NewDB(t))
ctx := context.Background()
isEmpty, err := SavedSearches(db).IsEmpty(ctx)
isEmpty, err := db.SavedSearches().IsEmpty(ctx)
if err != nil {
t.Fatal()
}
@ -40,12 +40,12 @@ func TestSavedSearchesIsEmpty(t *testing.T) {
UserID: &userID,
OrgID: nil,
}
_, err = SavedSearches(db).Create(ctx, fake)
_, err = db.SavedSearches().Create(ctx, fake)
if err != nil {
t.Fatal(err)
}
isEmpty, err = SavedSearches(db).IsEmpty(ctx)
isEmpty, err = db.SavedSearches().IsEmpty(ctx)
if err != nil {
t.Fatal(err)
}
@ -74,7 +74,7 @@ func TestSavedSearchesCreate(t *testing.T) {
UserID: &userID,
OrgID: nil,
}
ss, err := SavedSearches(db).Create(ctx, fake)
ss, err := db.SavedSearches().Create(ctx, fake)
if err != nil {
t.Fatal(err)
}
@ -113,7 +113,7 @@ func TestSavedSearchesUpdate(t *testing.T) {
UserID: &userID,
OrgID: nil,
}
_, err = SavedSearches(db).Create(ctx, fake)
_, err = db.SavedSearches().Create(ctx, fake)
if err != nil {
t.Fatal(err)
}
@ -126,7 +126,7 @@ func TestSavedSearchesUpdate(t *testing.T) {
OrgID: nil,
}
updatedSearch, err := SavedSearches(db).Update(ctx, updated)
updatedSearch, err := db.SavedSearches().Update(ctx, updated)
if err != nil {
t.Fatal(err)
}
@ -155,17 +155,17 @@ func TestSavedSearchesDelete(t *testing.T) {
UserID: &userID,
OrgID: nil,
}
_, err = SavedSearches(db).Create(ctx, fake)
_, err = db.SavedSearches().Create(ctx, fake)
if err != nil {
t.Fatal(err)
}
err = SavedSearches(db).Delete(ctx, 1)
err = db.SavedSearches().Delete(ctx, 1)
if err != nil {
t.Fatal(err)
}
allQueries, err := SavedSearches(db).ListAll(ctx)
allQueries, err := db.SavedSearches().ListAll(ctx)
if err != nil {
t.Fatal(err)
}
@ -194,7 +194,7 @@ func TestSavedSearchesGetByUserID(t *testing.T) {
UserID: &userID,
OrgID: nil,
}
ss, err := SavedSearches(db).Create(ctx, fake)
ss, err := db.SavedSearches().Create(ctx, fake)
if err != nil {
t.Fatal(err)
}
@ -202,7 +202,7 @@ func TestSavedSearchesGetByUserID(t *testing.T) {
if ss == nil {
t.Fatalf("no saved search returned, create failed")
}
savedSearch, err := SavedSearches(db).ListSavedSearchesByUserID(ctx, 1)
savedSearch, err := db.SavedSearches().ListSavedSearchesByUserID(ctx, 1)
if err != nil {
t.Fatal(err)
}
@ -237,7 +237,7 @@ func TestSavedSearchesGetByID(t *testing.T) {
UserID: &userID,
OrgID: nil,
}
ss, err := SavedSearches(db).Create(ctx, fake)
ss, err := db.SavedSearches().Create(ctx, fake)
if err != nil {
t.Fatal(err)
}
@ -245,7 +245,7 @@ func TestSavedSearchesGetByID(t *testing.T) {
if ss == nil {
t.Fatalf("no saved search returned, create failed")
}
savedSearch, err := SavedSearches(db).GetByID(ctx, ss.ID)
savedSearch, err := db.SavedSearches().GetByID(ctx, ss.ID)
if err != nil {
t.Fatal(err)
}
@ -281,7 +281,7 @@ func TestListSavedSearchesByUserID(t *testing.T) {
UserID: &userID,
OrgID: nil,
}
ss, err := SavedSearches(db).Create(ctx, fake)
ss, err := db.SavedSearches().Create(ctx, fake)
if err != nil {
t.Fatal(err)
}
@ -305,7 +305,7 @@ func TestListSavedSearchesByUserID(t *testing.T) {
UserID: nil,
OrgID: &org1.ID,
}
orgSearch, err := SavedSearches(db).Create(ctx, orgFake)
orgSearch, err := db.SavedSearches().Create(ctx, orgFake)
if err != nil {
t.Fatal(err)
}
@ -319,7 +319,7 @@ func TestListSavedSearchesByUserID(t *testing.T) {
UserID: nil,
OrgID: &org2.ID,
}
org2Search, err := SavedSearches(db).Create(ctx, org2Fake)
org2Search, err := db.SavedSearches().Create(ctx, org2Fake)
if err != nil {
t.Fatal(err)
}
@ -336,7 +336,7 @@ func TestListSavedSearchesByUserID(t *testing.T) {
t.Fatal(err)
}
savedSearches, err := SavedSearches(db).ListSavedSearchesByUserID(ctx, userID)
savedSearches, err := db.SavedSearches().ListSavedSearchesByUserID(ctx, userID)
if err != nil {
t.Fatal(err)
}

View File

@ -19,11 +19,6 @@ import (
var ErrSearchContextNotFound = errors.New("search context not found")
func SearchContexts(db dbutil.DB) SearchContextsStore {
store := basestore.NewWithDB(db, sql.TxOptions{})
return &searchContextsStore{store}
}
func SearchContextsWith(other basestore.ShareableStore) SearchContextsStore {
return &searchContextsStore{basestore.NewWithHandle(other.Handle())}
}

View File

@ -33,7 +33,7 @@ func TestSearchContexts_Get(t *testing.T) {
ctx := actor.WithInternalActor(context.Background())
u := Users(db)
o := db.Orgs()
sc := SearchContexts(db)
sc := db.SearchContexts()
user, err := u.Create(ctx, NewUser{Username: "u", Password: "p"})
if err != nil {
@ -85,7 +85,7 @@ func TestSearchContexts_Update(t *testing.T) {
ctx := actor.WithInternalActor(context.Background())
u := Users(db)
o := db.Orgs()
sc := SearchContexts(db)
sc := db.SearchContexts()
user, err := u.Create(ctx, NewUser{Username: "u", Password: "p"})
if err != nil {
@ -156,7 +156,7 @@ func TestSearchContexts_List(t *testing.T) {
t.Parallel()
ctx := actor.WithInternalActor(context.Background())
u := Users(db)
sc := SearchContexts(db)
sc := db.SearchContexts()
user, err := u.Create(ctx, NewUser{Username: "u", Password: "p"})
if err != nil {
@ -204,7 +204,7 @@ func TestSearchContexts_PaginationAndCount(t *testing.T) {
ctx := actor.WithInternalActor(context.Background())
u := Users(db)
o := db.Orgs()
sc := SearchContexts(db)
sc := db.SearchContexts()
user, err := u.Create(ctx, NewUser{Username: "u", Password: "p"})
if err != nil {
@ -302,7 +302,7 @@ func TestSearchContexts_CaseInsensitiveNames(t *testing.T) {
ctx := actor.WithInternalActor(context.Background())
u := Users(db)
o := db.Orgs()
sc := SearchContexts(db)
sc := db.SearchContexts()
user, err := u.Create(ctx, NewUser{Username: "u", Password: "p"})
if err != nil {
@ -361,7 +361,7 @@ func TestSearchContexts_CreateAndSetRepositoryRevisions(t *testing.T) {
db := NewDB(dbtest.NewDB(t))
t.Parallel()
ctx := actor.WithInternalActor(context.Background())
sc := SearchContexts(db)
sc := db.SearchContexts()
r := db.Repos()
err := r.Create(ctx, &types.Repo{Name: "testA", URI: "https://example.com/a"}, &types.Repo{Name: "testB", URI: "https://example.com/b"})
@ -426,7 +426,7 @@ func TestSearchContexts_Permissions(t *testing.T) {
u := Users(db)
o := db.Orgs()
om := db.OrgMembers()
sc := SearchContexts(db)
sc := db.SearchContexts()
user1, err := u.Create(internalCtx, NewUser{Username: "u1", Password: "p"})
if err != nil {
@ -626,7 +626,7 @@ func TestSearchContexts_Delete(t *testing.T) {
db := NewDB(dbtest.NewDB(t))
t.Parallel()
ctx := context.Background()
sc := SearchContexts(db)
sc := db.SearchContexts()
initialSearchContexts, err := createSearchContexts(ctx, sc, []*types.SearchContext{
{Name: "ctx", Public: true},
@ -679,7 +679,7 @@ func TestSearchContexts_OrderBy(t *testing.T) {
u := Users(db)
o := db.Orgs()
om := db.OrgMembers()
sc := SearchContexts(db)
sc := db.SearchContexts()
user1, err := u.Create(internalCtx, NewUser{Username: "u1", Password: "p"})
if err != nil {
@ -789,7 +789,7 @@ func TestSearchContexts_GetAllRevisionsForRepos(t *testing.T) {
t.Parallel()
// Required for this DB query.
internalCtx := actor.WithInternalActor(context.Background())
sc := SearchContexts(db)
sc := db.SearchContexts()
r := db.Repos()
repos := []*types.Repo{

View File

@ -11,7 +11,6 @@ import (
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/database/basestore"
"github.com/sourcegraph/sourcegraph/internal/database/dbutil"
"github.com/sourcegraph/sourcegraph/internal/jsonc"
"github.com/sourcegraph/sourcegraph/internal/trace"
"github.com/sourcegraph/sourcegraph/lib/errors"
@ -33,12 +32,7 @@ type settingsStore struct {
*basestore.Store
}
// Settings instantiates and returns a new SettingStore with prepared statements.
func Settings(db dbutil.DB) SettingsStore {
return &settingsStore{Store: basestore.NewWithDB(db, sql.TxOptions{})}
}
// NewSettingStoreWithDB instantiates and returns a new SettingStore using the other store handle.
// SettingsWith instantiates and returns a new SettingsStore using the other store handle.
func SettingsWith(other basestore.ShareableStore) SettingsStore {
return &settingsStore{Store: basestore.NewWithHandle(other.Handle())}
}

View File

@ -13,7 +13,7 @@ func TestSettings_ListAll(t *testing.T) {
t.Skip()
}
t.Parallel()
db := dbtest.NewDB(t)
db := NewDB(dbtest.NewDB(t))
ctx := context.Background()
user1, err := Users(db).Create(ctx, NewUser{Username: "u1"})
@ -26,15 +26,15 @@ func TestSettings_ListAll(t *testing.T) {
}
// Try creating both with non-nil author and nil author.
if _, err := Settings(db).CreateIfUpToDate(ctx, api.SettingsSubject{User: &user1.ID}, nil, &user1.ID, `{"abc": 1}`); err != nil {
if _, err := db.Settings().CreateIfUpToDate(ctx, api.SettingsSubject{User: &user1.ID}, nil, &user1.ID, `{"abc": 1}`); err != nil {
t.Fatal(err)
}
if _, err := Settings(db).CreateIfUpToDate(ctx, api.SettingsSubject{User: &user2.ID}, nil, nil, `{"xyz": 2}`); err != nil {
if _, err := db.Settings().CreateIfUpToDate(ctx, api.SettingsSubject{User: &user2.ID}, nil, nil, `{"xyz": 2}`); err != nil {
t.Fatal(err)
}
t.Run("all", func(t *testing.T) {
settings, err := Settings(db).ListAll(ctx, "")
settings, err := db.Settings().ListAll(ctx, "")
if err != nil {
t.Fatal(err)
}
@ -44,7 +44,7 @@ func TestSettings_ListAll(t *testing.T) {
})
t.Run("impreciseSubstring", func(t *testing.T) {
settings, err := Settings(db).ListAll(ctx, "xyz")
settings, err := db.Settings().ListAll(ctx, "xyz")
if err != nil {
t.Fatal(err)
}
@ -59,7 +59,7 @@ func TestSettings_ListAll(t *testing.T) {
func TestCreateIfUpToDate(t *testing.T) {
t.Parallel()
db := dbtest.NewDB(t)
db := NewDB(dbtest.NewDB(t))
ctx := context.Background()
u, err := Users(db).Create(ctx, NewUser{Username: "test"})
if err != nil {
@ -69,7 +69,7 @@ func TestCreateIfUpToDate(t *testing.T) {
t.Run("quicklink with safe link", func(t *testing.T) {
contents := "{\"quicklinks\": [{\"name\": \"malicious link test\", \"url\": \"https://example.com\"}]}"
_, err := Settings(db).CreateIfUpToDate(ctx, api.SettingsSubject{User: &u.ID}, nil, nil, contents)
_, err := db.Settings().CreateIfUpToDate(ctx, api.SettingsSubject{User: &u.ID}, nil, nil, contents)
if err != nil {
t.Fatal(err)
}
@ -80,7 +80,7 @@ func TestCreateIfUpToDate(t *testing.T) {
want := "invalid settings: quicklinks.0.url: Does not match pattern '^(https?://|/)'"
_, err := Settings(db).CreateIfUpToDate(ctx, api.SettingsSubject{User: &u.ID}, nil, nil, contents)
_, err := db.Settings().CreateIfUpToDate(ctx, api.SettingsSubject{User: &u.ID}, nil, nil, contents)
if err == nil {
t.Log("Expected an error")
t.Fail()
@ -94,7 +94,7 @@ func TestCreateIfUpToDate(t *testing.T) {
}
func TestGetLatestSchemaSettings(t *testing.T) {
db := dbtest.NewDB(t)
db := NewDB(dbtest.NewDB(t))
ctx := context.Background()
user1, err := Users(db).Create(ctx, NewUser{Username: "u1"})
@ -102,11 +102,11 @@ func TestGetLatestSchemaSettings(t *testing.T) {
t.Fatal(err)
}
if _, err := Settings(db).CreateIfUpToDate(ctx, api.SettingsSubject{User: &user1.ID}, nil, &user1.ID, `{"search.uppercase": true }`); err != nil {
if _, err := db.Settings().CreateIfUpToDate(ctx, api.SettingsSubject{User: &user1.ID}, nil, &user1.ID, `{"search.uppercase": true }`); err != nil {
t.Error(err)
}
settings, err := Settings(db).GetLastestSchemaSettings(ctx, api.SettingsSubject{User: &user1.ID})
settings, err := db.Settings().GetLastestSchemaSettings(ctx, api.SettingsSubject{User: &user1.ID})
if err != nil {
t.Fatal(err)
}

View File

@ -2,7 +2,6 @@ package database
import (
"context"
"database/sql"
"github.com/keegancsmith/sqlf"
"github.com/lib/pq"
@ -10,7 +9,6 @@ import (
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/authz"
"github.com/sourcegraph/sourcegraph/internal/database/basestore"
"github.com/sourcegraph/sourcegraph/internal/database/dbutil"
"github.com/sourcegraph/sourcegraph/internal/extsvc"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
@ -49,11 +47,6 @@ type subRepoPermsStore struct {
*basestore.Store
}
// SubRepoPerms returns a new SubRepoPermsStore with the given parameters.
func SubRepoPerms(db dbutil.DB) SubRepoPermsStore {
return &subRepoPermsStore{Store: basestore.NewWithDB(db, sql.TxOptions{})}
}
func SubRepoPermsWith(other basestore.ShareableStore) SubRepoPermsStore {
return &subRepoPermsStore{Store: basestore.NewWithHandle(other.Handle())}
}

View File

@ -18,11 +18,11 @@ func TestSubRepoPermsInsert(t *testing.T) {
}
t.Parallel()
db := dbtest.NewDB(t)
db := NewDB(dbtest.NewDB(t))
ctx := context.Background()
prepareSubRepoTestData(ctx, t, db)
s := SubRepoPerms(db)
s := db.SubRepoPerms()
userID := int32(1)
repoID := api.RepoID(1)
@ -50,11 +50,11 @@ func TestSubRepoPermsUpsert(t *testing.T) {
}
t.Parallel()
db := dbtest.NewDB(t)
db := NewDB(dbtest.NewDB(t))
ctx := context.Background()
prepareSubRepoTestData(ctx, t, db)
s := SubRepoPerms(db)
s := db.SubRepoPerms()
userID := int32(1)
repoID := api.RepoID(1)
@ -92,11 +92,11 @@ func TestSubRepoPermsUpsertWithSpec(t *testing.T) {
}
t.Parallel()
db := dbtest.NewDB(t)
db := NewDB(dbtest.NewDB(t))
ctx := context.Background()
prepareSubRepoTestData(ctx, t, db)
s := SubRepoPerms(db)
s := db.SubRepoPerms()
userID := int32(1)
repoID := api.RepoID(1)
@ -139,10 +139,10 @@ func TestSubRepoPermsGetByUser(t *testing.T) {
}
t.Parallel()
db := dbtest.NewDB(t)
db := NewDB(dbtest.NewDB(t))
ctx := context.Background()
s := SubRepoPerms(db)
s := db.SubRepoPerms()
prepareSubRepoTestData(ctx, t, db)
userID := int32(1)
@ -190,10 +190,10 @@ func TestSubRepoPermsSupportedForRepoId(t *testing.T) {
}
t.Parallel()
db := dbtest.NewDB(t)
db := NewDB(dbtest.NewDB(t))
ctx := context.Background()
s := SubRepoPerms(db)
s := db.SubRepoPerms()
prepareSubRepoTestData(ctx, t, db)
testSubRepoNotSupportedForRepo(ctx, t, s, 3, "perforce1", "Repo is not private, therefore sub-repo perms are not supported")

View File

@ -7,7 +7,6 @@ import (
"github.com/keegancsmith/sqlf"
"github.com/sourcegraph/sourcegraph/internal/database/basestore"
"github.com/sourcegraph/sourcegraph/internal/database/dbutil"
ts "github.com/sourcegraph/sourcegraph/internal/temporarysettings"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
@ -23,11 +22,6 @@ type temporarySettingsStore struct {
*basestore.Store
}
// TemporarySettings instantiates and returns a new TemporarySettingsStore with prepared statements.
func TemporarySettings(db dbutil.DB) TemporarySettingsStore {
return &temporarySettingsStore{Store: basestore.NewWithDB(db, sql.TxOptions{})}
}
// TemporarySettingsWith instantiates and returns a new TemporarySettingsStore using the other store handle.
func TemporarySettingsWith(other basestore.ShareableStore) TemporarySettingsStore {
return &temporarySettingsStore{Store: basestore.NewWithHandle(other.Handle())}

View File

@ -528,10 +528,10 @@ func TestUsers_Delete(t *testing.T) {
}
// Create settings for the user, and for another user authored by this user.
if _, err := Settings(db).CreateIfUpToDate(ctx, api.SettingsSubject{User: &user.ID}, nil, &user.ID, "{}"); err != nil {
if _, err := db.Settings().CreateIfUpToDate(ctx, api.SettingsSubject{User: &user.ID}, nil, &user.ID, "{}"); err != nil {
t.Fatal(err)
}
if _, err := Settings(db).CreateIfUpToDate(ctx, api.SettingsSubject{User: &otherUser.ID}, nil, &user.ID, "{}"); err != nil {
if _, err := db.Settings().CreateIfUpToDate(ctx, api.SettingsSubject{User: &otherUser.ID}, nil, &user.ID, "{}"); err != nil {
t.Fatal(err)
}
@ -541,7 +541,7 @@ func TestUsers_Delete(t *testing.T) {
}
// Create a saved search owned by the user.
if _, err := SavedSearches(db).Create(ctx, &types.SavedSearch{
if _, err := db.SavedSearches().Create(ctx, &types.SavedSearch{
Description: "desc",
Query: "foo",
UserID: &user.ID,
@ -589,13 +589,13 @@ func TestUsers_Delete(t *testing.T) {
}
// User's settings no longer exist.
if settings, err := Settings(db).GetLatest(ctx, api.SettingsSubject{User: &user.ID}); err != nil {
if settings, err := db.Settings().GetLatest(ctx, api.SettingsSubject{User: &user.ID}); err != nil {
t.Error(err)
} else if settings != nil {
t.Errorf("got settings %+v, want nil", settings)
}
// Settings authored by user still exist but have nil author.
if settings, err := Settings(db).GetLatest(ctx, api.SettingsSubject{User: &otherUser.ID}); err != nil {
if settings, err := db.Settings().GetLatest(ctx, api.SettingsSubject{User: &otherUser.ID}); err != nil {
t.Fatal(err)
} else if settings.AuthorUserID != nil {
t.Errorf("got author %v, want nil", *settings.AuthorUserID)

View File

@ -10,7 +10,6 @@ import (
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/database"
"github.com/sourcegraph/sourcegraph/internal/database/dbutil"
"github.com/sourcegraph/sourcegraph/internal/jsonc"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
@ -24,7 +23,7 @@ type Loader interface {
// DBLoader will load insights from a database. This is also where the application can access insights currently stored
// in user / org settings.
type DBLoader struct {
db dbutil.DB
db database.DB
}
func (d *DBLoader) LoadAll(ctx context.Context) ([]SearchInsight, error) {
@ -35,7 +34,7 @@ func (d *DBLoader) LoadDashboards(ctx context.Context) ([]SettingDashboard, erro
return DiscoverDashboardsInSettings(ctx, d.db)
}
func NewLoader(db dbutil.DB) Loader {
func NewLoader(db database.DB) Loader {
return &DBLoader{db: db}
}
@ -43,8 +42,8 @@ func NewLoader(db dbutil.DB) Loader {
// generating aggregates for code insights which are currently stored in the settings.
// 🚨 SECURITY: This method bypasses any user permissions to fetch a list of all settings on the Sourcegraph installation.
// It is used for generating aggregated analytics that require an accurate view across all settings, such as for code insights🚨
func GetSettings(ctx context.Context, db dbutil.DB, filter SettingFilter, prefix string) ([]*api.Settings, error) {
settingStore := database.Settings(db)
func GetSettings(ctx context.Context, db database.DB, filter SettingFilter, prefix string) ([]*api.Settings, error) {
settingStore := db.Settings()
settings, err := settingStore.ListAll(ctx, prefix)
if err != nil {
return []*api.Settings{}, err
@ -84,7 +83,7 @@ func FilterSettingJson(settingJson string, prefix string) (map[string]json.RawMe
// GetSearchInsights returns insights stored in user / org / global settings that match the extensions schema. This schema is planned for deprecation
// and currently only exists to service pings.
func GetSearchInsights(ctx context.Context, db dbutil.DB, filter SettingFilter) ([]SearchInsight, error) {
func GetSearchInsights(ctx context.Context, db database.DB, filter SettingFilter) ([]SearchInsight, error) {
prefix := "searchInsights."
settings, err := GetSettings(ctx, db, filter, prefix)
if err != nil {
@ -113,7 +112,7 @@ func GetSearchInsights(ctx context.Context, db dbutil.DB, filter SettingFilter)
return results, nil
}
func GetLangStatsInsights(ctx context.Context, db dbutil.DB, filter SettingFilter) ([]LangStatsInsight, error) {
func GetLangStatsInsights(ctx context.Context, db database.DB, filter SettingFilter) ([]LangStatsInsight, error) {
prefix := "codeStatsInsights."
settings, err := GetSettings(ctx, db, filter, prefix)
@ -153,7 +152,7 @@ func GetLangStatsInsights(ctx context.Context, db dbutil.DB, filter SettingFilte
// dictionary of unique keys to extension setting body. This is intended to be deprecated as soon as code insights migrates
// fully to a persistent database. Any deserialization errors that occur during parsing will be logged as errors, but will not
// cause any errors to surface.
func GetIntegratedInsights(ctx context.Context, db dbutil.DB) ([]SearchInsight, error) {
func GetIntegratedInsights(ctx context.Context, db database.DB) ([]SearchInsight, error) {
prefix := "insights.allrepos"
settings, err := GetSettings(ctx, db, All, prefix)
@ -301,7 +300,7 @@ type SettingDashboard struct {
OrgID *int32
}
func DiscoverDashboardsInSettings(ctx context.Context, db dbutil.DB) ([]SettingDashboard, error) {
func DiscoverDashboardsInSettings(ctx context.Context, db database.DB) ([]SettingDashboard, error) {
prefix := "insights.dashboards"
settings, err := GetSettings(ctx, db, All, prefix)
if err != nil {

View File

@ -8,25 +8,24 @@ import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/hexops/autogold"
"github.com/hexops/valast"
"github.com/inconshreveable/log15"
"github.com/google/go-cmp/cmp"
"github.com/sourcegraph/sourcegraph/internal/database"
"github.com/sourcegraph/sourcegraph/internal/database/dbtest"
)
func TestGetSearchInsights(t *testing.T) {
ctx := context.Background()
db := dbtest.NewDB(t)
_, err := db.Exec(`INSERT INTO orgs(id, name) VALUES (1, 'first-org'), (2, 'second-org');`)
db := database.NewDB(dbtest.NewDB(t))
_, err := db.ExecContext(ctx, `INSERT INTO orgs(id, name) VALUES (1, 'first-org'), (2, 'second-org');`)
if err != nil {
t.Fatal(err)
}
_, err = db.Exec(`
_, err = db.ExecContext(ctx, `
INSERT INTO settings (id, org_id, contents, created_at, user_id, author_user_id)
VALUES (1, 1, $1, CURRENT_TIMESTAMP, NULL, NULL)`, insightSettingSimple)
@ -64,19 +63,19 @@ func TestGetSearchInsightsMulti(t *testing.T) {
t.Parallel()
ctx := context.Background()
db := dbtest.NewDB(t)
_, err := db.Exec(`INSERT INTO orgs(id, name) VALUES (1, 'first-org'), (2, 'second-org');`)
db := database.NewDB(dbtest.NewDB(t))
_, err := db.ExecContext(ctx, `INSERT INTO orgs(id, name) VALUES (1, 'first-org'), (2, 'second-org');`)
if err != nil {
t.Fatal(err)
}
_, err = db.Exec(`
_, err = db.ExecContext(ctx, `
INSERT INTO settings (id, org_id, contents, created_at, user_id, author_user_id)
VALUES (1, 1, $1, CURRENT_TIMESTAMP, NULL, NULL)`, insightSettingNotSoSimple)
if err != nil {
t.Fatal(err)
}
_, err = db.Exec(`
_, err = db.ExecContext(ctx, `
INSERT INTO settings (id, org_id, contents, created_at, user_id, author_user_id)
VALUES (2, 2, $1, CURRENT_TIMESTAMP, NULL, NULL)`, insightSettingThree)
@ -135,12 +134,12 @@ func TestGetIntegrationInsights(t *testing.T) {
t.Parallel()
ctx := context.Background()
db := dbtest.NewDB(t)
_, err := db.Exec(`INSERT INTO orgs(id, name) VALUES (1, 'first-org'), (2, 'second-org');`)
db := database.NewDB(dbtest.NewDB(t))
_, err := db.ExecContext(ctx, `INSERT INTO orgs(id, name) VALUES (1, 'first-org'), (2, 'second-org');`)
if err != nil {
t.Fatal(err)
}
_, err = db.Exec(`
_, err = db.ExecContext(ctx, `
INSERT INTO settings (id, org_id, contents, created_at, user_id, author_user_id)
VALUES (1, 1, $1, CURRENT_TIMESTAMP, NULL, NULL)`, integratedInsightSimple)

View File

@ -17,7 +17,6 @@ import (
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/database"
"github.com/sourcegraph/sourcegraph/internal/database/dbutil"
"github.com/sourcegraph/sourcegraph/internal/errcode"
"github.com/sourcegraph/sourcegraph/internal/lazyregexp"
"github.com/sourcegraph/sourcegraph/internal/search"
@ -253,8 +252,8 @@ func validateSearchContextQuery(contextQuery string) error {
return errs
}
func validateSearchContextDoesNotExist(ctx context.Context, db dbutil.DB, searchContext *types.SearchContext) error {
_, err := database.SearchContexts(db).GetSearchContext(ctx, database.GetSearchContextOptions{
func validateSearchContextDoesNotExist(ctx context.Context, db database.DB, searchContext *types.SearchContext) error {
_, err := db.SearchContexts().GetSearchContext(ctx, database.GetSearchContextOptions{
Name: searchContext.Name,
NamespaceUserID: searchContext.NamespaceUserID,
NamespaceOrgID: searchContext.NamespaceOrgID,

View File

@ -404,7 +404,7 @@ func TestCreatingSearchContexts(t *testing.T) {
t.Fatalf("Expected no error, got %s", err)
}
existingSearchContext, err := database.SearchContexts(db).CreateSearchContextWithRepositoryRevisions(
existingSearchContext, err := db.SearchContexts().CreateSearchContextWithRepositoryRevisions(
internalCtx,
&types.SearchContext{Name: "existing"},
[]*types.SearchContextRepositoryRevisions{},
@ -504,7 +504,7 @@ func TestUpdatingSearchContexts(t *testing.T) {
var scs []*types.SearchContext
for i := 0; i < 6; i++ {
sc, err := database.SearchContexts(db).CreateSearchContextWithRepositoryRevisions(
sc, err := db.SearchContexts().CreateSearchContextWithRepositoryRevisions(
internalCtx,
&types.SearchContext{Name: strconv.Itoa(i)},
[]*types.SearchContextRepositoryRevisions{},