Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: add db_schema store (#3741)

Danny Xu0636edacbd7b
diff --git a/api/db_schema.go b/api/db_schema.go
new file mode 100644
index 0000000000..0dffbbc75e
--- /dev/null
+++ b/api/db_schema.go
@@ -0,0 +1,44 @@
+package api
+
+// DBSchema is the API message for database schema.
+type DBSchema struct {
+ ID int `jsonapi:"primary,dbSchema"`
+
+ // Standard fields
+
+ // Related fields
+ DatabaseID int
+
+ // Domain specific fields
+ Metadata string `jsonapi:"attr,metadata"`
+ RawDump string `jsonapi:"attr,rawDump"`
+}
+
+// DBSchemaUpsert is the API message for creating a database schema.
+type DBSchemaUpsert struct {
+ // Standard fields
+ // Value is assigned from the jwt subject field passed by the client.
+ UpdatorID int
+ CreatedTs int64
+ UpdatedTs int64
+
+ // Related fields
+ DatabaseID int
+
+ // Domain specific fields
+ Metadata string
+ RawDump string
+}
+
+// DBSchemaFind is the API message for finding database schemas.
+type DBSchemaFind struct {
+ // Related fields
+ DatabaseID int
+
+ // Domain specific fields
+}
+
+// DBSchemaDelete is the API message for deleting a database schema.
+type DBSchemaDelete struct {
+ DatabaseID int
+}
diff --git a/store/cache.go b/store/cache.go
index 9664bbd83f..e4207a0b43 100644
--- a/store/cache.go
+++ b/store/cache.go
@@ -42,6 +42,8 @@ const (
dataSourceCacheNamespace cacheNamespace = "ds"
// tierPolicyCacheNamespace is the cache type of tier policy.
tierPolicyCacheNamespace cacheNamespace = "pot"
+ // schemaCacheNamespace is the cache type of database schema.
+ schemaCacheNamespace cacheNamespace = "sc"
)
// CacheService implements a cache.
diff --git a/store/db_schema.go b/store/db_schema.go
new file mode 100644
index 0000000000..f512c01edc
--- /dev/null
+++ b/store/db_schema.go
@@ -0,0 +1,154 @@
+package store
+
+import (
+ "context"
+ "database/sql"
+ "fmt"
+ "strings"
+
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/api"
+ "github.com/bytebase/bytebase/common"
+)
+
+// dbSchemaRaw is the store model for a database schema.
+// Fields have exactly the same meaning as DBSchema.
+type dbSchemaRaw struct {
+ ID int
+
+ // Standard fields
+
+ // Related fields
+ DatabaseID int
+
+ // Domain specific fields
+ Metadata string
+ RawDump string
+}
+
+func (raw *dbSchemaRaw) toDBSchema() *api.DBSchema {
+ return &api.DBSchema{
+ ID: raw.ID,
+
+ // Standard fields
+
+ // Related fields
+ DatabaseID: raw.DatabaseID,
+
+ // Domain specific fields
+ Metadata: raw.Metadata,
+ RawDump: raw.RawDump,
+ }
+}
+
+// GetDBSchema gets the schema for a database.
+func (s *Store) GetDBSchema(ctx context.Context, databaseID int) (*api.DBSchema, error) {
+ cachedDBSchema := &api.DBSchema{}
+ ok, err := s.cache.FindCache(principalCacheNamespace, databaseID, cachedDBSchema)
+ if err != nil {
+ return nil, err
+ }
+ if ok {
+ return cachedDBSchema, nil
+ }
+
+ // Build WHERE clause.
+ where, args := []string{"1 = 1"}, []interface{}{}
+ where, args = append(where, fmt.Sprintf("database_id = $%d", len(args)+1)), append(args, databaseID)
+
+ rows, err := s.db.db.QueryContext(ctx, `
+ SELECT
+ id,
+ database_id,
+ metadata,
+ raw_dump
+ FROM db_schema
+ WHERE `+strings.Join(where, " AND "),
+ args...,
+ )
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer rows.Close()
+
+ var list []*dbSchemaRaw
+ for rows.Next() {
+ var raw dbSchemaRaw
+ if err := rows.Scan(
+ &raw.ID,
+ &raw.DatabaseID,
+ &raw.Metadata,
+ &raw.RawDump,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+ list = append(list, &raw)
+ }
+ if err := rows.Err(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ if len(list) == 0 {
+ return nil, nil
+ } else if len(list) > 1 {
+ return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d database schema for database %d, expect 1", len(list), databaseID)}
+ }
+
+ dbSchema := list[0].toDBSchema()
+ if err := s.cache.UpsertCache(schemaCacheNamespace, databaseID, dbSchema); err != nil {
+ return nil, err
+ }
+ return dbSchema, nil
+}
+
+// UpsertDBSchema upserts a database schema.
+func (s *Store) UpsertDBSchema(ctx context.Context, upsert api.DBSchemaUpsert) (*api.DBSchema, error) {
+ query := `
+ INSERT INTO db_schema (
+ creator_id,
+ updater_id,
+ database_id,
+ metadata,
+ raw_dump
+ )
+ VALUES ($1, $2, $3, $4, $5)
+ ON CONFLICT(database_id) DO UPDATE SET
+ metadata = EXCLUDED.metadata,
+ raw_dump = EXCLUDED.raw_dump
+ RETURNING id, database_id, metadata, raw_dump
+ `
+ var raw dbSchemaRaw
+ if err := s.db.db.QueryRowContext(ctx, query,
+ upsert.UpdatorID,
+ upsert.UpdatorID,
+ upsert.DatabaseID,
+ upsert.Metadata,
+ upsert.RawDump,
+ ).Scan(
+ &raw.ID,
+ &raw.DatabaseID,
+ &raw.Metadata,
+ &raw.RawDump,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
+ }
+ return nil, FormatError(err)
+ }
+ dbSchema := raw.toDBSchema()
+ if err := s.cache.UpsertCache(schemaCacheNamespace, upsert.DatabaseID, dbSchema); err != nil {
+ return nil, err
+ }
+ return dbSchema, nil
+}
+
+// DeleteDBSchema deletes the schema for a database.
+func (s *Store) DeleteDBSchema(ctx context.Context, databaseID int) (*api.DBSchema, error) {
+ if _, err := s.db.db.ExecContext(ctx, "DELETE FROM db_schema WHERE database_id = $1", databaseID); err != nil {
+ return nil, FormatError(err)
+ }
+ // Invalidate cache.
+ s.cache.DeleteCache(schemaCacheNamespace, databaseID)
+ return nil, nil
+}