Skip to content

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

feat(mongodb): sync mongodb index schema (part2) (#3916)

h3n4led4e973725f5
diff --git a/frontend/src/components/IndexTable.vue b/frontend/src/components/IndexTable.vue
index 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" />
<BBTableHeaderCell
v-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.go
index 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 []Index
diff --git a/plugin/db/mongodb/sync.go b/plugin/db/mongodb/sync.go
index 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, databaseName
return tableList, nil
}
+// syncCollectionSchema returns the collection schema.
func (driver *Driver) syncCollectionSchema(ctx context.Context, databaseName string, collectionName string) (db.Table, error) {
var table db.Table
table.Name = collectionName
@@ -121,11 +122,73 @@ func (driver *Driver) syncCollectionSchema(ctx context.Context, databaseName str
table.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 schema
return 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)