Skip to content

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

fix: fix bugs for get foreign keys (#4020)

rebelice853ddd362606
diff --git a/plugin/db/mysql/sync.go b/plugin/db/mysql/sync.go
index e625ff5133..9ad72cd7fd 100644
--- a/plugin/db/mysql/sync.go
+++ b/plugin/db/mysql/sync.go
@@ -455,14 +455,17 @@ func (driver *Driver) getForeignKeyList(ctx context.Context, databaseName string
} else {
if fkList, ok := fkMap[buildingTable]; ok {
fkMap[buildingTable] = append(fkList, buildingFk)
- buildingTable = tableName
- buildingFk = &fk
} else {
fkMap[buildingTable] = []*storepb.ForeignKeyMetadata{buildingFk}
}
+ buildingTable = tableName
+ buildingFk = &fk
}
}
}
+ if err := fkRows.Err(); err != nil {
+ return nil, util.FormatErrorWithQuery(err, fkQuery)
+ }
if buildingFk != nil {
if fkList, ok := fkMap[buildingTable]; ok {
diff --git a/plugin/db/pg/sync.go b/plugin/db/pg/sync.go
index 60cafff45a..7640684728 100644
--- a/plugin/db/pg/sync.go
+++ b/plugin/db/pg/sync.go
@@ -375,6 +375,9 @@ func getPgForeignKeys(txn *sql.Tx) (map[string][]*storepb.ForeignKeyMetadata, er
ret[key] = []*storepb.ForeignKeyMetadata{&fkMetadata}
}
}
+ if err := rows.Err(); err != nil {
+ return nil, err
+ }
return ret, nil
}
diff --git a/tests/syncer_test.go b/tests/syncer_test.go
index 3e754ca16a..b4186b8961 100644
--- a/tests/syncer_test.go
+++ b/tests/syncer_test.go
@@ -263,20 +263,49 @@ func TestSyncerForMySQL(t *testing.T) {
b int DEFAULT NULL,
c int DEFAULT NULL,
UNIQUE KEY a (a,b,c)
- );
- CREATE TABLE tfk (
+ );
+ CREATE TABLE t1 (
+ id int PRIMARY KEY
+ );
+ CREATE TABLE tfk (
a int DEFAULT NULL,
b int DEFAULT NULL,
c int DEFAULT NULL,
KEY a (a,b,c),
- CONSTRAINT tfk_ibfk_1 FOREIGN KEY (a, b, c) REFERENCES trd (a, b, c)
- );
+ CONSTRAINT tfk_ibfk_1 FOREIGN KEY (a, b, c) REFERENCES trd (a, b, c),
+ CONSTRAINT tfk_ibfk_2 FOREIGN KEY (a) REFERENCES t1 (id)
+ );
`
expectedSchema = `{
"name":"test_sync_mysql_schema_db",
"schemas":[
{
"tables":[
+ {
+ "name":"t1",
+ "columns":[
+ {
+ "name":"id",
+ "position":1,
+ "type":"int"
+ }
+ ],
+ "indexes":[
+ {
+ "name":"PRIMARY",
+ "expressions":[
+ "id"
+ ],
+ "type":"BTREE",
+ "unique":true,
+ "primary":true,
+ "visible":true
+ }
+ ],
+ "engine":"InnoDB",
+ "collation":"utf8mb4_general_ci",
+ "dataSize":"16384"
+ },
{
"name":"tfk",
"columns":[
@@ -332,6 +361,19 @@ func TestSyncerForMySQL(t *testing.T) {
"onDelete":"NO ACTION",
"onUpdate":"NO ACTION",
"matchType":"NONE"
+ },
+ {
+ "name":"tfk_ibfk_2",
+ "columns":[
+ "a"
+ ],
+ "referencedTable":"t1",
+ "referencedColumns":[
+ "id"
+ ],
+ "onDelete":"NO ACTION",
+ "onUpdate":"NO ACTION",
+ "matchType":"NONE"
}
]
},