Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(mongo): sync mongodb instance (#3889)
h3n4l563fa998d473
diff --git a/frontend/src/components/DatabaseOverviewPanel.vue b/frontend/src/components/DatabaseOverviewPanel.vueindex ccf70df749..337d103f80 100644--- a/frontend/src/components/DatabaseOverviewPanel.vue+++ b/frontend/src/components/DatabaseOverviewPanel.vue@@ -28,8 +28,9 @@><templatev-if="- database.instance.engine != 'CLICKHOUSE' &&- database.instance.engine != 'SNOWFLAKE'+ database.instance.engine !== 'CLICKHOUSE' &&+ database.instance.engine !== 'SNOWFLAKE' &&+ database.instance.engine !== 'MONGODB'"><div class="col-span-1 col-start-1">diff --git a/plugin/db/driver.go b/plugin/db/driver.goindex 75b96130e9..81eb0cccd2 100644--- a/plugin/db/driver.go+++ b/plugin/db/driver.go@@ -143,9 +143,9 @@ type InstanceMeta struct {// DatabaseMeta is the metadata for a database.type DatabaseMeta struct {Name string- // CharacterSet isn't supported for ClickHouse, Snowflake.+ // CharacterSet isn't supported for ClickHouse, Snowflake, MongoDB.CharacterSet string- // Collation isn't supported for ClickHouse, Snowflake.+ // Collation isn't supported for ClickHouse, Snowflake, MongoDB.Collation string}diff --git a/plugin/db/mongodb/mongodb.go b/plugin/db/mongodb/mongodb.goindex f9b6ba53e5..775a0137e8 100644--- a/plugin/db/mongodb/mongodb.go+++ b/plugin/db/mongodb/mongodb.go@@ -78,16 +78,6 @@ func (*Driver) Query(_ context.Context, _ string, _ *db.QueryContext) ([]interfapanic("not implemented")}-// SyncInstance syncs the instance meta.-func (*Driver) SyncInstance(_ context.Context) (*db.InstanceMeta, error) {- panic("not implemented")-}--// SyncDBSchema syncs the database schema.-func (*Driver) SyncDBSchema(_ context.Context, _ string) (*db.Schema, error) {- panic("not implemented")-}-// Dump dumps the database.func (*Driver) Dump(_ context.Context, _ string, _ io.Writer, _ bool) (string, error) {panic("not implemented")diff --git a/plugin/db/mongodb/sync.go b/plugin/db/mongodb/sync.gonew file mode 100644index 0000000000..7b1044e596--- /dev/null+++ b/plugin/db/mongodb/sync.go@@ -0,0 +1,133 @@+package mongodb++import (+ "context"+ "encoding/json"++ "go.mongodb.org/mongo-driver/bson"++ "github.com/pkg/errors"++ "github.com/bytebase/bytebase/plugin/db"+)++// UsersInfo is the subset of the mongodb command result of "usersInfo".+type UsersInfo struct {+ Users []User `bson:"users"`+}++// User is the subset of the `users` field in the `User`.+type User struct {+ ID string `json:"_id" bson:"_id"`+ UserName string `json:"user" bson:"user"`+ DB string `json:"db" bson:"db"`+ Roles []Role `json:"roles" bson:"roles"`+}++// Role is the subset of the `roles` field in the `User`.+type Role struct {+ RoleName string `json:"role" bson:"role"`+ DB string `json:"db" bson:"db"`+}++var (+ systemDatabases = map[string]bool{+ "admin": true,+ "config": true,+ "local": true,+ }+)++// SyncInstance syncs the instance meta.+func (driver *Driver) SyncInstance(ctx context.Context) (*db.InstanceMeta, error) {+ version, err := driver.getVersion(ctx)+ if err != nil {+ return nil, err+ }+ userList, err := driver.getUserMetaList(ctx)+ if err != nil {+ return nil, err+ }+ var databaseMetaList []db.DatabaseMeta+ dbList, err := driver.getDatabaseList(ctx)+ if err != nil {+ return nil, err+ }+ for _, databaseName := range dbList {+ databaseMetaList = append(databaseMetaList, db.DatabaseMeta{+ Name: databaseName,+ })+ }++ return &db.InstanceMeta{+ Version: version,+ UserList: userList,+ DatabaseList: databaseMetaList,+ }, nil+}++// SyncDBSchema syncs the database schema.+func (*Driver) SyncDBSchema(_ context.Context, _ string) (*db.Schema, error) {+ panic("not implemented")+}++// getVersion returns the version of mongod or mongos instance.+func (driver *Driver) getVersion(ctx context.Context) (string, error) {+ database := driver.client.Database(migrationHistoryDefaultDatabase)+ var commandResult bson.M+ command := bson.D{{Key: "buildInfo", Value: 1}}+ if err := database.RunCommand(ctx, command).Decode(&commandResult); err != nil {+ return "", errors.Wrap(err, "cannot run buildInfo command")+ }+ version, ok := commandResult["version"]+ if !ok {+ return "", errors.New("cannot get version from buildInfo command result")+ }+ return version.(string), nil+}++// getUserList returns the list of users.+func (driver *Driver) getUserMetaList(ctx context.Context) ([]db.User, error) {+ database := driver.client.Database(migrationHistoryDefaultDatabase)+ command := bson.D{{+ Key: "usersInfo",+ Value: bson.D{{+ Key: "forAllDBs",+ Value: true,+ }},+ }}+ var commandResult UsersInfo+ if err := database.RunCommand(ctx, command).Decode(&commandResult); err != nil {+ return nil, errors.Wrap(err, "cannot run usersInfo command")+ }+ var dbUserList []db.User+ for _, user := range commandResult.Users {+ var dbUser db.User+ dbUser.Name = user.UserName+ bs, err := json.Marshal(user)+ if err != nil {+ return nil, errors.Wrap(err, "cannot marshal user")+ }+ dbUser.Grant = string(bs)+ dbUserList = append(dbUserList, dbUser)+ }+ return dbUserList, nil+}++func flattenSystemDatabase() []string {+ var systemDatabaseList []string+ for databaseName := range systemDatabases {+ systemDatabaseList = append(systemDatabaseList, databaseName)+ }+ return systemDatabaseList+}++// getDatabaseList returns the list of databases.+func (driver *Driver) getDatabaseList(ctx context.Context) ([]string, error) {+ filter := bson.M{+ "name": bson.M{+ "$nin": append(flattenSystemDatabase(), "bytebase"),+ },+ }+ return driver.client.ListDatabaseNames(ctx, filter)+}diff --git a/server/runner/schemasync/syncer.go b/server/runner/schemasync/syncer.goindex 17121a3379..a176a33a27 100644--- a/server/runner/schemasync/syncer.go+++ b/server/runner/schemasync/syncer.go@@ -171,10 +171,6 @@ func (s *Syncer) syncAllDatabases(ctx context.Context, instanceID *int) {// SyncInstance syncs the schema for all databases in an instance.func (s *Syncer) SyncInstance(ctx context.Context, instance *api.Instance) ([]string, error) {- // TODO(zp): support sync schema for mongodb.- if instance.Engine == db.MongoDB {- return nil, nil- }driver, err := s.dbFactory.GetAdminDatabaseDriver(ctx, instance, "")if err != nil {return nil, err@@ -333,6 +329,10 @@ func (s *Syncer) syncInstanceSchema(ctx context.Context, instance *api.Instance,// SyncDatabaseSchema will sync the schema for a database.func (s *Syncer) SyncDatabaseSchema(ctx context.Context, instance *api.Instance, databaseName string, force bool) error {+ // TODO(zp): Support Sync Database Schema for MongoDB later.+ if instance.Engine == db.MongoDB {+ return nil+ }driver, err := s.dbFactory.GetAdminDatabaseDriver(ctx, instance, databaseName)if err != nil {return err