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(mongodb): sync mongodb index schema (part2) (#3916)
h3n4led4e973725f5
diff --git a/frontend/src/components/IndexTable.vue b/frontend/src/components/IndexTable.vueindex bae9cdc3a2..630444dc66 100644--- a/frontend/src/components/IndexTable.vue+++ b/frontend/src/components/IndexTable.vue@@ -11,20 +11,28 @@class="w-16":title="columnList[0].title"/>- <BBTableHeaderCell class="w-4" :title="columnList[1].title" />+ <BBTableHeaderCell+ v-if="showPositionColumn"+ class="w-4"+ :title="columnList[1].title"+ /><BBTableHeaderCell class="w-4" :title="columnList[2].title" /><BBTableHeaderCellv-if="showVisibleColumn"class="w-4":title="columnList[3].title"/>- <BBTableHeaderCell class="w-16" :title="columnList[4].title" />+ <BBTableHeaderCell+ v-if="showCommentColumn"+ class="w-16"+ :title="columnList[4].title"+ /></template><template #body="{ rowData: index }"><BBTableCell :left-padding="4">{{ index.expressions.join(",") }}</BBTableCell>- <BBTableCell>+ <BBTableCell v-if="showPositionColumn">{{ index.position }}</BBTableCell><BBTableCell>@@ -63,7 +71,16 @@ export default defineComponent({setup(props) {const { t } = useI18n();const showVisibleColumn = computed(() => {- return props.database.instance.engine !== "POSTGRES";+ return (+ props.database.instance.engine !== "POSTGRES" &&+ props.database.instance.engine !== "MONGODB"+ );+ });+ const showPositionColumn = computed(() => {+ return props.database.instance.engine !== "MONGODB";+ });+ const showCommentColumn = computed(() => {+ return props.database.instance.engine !== "MONGODB";});const columnList = computed(() => [{@@ -104,6 +121,8 @@ export default defineComponent({columnList,sectionList,showVisibleColumn,+ showPositionColumn,+ showCommentColumn,};},});diff --git a/plugin/db/driver.go b/plugin/db/driver.goindex 9e232b6626..26b8bf9595 100644--- a/plugin/db/driver.go+++ b/plugin/db/driver.go@@ -73,14 +73,16 @@ type Index struct {Name string// This could refer to a column or an expression.Expression string- Position int- // Type isn't supported for SQLite.- Type string- Unique bool+ // Position isn't supported for MongoDB.+ Position int+ // Type isn't supported for SQLite, MongoDB.+ Type string+ Unique bool+ // Primary isn't supported for MongoDB.Primary bool- // Visible isn't supported for Postgres, SQLite.+ // Visible isn't supported for Postgres, SQLite, MongoDB.Visible bool- // Comment isn't supported for SQLite.+ // Comment isn't supported for SQLite, MongoDB.Comment string}@@ -127,7 +129,8 @@ type Table struct {// CreateOptions isn't supported for Postgres, ClickHouse, Snowflake, SQLite, MongoDB.CreateOptions string// Comment isn't supported for SQLite, MongoDB.- Comment string+ Comment string+ // Columnlist isn't supported for MongoDB.ColumnList []Column// IndexList isn't supported for ClickHouse, Snowflake.IndexList []Indexdiff --git a/plugin/db/mongodb/sync.go b/plugin/db/mongodb/sync.goindex b8320107b9..61a89d1340 100644--- a/plugin/db/mongodb/sync.go+++ b/plugin/db/mongodb/sync.go@@ -98,6 +98,7 @@ func (driver *Driver) syncAllCollectionSchema(ctx context.Context, databaseNamereturn tableList, nil}+// syncCollectionSchema returns the collection schema.func (driver *Driver) syncCollectionSchema(ctx context.Context, databaseName string, collectionName string) (db.Table, error) {var table db.Tabletable.Name = collectionName@@ -121,11 +122,73 @@ func (driver *Driver) syncCollectionSchema(ctx context.Context, databaseName strtable.DataSize = int64(dataSize)table.IndexSize = int64(totalIndexSize)- // TODO(zp): sync Column and Index schema+ // Get collection index schema.+ indexList, err := driver.syncAllIndexSchema(ctx, databaseName, collectionName)+ if err != nil {+ return table, errors.Wrapf(err, "failed to get index schema of collection %s", collectionName)+ }+ table.IndexList = indexList++ // TODO(zp): sync Column schemareturn table, nil}+// syncAllIndexSchema returns all indexes schema of a collection.+func (driver *Driver) syncAllIndexSchema(ctx context.Context, databaseName, collectionName string) ([]db.Index, error) {+ database := driver.client.Database(databaseName)+ collection := database.Collection(collectionName)+ indexCursor, err := collection.Indexes().List(ctx)+ if err != nil {+ return nil, errors.Wrap(err, "failed to list indexes")+ }++ var indexList []db.Index+ for indexCursor.Next(ctx) {+ var indexInfo bson.M+ if err := indexCursor.Decode(&indexInfo); err != nil {+ return nil, errors.Wrap(err, "failed to decode index info")+ }+ index, err := getIndexSchema(indexInfo)+ if err != nil {+ return nil, err+ }+ indexList = append(indexList, index)+ }++ return indexList, nil+}++// getIndexSchema returns the index schema.+// https://www.mongodb.com/docs/manual/reference/command/listIndexes/#output+func getIndexSchema(indexInfo bson.M) (db.Index, error) {+ var index db.Index+ indexName, ok := indexInfo["name"]+ if !ok {+ return index, errors.New("cannot get index name from index info")+ }+ index.Name = indexName.(string)++ key, ok := indexInfo["key"]+ if !ok {+ return index, errors.New("cannot get index key from index info")+ }+ keystr, err := json.Marshal(key)+ if err != nil {+ return index, errors.Wrap(err, "cannot marshal index key to json")+ }+ index.Expression = string(keystr)++ unique, ok := indexInfo["unique"]+ if !ok {+ // If the unique field is not set, the index is not unique.+ unique = false+ }+ index.Unique = unique.(bool)++ return index, nil+}+// getCollectionDataSizeAndIndexSizeInByte returns the collection data size and total index size in bytes.func (driver *Driver) getCollectionDataSizeAndIndexSizeInByte(ctx context.Context, databaseName string, collectionName string) (int32, int32, error) {database := driver.client.Database(databaseName)