Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(mongodb): skip all system collections in schema sync (#20283)
rebelicef30e38fa5395
diff --git a/backend/plugin/db/mongodb/mongodb_test.go b/backend/plugin/db/mongodb/mongodb_test.goindex 1f662a76bb..ffadf019f3 100644--- a/backend/plugin/db/mongodb/mongodb_test.go+++ b/backend/plugin/db/mongodb/mongodb_test.go@@ -143,6 +143,36 @@ func TestGetMongoDBConnectionURL(t *testing.T) {}}+func TestIsSystemCollection(t *testing.T) {+ tests := []struct {+ name string+ want bool+ }{+ {+ name: "users",+ want: false,+ },+ {+ name: "system.namespaces",+ want: true,+ },+ {+ name: "system.users",+ want: true,+ },+ {+ name: "system.buckets.events",+ want: true,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ require.Equal(t, tt.want, isSystemCollection(tt.name))+ })+ }+}+func TestIsMongoStatement(t *testing.T) {tests := []struct {statement stringdiff --git a/backend/plugin/db/mongodb/sync.go b/backend/plugin/db/mongodb/sync.goindex 672ab867c3..29cd1bfcf5 100644--- a/backend/plugin/db/mongodb/sync.go+++ b/backend/plugin/db/mongodb/sync.go@@ -16,14 +16,6 @@ import ("github.com/bytebase/bytebase/backend/plugin/db")-var systemCollection = map[string]bool{- "system.namespaces": true,- "system.indexes": true,- "system.profile": true,- "system.js": true,- "system.views": true,-}-var systemDatabase = map[string]bool{"admin": true,"config": true,@@ -142,7 +134,7 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetadslices.Sort(viewNames)for _, collectionName := range collectionNames {- if systemCollection[collectionName] {+ if isSystemCollection(collectionName) {continue}@@ -260,6 +252,10 @@ func getIndexes(ctx context.Context, collection *mongo.Collection) ([]*storepb.Ireturn indexes, nil}+func isSystemCollection(collectionName string) bool {+ return strings.HasPrefix(collectionName, "system.")+}+// getVersion returns the version of mongod or mongos instance.func (d *Driver) getVersion(ctx context.Context) (string, error) {database := d.client.Database(bytebaseDefaultDatabase)