Skip to content

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

chore: support sync pk information for index (#1911)

rebelice9be4d60c05bc
diff --git a/plugin/db/mysql/sync.go b/plugin/db/mysql/sync.go
index 2a2b3dcc06..c12e8965f8 100644
--- a/plugin/db/mysql/sync.go
+++ b/plugin/db/mysql/sync.go
@@ -158,6 +158,10 @@ func (driver *Driver) SyncDBSchema(ctx context.Context, databaseName string) (*d
index.Expression = expression.String
}
+ if index.Name == "PRIMARY" {
+ index.Primary = true
+ }
+
key := fmt.Sprintf("%s/%s", dbName, tableName)
if indexList, ok := indexMap[key]; ok {
indexMap[key] = append(indexList, index)
diff --git a/plugin/db/pg/sync.go b/plugin/db/pg/sync.go
index 49d479f213..1ec35b4368 100644
--- a/plugin/db/pg/sync.go
+++ b/plugin/db/pg/sync.go
@@ -82,6 +82,7 @@ type indexSchema struct {
tableName string
statement string
unique bool
+ primary bool
// methodType such as btree.
methodType string
columnExpressions []string
@@ -212,6 +213,7 @@ func (driver *Driver) SyncDBSchema(ctx context.Context, databaseName string) (*d
dbIndex.Position = i + 1
dbIndex.Type = idx.methodType
dbIndex.Unique = idx.unique
+ dbIndex.Primary = idx.primary
dbIndex.Comment = idx.comment
dbTable.IndexList = append(dbTable.IndexList, dbIndex)
}
@@ -595,11 +597,35 @@ func getIndices(txn *sql.Tx) ([]*indexSchema, error) {
if err = getIndex(txn, idx); err != nil {
return nil, fmt.Errorf("getIndex(%q, %q) got error %v", idx.schemaName, idx.name, err)
}
+
+ if err = getPrimary(txn, idx); err != nil {
+ return nil, fmt.Errorf("getPrimary(%q, %q) got error %v", idx.schemaName, idx.name, err)
+ }
}
return indices, nil
}
+func getPrimary(txn *sql.Tx, idx *indexSchema) error {
+ isPrimaryQuery := `
+ SELECT count(*)
+ FROM information_schema.table_constraints
+ WHERE constraint_schema = $1
+ AND constraint_name = $2
+ AND table_schema = $1
+ AND table_name = $3
+ AND constraint_type = 'PRIMARY KEY'
+ `
+
+ var yes int
+ if err := txn.QueryRow(isPrimaryQuery, idx.schemaName, idx.name, idx.tableName).Scan(&yes); err != nil {
+ return err
+ }
+
+ idx.primary = (yes == 1)
+ return nil
+}
+
func getIndex(txn *sql.Tx, idx *indexSchema) error {
commentQuery := fmt.Sprintf(`SELECT obj_description('"%s"."%s"'::regclass);`, idx.schemaName, idx.name)
rows, err := txn.Query(commentQuery)