Skip to content

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

fix(schema/mysql): align omni catalogToProto with driver metadata conventions (#20571)

rebeliceb8c83e8307df
diff --git a/backend/plugin/schema/mysql/get_database_metadata_testcontainer_test.go b/backend/plugin/schema/mysql/get_database_metadata_testcontainer_test.go
index 37b1893076..b84cee9683 100644
--- a/backend/plugin/schema/mysql/get_database_metadata_testcontainer_test.go
+++ b/backend/plugin/schema/mysql/get_database_metadata_testcontainer_test.go
@@ -531,8 +531,9 @@ CREATE TABLE json_features (
dbMetadata, err := driver.SyncDBSchema(ctx)
require.NoError(t, err)
- // Get metadata from parser
- parsedMetadata, err := GetDatabaseMetadata(tc.ddl)
+ // Get metadata from the omni parser, the function registered for
+ // the production diff path (see get_database_metadata_omni.go).
+ parsedMetadata, err := GetDatabaseMetadataOmni(tc.ddl)
require.NoError(t, err)
// Compare metadata
diff --git a/backend/plugin/schema/mysql/testdata/walk_through.yaml b/backend/plugin/schema/mysql/testdata/walk_through.yaml
index e3570850e7..aae50ed782 100644
--- a/backend/plugin/schema/mysql/testdata/walk_through.yaml
+++ b/backend/plugin/schema/mysql/testdata/walk_through.yaml
@@ -19,8 +19,7 @@
{
"name": "test",
"schemas": [
- {
- }
+ {}
],
"characterSet": "utf8mb4",
"collation": "utf8mb4_0900_ai_ci"
@@ -67,6 +66,7 @@
{
"name": "a",
"position": 1,
+ "default": "NULL",
"nullable": true,
"type": "int"
}
@@ -125,8 +125,7 @@
{
"name": "test",
"schemas": [
- {
- }
+ {}
],
"characterSet": "utf8mb4",
"collation": "utf8mb4_polish_ci"
@@ -199,12 +198,14 @@
{
"name": "a",
"position": 1,
+ "default": "NULL",
"nullable": true,
"type": "int"
},
{
"name": "b",
"position": 2,
+ "default": "NULL",
"nullable": true,
"type": "int"
},
@@ -228,7 +229,7 @@
"a"
],
"keyLength": [
- "0"
+ "-1"
],
"descending": [
false
@@ -241,7 +242,7 @@
"b"
],
"keyLength": [
- "0"
+ "-1"
],
"descending": [
false
@@ -256,7 +257,7 @@
"c"
],
"keyLength": [
- "0"
+ "-1"
],
"descending": [
false
@@ -270,7 +271,7 @@
"d"
],
"keyLength": [
- "0"
+ "32"
],
"descending": [
false
@@ -328,6 +329,7 @@
{
"name": "b",
"position": 2,
+ "default": "NULL",
"nullable": true,
"type": "int"
}
@@ -339,7 +341,7 @@
"a"
],
"keyLength": [
- "0"
+ "-1"
],
"descending": [
false
@@ -355,7 +357,7 @@
"b"
],
"keyLength": [
- "0"
+ "-1"
],
"descending": [
false
@@ -403,6 +405,7 @@
{
"name": "b",
"position": 2,
+ "default": "NULL",
"nullable": true,
"type": "int"
}
@@ -441,12 +444,14 @@
{
"name": "a",
"position": 1,
+ "default": "NULL",
"nullable": true,
"type": "int"
},
{
"name": "b",
"position": 2,
+ "default": "NULL",
"nullable": true,
"type": "int"
}
@@ -458,7 +463,7 @@
"b"
],
"keyLength": [
- "0"
+ "-1"
],
"descending": [
false
@@ -473,7 +478,7 @@
"b"
],
"keyLength": [
- "0"
+ "-1"
],
"descending": [
false
@@ -499,8 +504,7 @@
{
"name": "test",
"schemas": [
- {
- }
+ {}
],
"characterSet": "utf8mb4",
"collation": "utf8mb4_0900_ai_ci"
@@ -586,13 +590,14 @@
{
"name": "a",
"position": 1,
- "default": "1",
+ "default": "'1'",
"nullable": true,
"type": "int"
},
{
"name": "d",
"position": 2,
+ "default": "NULL",
"nullable": true,
"type": "text",
"characterSet": "utf8mb4",
@@ -637,12 +642,14 @@
{
"name": "e",
"position": 1,
+ "default": "NULL",
"nullable": true,
"type": "int"
},
{
"name": "f",
"position": 2,
+ "default": "NULL",
"nullable": true,
"type": "text",
"characterSet": "utf8mb4",
@@ -651,6 +658,7 @@
{
"name": "c",
"position": 3,
+ "default": "NULL",
"nullable": true,
"type": "int"
}
@@ -683,6 +691,7 @@
{
"name": "a",
"position": 1,
+ "default": "NULL",
"nullable": true,
"type": "int"
}
@@ -761,9 +770,9 @@
{
"name": "c",
"position": 1,
+ "default": "' SELECT '",
"nullable": true,
"type": "varchar(20)",
- "default": "' SELECT '",
"characterSet": "utf8mb4",
"collation": "utf8mb4_0900_ai_ci"
}
@@ -796,6 +805,7 @@
{
"name": "a",
"position": 1,
+ "default": "NULL",
"nullable": true,
"type": "int"
}
@@ -809,6 +819,7 @@
{
"name": "a",
"position": 1,
+ "default": "NULL",
"nullable": true,
"type": "int"
}
@@ -852,12 +863,13 @@
{
"name": "b",
"position": 2,
- "default": "1",
+ "default": "'1'",
"type": "int"
},
{
"name": "c",
"position": 3,
+ "default": "NULL",
"nullable": true,
"type": "int"
},
@@ -871,6 +883,7 @@
{
"name": "e",
"position": 5,
+ "default": "NULL",
"nullable": true,
"type": "varchar(255)",
"characterSet": "utf8mb4",
@@ -884,7 +897,7 @@
"a"
],
"keyLength": [
- "0"
+ "-1"
],
"descending": [
false
@@ -901,8 +914,8 @@
"c"
],
"keyLength": [
- "0",
- "0"
+ "-1",
+ "-1"
],
"descending": [
false,
@@ -918,7 +931,7 @@
"c"
],
"keyLength": [
- "0"
+ "-1"
],
"descending": [
false
@@ -933,8 +946,8 @@
"e"
],
"keyLength": [
- "0",
- "0"
+ "-1",
+ "-1"
],
"descending": [
false,
@@ -1014,12 +1027,13 @@
{
"name": "a",
"position": 1,
- "default": "1",
+ "default": "'1'",
"type": "int"
},
{
"name": "b",
"position": 2,
+ "default": "NULL",
"nullable": true,
"type": "int"
},
@@ -1061,7 +1075,7 @@
"a"
],
"keyLength": [
- "0"
+ "-1"
],
"descending": [
false
@@ -1077,7 +1091,7 @@
"b"
],
"keyLength": [
- "0"
+ "-1"
],
"descending": [
false
@@ -1122,12 +1136,14 @@
{
"name": "id",
"position": 1,
+ "default": "NULL",
"nullable": true,
"type": "int"
},
{
"name": "name",
"position": 2,
+ "default": "NULL",
"nullable": true,
"type": "varchar(255)",
"characterSet": "utf8mb4",
@@ -1141,7 +1157,7 @@
"name"
],
"keyLength": [
- "0"
+ "-1"
],
"descending": [
false
@@ -1159,12 +1175,14 @@
{
"name": "id",
"position": 1,
+ "default": "NULL",
"nullable": true,
"type": "int"
},
{
"name": "t1_name",
"position": 2,
+ "default": "NULL",
"nullable": true,
"type": "varchar(255)",
"characterSet": "utf8mb4",
@@ -1178,7 +1196,7 @@
"t1_name"
],
"keyLength": [
- "0"
+ "-1"
],
"descending": [
false
@@ -1276,6 +1294,7 @@
{
"name": "c10",
"position": 10,
+ "default": "AUTO_INCREMENT",
"type": "bigint unsigned"
}
],
@@ -1286,7 +1305,7 @@
"c10"
],
"keyLength": [
- "0"
+ "-1"
],
"descending": [
false
diff --git a/backend/plugin/schema/mysql/walk_through_omni.go b/backend/plugin/schema/mysql/walk_through_omni.go
index 07c1ceaa24..44a51aba95 100644
--- a/backend/plugin/schema/mysql/walk_through_omni.go
+++ b/backend/plugin/schema/mysql/walk_through_omni.go
@@ -685,6 +685,11 @@ func tableToProto(t *catalog.Table) *storepb.TableMetadata {
// Columns.
for _, col := range t.Columns {
+ if col.Hidden == catalog.ColumnHiddenSystem {
+ // System-hidden columns (e.g. backing columns of functional
+ // indexes) are not reported by information_schema.
+ continue
+ }
colMeta := &storepb.ColumnMetadata{
Name: col.Name,
Position: int32(col.Position),
@@ -694,8 +699,18 @@ func tableToProto(t *catalog.Table) *storepb.TableMetadata {
Collation: col.Collation,
Comment: col.Comment,
}
- if col.Default != nil {
- colMeta.Default = *col.Default
+ switch {
+ case col.AutoIncrement:
+ // Match the driver convention: AUTO_INCREMENT columns report
+ // "AUTO_INCREMENT" as the default (see backend/plugin/db/mysql/sync.go).
+ colMeta.Default = "AUTO_INCREMENT"
+ case col.Default != nil:
+ colMeta.Default = normalizeOmniDefault(*col.Default, col.DefaultKind)
+ case col.Nullable:
+ // Match the driver convention: a nullable column without an explicit
+ // default has an implicit default of NULL.
+ colMeta.Default = "NULL"
+ default:
}
if col.OnUpdate != "" {
colMeta.OnUpdate = col.OnUpdate
@@ -733,7 +748,16 @@ func tableToProto(t *catalog.Table) *storepb.TableMetadata {
} else {
idxMeta.Expressions = append(idxMeta.Expressions, col.Name)
}
- idxMeta.KeyLength = append(idxMeta.KeyLength, int64(col.Length))
+ keyLength := int64(col.Length)
+ if keyLength == 0 {
+ // Match the driver convention: -1 means no prefix length specified.
+ keyLength = -1
+ }
+ if keyLength == -1 && indexType == "SPATIAL" {
+ // MySQL reports a key length of 32 for spatial index columns.
+ keyLength = 32
+ }
+ idxMeta.KeyLength = append(idxMeta.KeyLength, keyLength)
idxMeta.Descending = append(idxMeta.Descending, col.Descending)
}
table.Indexes = append(table.Indexes, idxMeta)
@@ -773,13 +797,40 @@ func tableToProto(t *catalog.Table) *storepb.TableMetadata {
return table
}
+// normalizeOmniDefault converts an omni catalog default value to the driver
+// representation produced by SyncDBSchema (see backend/plugin/db/mysql/sync.go):
+// static defaults are kept single-quoted for mysqldump compatibility and
+// expression defaults are wrapped in parentheses.
+func normalizeOmniDefault(value string, kind catalog.ColumnDefaultKind) string {
+ switch kind {
+ case catalog.ColumnDefaultConstant:
+ if strings.EqualFold(value, "NULL") {
+ return "NULL"
+ }
+ if len(value) >= 2 && strings.HasPrefix(value, "'") && strings.HasSuffix(value, "'") {
+ return value
+ }
+ return fmt.Sprintf("'%s'", value)
+ case catalog.ColumnDefaultExpression:
+ return fmt.Sprintf("(%s)", value)
+ default:
+ return value
+ }
+}
+
func partitionsToProto(p *catalog.PartitionInfo) []*storepb.TablePartitionMetadata {
+ expr := p.Expr
+ if expr == "" && len(p.Columns) > 0 {
+ // RANGE COLUMNS / LIST COLUMNS / KEY partitioning carries a column
+ // list instead of an expression; the driver reports the joined list.
+ expr = strings.Join(p.Columns, ",")
+ }
var result []*storepb.TablePartitionMetadata
for _, pd := range p.Partitions {
result = append(result, &storepb.TablePartitionMetadata{
Name: pd.Name,
Type: partitionTypeToProto(p.Type),
- Expression: p.Expr,
+ Expression: expr,
Value: pd.ValueExpr,
})
}