Skip to content

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

feat: sync partition table indexes (#14659)

rebelicee0c5b57049d2
diff --git a/backend/api/v1/database_converter.go b/backend/api/v1/database_converter.go
index 8cb3c5b028..ad00129746 100644
--- a/backend/api/v1/database_converter.go
+++ b/backend/api/v1/database_converter.go
@@ -225,6 +225,21 @@ func convertStoreDatabaseMetadata(ctx context.Context, metadata *storepb.Databas
return m, nil
}
+func convertStoreIndexMetadata(index *storepb.IndexMetadata) *v1pb.IndexMetadata {
+ return &v1pb.IndexMetadata{
+ Name: index.Name,
+ Expressions: index.Expressions,
+ KeyLength: index.KeyLength,
+ Descending: index.Descending,
+ Type: index.Type,
+ Unique: index.Unique,
+ Primary: index.Primary,
+ Visible: index.Visible,
+ Comment: index.Comment,
+ Definition: index.Definition,
+ }
+}
+
func convertStoreTableMetadata(table *storepb.TableMetadata) *v1pb.TableMetadata {
t := &v1pb.TableMetadata{
Name: table.Name,
@@ -257,18 +272,7 @@ func convertStoreTableMetadata(table *storepb.TableMetadata) *v1pb.TableMetadata
if index == nil {
continue
}
- t.Indexes = append(t.Indexes, &v1pb.IndexMetadata{
- Name: index.Name,
- Expressions: index.Expressions,
- KeyLength: index.KeyLength,
- Descending: index.Descending,
- Type: index.Type,
- Unique: index.Unique,
- Primary: index.Primary,
- Visible: index.Visible,
- Comment: index.Comment,
- Definition: index.Definition,
- })
+ t.Indexes = append(t.Indexes, convertStoreIndexMetadata(index))
}
for _, foreignKey := range table.ForeignKeys {
if foreignKey == nil {
@@ -340,6 +344,12 @@ func convertStoreTablePartitionMetadata(partition *storepb.TablePartitionMetadat
default:
metadata.Type = v1pb.TablePartitionMetadata_TYPE_UNSPECIFIED
}
+ for _, index := range partition.Indexes {
+ if index == nil {
+ continue
+ }
+ metadata.Indexes = append(metadata.Indexes, convertStoreIndexMetadata(index))
+ }
for _, subpartition := range partition.Subpartitions {
if subpartition == nil {
continue
@@ -701,6 +711,21 @@ func convertV1DatabaseMetadata(ctx context.Context, metadata *v1pb.DatabaseMetad
return m, databaseConfig, nil
}
+func convertV1IndexMetadata(index *v1pb.IndexMetadata) *storepb.IndexMetadata {
+ return &storepb.IndexMetadata{
+ Name: index.Name,
+ Expressions: index.Expressions,
+ KeyLength: index.KeyLength,
+ Descending: index.Descending,
+ Type: index.Type,
+ Unique: index.Unique,
+ Primary: index.Primary,
+ Visible: index.Visible,
+ Comment: index.Comment,
+ Definition: index.Definition,
+ }
+}
+
func convertV1TableMetadata(table *v1pb.TableMetadata) *storepb.TableMetadata {
t := &storepb.TableMetadata{
Name: table.Name,
@@ -726,18 +751,7 @@ func convertV1TableMetadata(table *v1pb.TableMetadata) *storepb.TableMetadata {
if index == nil {
continue
}
- t.Indexes = append(t.Indexes, &storepb.IndexMetadata{
- Name: index.Name,
- Expressions: index.Expressions,
- KeyLength: index.KeyLength,
- Descending: index.Descending,
- Type: index.Type,
- Unique: index.Unique,
- Primary: index.Primary,
- Visible: index.Visible,
- Comment: index.Comment,
- Definition: index.Definition,
- })
+ t.Indexes = append(t.Indexes, convertV1IndexMetadata(index))
}
for _, foreignKey := range table.ForeignKeys {
if foreignKey == nil {
@@ -799,6 +813,12 @@ func convertV1TablePartitionMetadata(tablePartition *v1pb.TablePartitionMetadata
default:
metadata.Type = storepb.TablePartitionMetadata_TYPE_UNSPECIFIED
}
+ for _, index := range tablePartition.Indexes {
+ if index == nil {
+ continue
+ }
+ metadata.Indexes = append(metadata.Indexes, convertV1IndexMetadata(index))
+ }
for _, subpartition := range tablePartition.Subpartitions {
if subpartition == nil {
continue
diff --git a/backend/plugin/db/pg/sync.go b/backend/plugin/db/pg/sync.go
index 5038f04606..a4c68b6a18 100644
--- a/backend/plugin/db/pg/sync.go
+++ b/backend/plugin/db/pg/sync.go
@@ -95,13 +95,17 @@ func (driver *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchema
if err != nil {
return nil, errors.Wrapf(err, "failed to get columns from database %q", driver.databaseName)
}
- tableMap, externalTableMap, err := getTables(txn, isAtLeastPG10, columnMap)
+ indexMap, err := getIndexes(txn)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to get indexes from database %q", driver.databaseName)
+ }
+ tableMap, externalTableMap, err := getTables(txn, isAtLeastPG10, columnMap, indexMap)
if err != nil {
return nil, errors.Wrapf(err, "failed to get tables from database %q", driver.databaseName)
}
var tablePartitionMap map[db.TableKey][]*storepb.TablePartitionMetadata
if isAtLeastPG10 {
- tablePartitionMap, err = getTablePartitions(txn)
+ tablePartitionMap, err = getTablePartitions(txn, indexMap)
if err != nil {
return nil, errors.Wrapf(err, "failed to get table partitions from database %q", driver.databaseName)
}
@@ -357,11 +361,7 @@ func getListTableQuery(isAtLeastPG10 bool) string {
}
// getTables gets all tables of a database.
-func getTables(txn *sql.Tx, isAtLeastPG10 bool, columnMap map[db.TableKey][]*storepb.ColumnMetadata) (map[string][]*storepb.TableMetadata, map[string][]*storepb.ExternalTableMetadata, error) {
- indexMap, err := getIndexes(txn)
- if err != nil {
- return nil, nil, errors.Wrapf(err, "failed to get indexes")
- }
+func getTables(txn *sql.Tx, isAtLeastPG10 bool, columnMap map[db.TableKey][]*storepb.ColumnMetadata, indexMap map[db.TableKey][]*storepb.IndexMetadata) (map[string][]*storepb.TableMetadata, map[string][]*storepb.ExternalTableMetadata, error) {
foreignKeysMap, err := getForeignKeys(txn)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to get foreign keys")
@@ -467,7 +467,7 @@ WHERE
AND n.nspname NOT IN (%s)
ORDER BY c.oid;`, pgparser.SystemSchemaWhereClause)
-func getTablePartitions(txn *sql.Tx) (map[db.TableKey][]*storepb.TablePartitionMetadata, error) {
+func getTablePartitions(txn *sql.Tx, indexMap map[db.TableKey][]*storepb.IndexMetadata) (map[db.TableKey][]*storepb.TablePartitionMetadata, error) {
result := make(map[db.TableKey][]*storepb.TablePartitionMetadata)
rows, err := txn.Query(listTablePartitionQuery)
if err != nil {
@@ -488,6 +488,7 @@ func getTablePartitions(txn *sql.Tx) (map[db.TableKey][]*storepb.TablePartitionM
Name: tableName,
Expression: partKeyDef,
Value: relPartBound,
+ Indexes: indexMap[key],
}
switch strings.ToLower(partitionType) {
case "l":