Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor(store): centralize db schema cache in store package (#18881)
p0nyfba2e0d7e502
diff --git a/backend/api/lsp/completion.go b/backend/api/lsp/completion.goindex 9819fc7360..25772a1f01 100644--- a/backend/api/lsp/completion.go+++ b/backend/api/lsp/completion.go@@ -145,34 +145,16 @@ func convertLSPCompletionItemKind(tp parserbase.CandidateType) lsp.CompletionIte}func (h *Handler) GetDatabaseMetadataFunc(ctx context.Context, instanceID, databaseName string) (string, *model.DatabaseMetadata, error) {- // Check cache first- cacheKey := getDatabaseMetadataCacheKey(instanceID, databaseName)- if cached, exists := h.metadataCache.Get(cacheKey); exists {- return databaseName, cached, nil- }-- // Cache miss, fetch from store- metadata, err := h.store.GetDBSchema(ctx, &store.FindDBSchemaMessage{- InstanceID: instanceID,- DatabaseName: databaseName,- })+ metadata, err := h.store.GetDBSchemaSnapshot(ctx, instanceID, databaseName)if err != nil {return "", nil, errors.Wrap(err, "failed to get database schema")}if metadata == nil {return "", nil, errors.Errorf("database %s schema for instance %s not found", databaseName, instanceID)}-- // Store in cache- h.metadataCache.Add(cacheKey, metadata)-return databaseName, metadata, nil}-func getDatabaseMetadataCacheKey(instanceID, databaseName string) string {- return instanceID + "/" + databaseName-}-func (h *Handler) ListDatabaseNamesFunc(ctx context.Context, instanceID string) ([]string, error) {if instanceID == "" {return nil, errors.Errorf("instance is not specified")diff --git a/backend/api/lsp/handler.go b/backend/api/lsp/handler.goindex c0e9c43ea1..e7e2d2e54d 100644--- a/backend/api/lsp/handler.go+++ b/backend/api/lsp/handler.go@@ -9,7 +9,6 @@ import ("time"lsp "github.com/bytebase/lsp-protocol"- "github.com/hashicorp/golang-lru/v2/expirable""github.com/pkg/errors""github.com/sourcegraph/jsonrpc2"@@ -21,7 +20,6 @@ import (storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/parser/base""github.com/bytebase/bytebase/backend/store"- "github.com/bytebase/bytebase/backend/store/model")type Method string@@ -58,9 +56,8 @@ func NewHandlerWithAuth(s *store.Store, profile *config.Profile, iamManager *iamuser: user,tokenExpiry: tokenExpiry,iamManager: iamManager,- diagnosticsDebouncer: NewDiagnosticsDebouncer(500 * time.Millisecond), // 500ms debounce- contentCache: NewContentCache(100), // Cache up to 100 documents- metadataCache: expirable.NewLRU[string, *model.DatabaseMetadata](128, nil, 5*time.Minute), // Cache up to 128 database metadata with 5min TTL+ diagnosticsDebouncer: NewDiagnosticsDebouncer(500 * time.Millisecond), // 500ms debounce+ contentCache: NewContentCache(100), // Cache up to 100 documents}return lspHandler{Handler: jsonrpc2.HandlerWithError(handler.handle)}}@@ -97,7 +94,6 @@ type Handler struct {// Performance optimizationsdiagnosticsDebouncer *DiagnosticsDebouncercontentCache *ContentCache- metadataCache *expirable.LRU[string, *model.DatabaseMetadata]}// ShutDown shuts down the handler.diff --git a/backend/store/db_schema.go b/backend/store/db_schema.goindex 8577ad7e7e..25f9706104 100644--- a/backend/store/db_schema.go+++ b/backend/store/db_schema.go@@ -64,9 +64,20 @@ func (s *Store) GetDBSchema(ctx context.Context, find *FindDBSchemaMessage) (*moreturn nil, err}+ s.dbSchemaCache.Add(getDBSchemaCacheKey(find.InstanceID, find.DatabaseName), dbMetadata)+return dbMetadata, nil}+// GetDBSchemaSnapshot gets the schema for a database with cache.+func (s *Store) GetDBSchemaSnapshot(ctx context.Context, instanceID, databaseName string) (*model.DatabaseMetadata, error) {+ cacheKey := getDBSchemaCacheKey(instanceID, databaseName)+ if v, ok := s.dbSchemaCache.Get(cacheKey); ok {+ return v, nil+ }+ return s.GetDBSchema(ctx, &FindDBSchemaMessage{InstanceID: instanceID, DatabaseName: databaseName})+}+// UpsertDBSchema upserts a database schema.func (s *Store) UpsertDBSchema(ctx context.Context,@@ -126,7 +137,13 @@ func (s *Store) UpsertDBSchema(return err}- return tx.Commit()+ if err := tx.Commit(); err != nil {+ return err+ }++ s.dbSchemaCache.Remove(getDBSchemaCacheKey(instanceID, databaseName))++ return nil}// UpdateDBSchema updates a database schema.@@ -159,7 +176,13 @@ func (s *Store) UpdateDBSchema(ctx context.Context, instanceID, databaseName strreturn err}- return tx.Commit()+ if err := tx.Commit(); err != nil {+ return err+ }++ s.dbSchemaCache.Remove(getDBSchemaCacheKey(instanceID, databaseName))++ return nil}func (s *Store) convertMetadataAndConfig(ctx context.Context, metadata, schema, config []byte, instanceID string) (*model.DatabaseMetadata, error) {diff --git a/backend/store/store.go b/backend/store/store.goindex a7d73dfc34..6b1f6ee148 100644--- a/backend/store/store.go+++ b/backend/store/store.go@@ -11,6 +11,7 @@ import ("github.com/hashicorp/golang-lru/v2/expirable"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/model")// Store provides database access to all raw objects.@@ -30,6 +31,7 @@ type Store struct {groupCache *expirable.LRU[string, *GroupMessage]groupMembersCache *expirable.LRU[string, map[string]bool]memberGroupsCache *expirable.LRU[string, []string]+ dbSchemaCache *expirable.LRU[string, *model.DatabaseMetadata]// Large objects.sheetFullCache *lru.Cache[string, *SheetMessage]@@ -70,6 +72,7 @@ func New(ctx context.Context, pgURL string, enableCache bool) (*Store, error) {groupCache := expirable.NewLRU[string, *GroupMessage](1024, nil, time.Minute)groupMembersCache := expirable.NewLRU[string, map[string]bool](1024, nil, time.Minute)memberGroupsCache := expirable.NewLRU[string, []string](4096, nil, time.Minute)+ dbSchemaCache := expirable.NewLRU[string, *model.DatabaseMetadata](128, nil, 5*time.Minute)// Initialize database connection (handles both direct URL and file-based)dbConnManager := NewDBConnectionManager(pgURL)@@ -93,6 +96,7 @@ func New(ctx context.Context, pgURL string, enableCache bool) (*Store, error) {groupCache: groupCache,groupMembersCache: groupMembersCache,memberGroupsCache: memberGroupsCache,+ dbSchemaCache: dbSchemaCache,}return s, nil@@ -132,3 +136,7 @@ func getPolicyCacheKey(resourceType storepb.Policy_Resource, resource string, pofunc getDatabaseCacheKey(instanceID, databaseName string) string {return fmt.Sprintf("%s/%s", instanceID, databaseName)}++func getDBSchemaCacheKey(instanceID, databaseName string) string {+ return fmt.Sprintf("%s/%s", instanceID, databaseName)+}