Skip to content

Commit

Permalink
CBG3026 prereq: More low-risk cleanup/refactoring (#7120)
Browse files Browse the repository at this point in the history
* More low-risk cleanup/refactoring
- Move `DatabaseLogCtx` earlier in `_getOrAddDatabaseFromConfig` so we can identify which database init-related logging is for
- `t.Run`->`rt.Run` fixes (prevents panics with failing subtests)
- Minor rename and TODO comment for upcoming work

* rearrange code based on review
  • Loading branch information
bbrks authored Sep 20, 2024
1 parent 7b5a401 commit 03a3f3d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
6 changes: 3 additions & 3 deletions db/design_doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,9 @@ func stripSyncProperty(row *sgbucket.ViewRow) {
}

func InitializeViews(ctx context.Context, ds sgbucket.DataStore) error {
collection, ok := ds.(*base.Collection)
if ok && !collection.IsDefaultScopeCollection() {
return fmt.Errorf("Can not initialize views on a non default collection")
collection, isCBServerCollection := ds.(*base.Collection)
if isCBServerCollection && !collection.IsDefaultScopeCollection() {
return fmt.Errorf("Can not initialize views on a non-default Couchbase Server collection")
}

viewStore, ok := ds.(sgbucket.ViewStore)
Expand Down
1 change: 1 addition & 0 deletions db/indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ func InitializeIndexes(ctx context.Context, n1QLStore base.N1QLStore, options In
deferredIndexes := make([]string, 0)
for _, sgIndex := range sgIndexes {

// TODO CBG-2838: build list of indexes to create (with one GetIndexMeta call)
if !sgIndex.shouldCreate(options) {
base.DebugfCtx(ctx, base.KeyAll, "Skipping index: %s ...", sgIndex.simpleName)
continue
Expand Down
4 changes: 2 additions & 2 deletions rest/indextest/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestSyncGatewayStartupIndexes(t *testing.T) {
}

// tests sg_users index
t.Run("testUserQueries", func(t *testing.T) {
rt.Run("testUserQueries", func(t *testing.T) {
users := []string{"alice", "bob"}

for _, user := range users {
Expand All @@ -96,7 +96,7 @@ func TestSyncGatewayStartupIndexes(t *testing.T) {
})

// tests sg_roles index
t.Run("testRoleQueries", func(t *testing.T) {
rt.Run("testRoleQueries", func(t *testing.T) {
roles := []string{"roleA", "roleB"}

for _, role := range roles {
Expand Down
24 changes: 12 additions & 12 deletions rest/server_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,10 +608,6 @@ func (sc *ServerContext) _getOrAddDatabaseFromConfig(ctx context.Context, config
return nil, err
}

if spec.Server == "" {
spec.Server = sc.Config.Bootstrap.Server
}

previousDatabase := sc.databases_[dbName]
if previousDatabase != nil {
if options.useExisting {
Expand All @@ -622,6 +618,18 @@ func (sc *ServerContext) _getOrAddDatabaseFromConfig(ctx context.Context, config
"Duplicate database name %q", dbName)
}

// Generate database context options from config and server context
contextOptions, err := dbcOptionsFromConfig(ctx, sc, &config.DbConfig, dbName)
if err != nil {
return nil, err
}
// set this early so we have dbName available in db-init related logging, before we have an actual database
ctx = base.DatabaseLogCtx(ctx, dbName, contextOptions.LoggingConfig)

if spec.Server == "" {
spec.Server = sc.Config.Bootstrap.Server
}

// Connect to bucket
base.InfofCtx(ctx, base.KeyAll, "Opening db /%s as bucket %q, pool %q, server <%s>",
base.MD(dbName), base.MD(spec.BucketName), base.SD(base.DefaultPool), base.SD(spec.Server))
Expand Down Expand Up @@ -777,14 +785,6 @@ func (sc *ServerContext) _getOrAddDatabaseFromConfig(ctx context.Context, config
return nil, err
}

// Generate database context options from config and server context
contextOptions, err := dbcOptionsFromConfig(ctx, sc, &config.DbConfig, dbName)
if err != nil {
return nil, err
}

ctx = base.DatabaseLogCtx(ctx, dbName, contextOptions.LoggingConfig)

contextOptions.UseViews = useViews

javascriptTimeout := getJavascriptTimeout(&config.DbConfig)
Expand Down

0 comments on commit 03a3f3d

Please sign in to comment.