Skip to content

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

feat(mongodb): sync mongodb view schema (#4010)

h3n4l18d88191ad7b
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index 08bfb7176b..978475d898 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -55,11 +55,14 @@ type View struct {
ShortName string
// Schema is the schema name for a table. It should be supported only for Postgres and Snowflake.
Schema string
- // CreatedTs isn't supported for ClickHouse.
- CreatedTs int64
- UpdatedTs int64
+ // CreatedTs isn't supported for ClickHouse, MongoDB.
+ CreatedTs int64
+ // UpdatedTs isn't supported for MongoDB.
+ UpdatedTs int64
+ // Definition isn't supported for MongoDB.
Definition string
- Comment string
+ // Comment isn't supported for MongoDB.
+ Comment string
}
// Extension is the database extension.
diff --git a/plugin/db/mongodb/mongodb.go b/plugin/db/mongodb/mongodb.go
index 15ec68a3a8..b6e8a6d779 100644
--- a/plugin/db/mongodb/mongodb.go
+++ b/plugin/db/mongodb/mongodb.go
@@ -22,6 +22,7 @@ import (
var (
collectionType = "collection"
+ viewType = "view"
_ db.Driver = (*Driver)(nil)
)
diff --git a/plugin/db/mongodb/sync.go b/plugin/db/mongodb/sync.go
index 34c0975b77..ed20e6abea 100644
--- a/plugin/db/mongodb/sync.go
+++ b/plugin/db/mongodb/sync.go
@@ -85,10 +85,42 @@ func (driver *Driver) SyncDBSchema(ctx context.Context, databaseName string) (*d
}
schema.TableList = tableList
- // TODO(zp): sync View schema
+ viewList, err := driver.syncAllViewSchema(ctx, databaseName)
+ if err != nil {
+ return nil, nil, err
+ }
+ schema.ViewList = viewList
+
return &schema, nil, nil
}
+// syncAllViewSchema returns all views schema of a database.
+func (driver *Driver) syncAllViewSchema(ctx context.Context, databaseName string) ([]db.View, error) {
+ database := driver.client.Database(databaseName)
+ viewFilter := bson.M{"type": viewType}
+ viewList, err := database.ListCollectionNames(ctx, viewFilter)
+ if err != nil {
+ return nil, errors.Wrap(err, "failed to list view names")
+ }
+ var viewSchemaList []db.View
+ for _, viewName := range viewList {
+ view, err := driver.syncViewSchema(ctx, databaseName, viewName)
+ if err != nil {
+ return nil, err
+ }
+ viewSchemaList = append(viewSchemaList, view)
+ }
+ return viewSchemaList, nil
+}
+
+// syncViewSchema returns the view schema.
+func (*Driver) syncViewSchema(_ context.Context, _ string, viewName string) (db.View, error) {
+ var view db.View
+ view.Name = viewName
+ view.ShortName = viewName
+ return view, nil
+}
+
func (driver *Driver) syncAllCollectionSchema(ctx context.Context, databaseName string) ([]db.Table, error) {
database := driver.client.Database(databaseName)
collectionFilter := bson.M{"type": collectionType}