25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(kingbase): preserve generated and identity column metadata (#12805)
Hand Sonicada7537e9526
diff --git a/agents/drivers/kingbase-go/kingbase_metadata.go b/agents/drivers/kingbase-go/kingbase_metadata.goindex f7f81d6687..0c9f9911dd 100644--- a/agents/drivers/kingbase-go/kingbase_metadata.go+++ b/agents/drivers/kingbase-go/kingbase_metadata.go@@ -1902,10 +1902,7 @@ func (s *server) queryCatalogColumns(schema, table string,catalog, prefix, expression string,) ([]columnInfo, error) {- identityExpression := "a.attidentity"- if s.catalogIdentityUnsupported {- identityExpression = "CAST(NULL AS varchar(1)) AS attidentity"- }+ identityExpression, generatedExpression := s.catalogColumnGenerationExpressions()relationPredicate := fmt.Sprintf("n.nspname = %s AND c.relname = %s", quoteLiteral(schema), quoteLiteral(table))if strings.TrimSpace(schema) == "" {visibilityFunction := kingbaseCatalogFunction(catalog, "sys_table_is_visible", "pg_table_is_visible")@@ -1921,14 +1918,13 @@ func (s *server) queryCatalogColumns(CASE WHEN t.typname = 'numeric' AND a.atttypmod > 0 THEN ((a.atttypmod - 4) >> 16) & 65535 END,CASE WHEN t.typname = 'numeric' AND a.atttypmod > 0 THEN (a.atttypmod - 4) & 65535 END,CASE WHEN t.typname IN ('varchar','bpchar') AND a.atttypmod > 0 THEN a.atttypmod - 4 END,- %s+ %s, %sFROM %s.%s_attribute a JOIN %s.%s_type t ON t.oid = a.atttypidJOIN %s.%s_class c ON c.oid = a.attrelid JOIN %s.%s_namespace n ON n.oid = c.relnamespaceLEFT JOIN %s.%s_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum-WHERE %s AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum`, expression, identityExpression, catalog, prefix, catalog, prefix, catalog, prefix, catalog, prefix, catalog, prefix, relationPredicate)+WHERE %s AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum`, expression, identityExpression, generatedExpression, catalog, prefix, catalog, prefix, catalog, prefix, catalog, prefix, catalog, prefix, relationPredicate)rows, err := s.metadataQuery(query)- if err != nil && !s.catalogIdentityUnsupported && isUndefinedColumn(err, "attidentity") {- s.catalogIdentityUnsupported = true+ if err != nil && s.cacheMissingColumnGenerationCapability(err) {return s.queryCatalogColumns(schema, table, catalog, prefix, expression)}if err != nil {@@ -1939,12 +1935,16 @@ WHERE %s AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum`, expression,for rows.Next() {var resolvedSchema, name, dataType stringvar nullable bool- var defaultValue, comment, identity sql.NullString+ var defaultValue, comment, identity, generated sql.NullStringvar precision, scale, length sql.NullInt64- if err := rows.Scan(&resolvedSchema, &name, &dataType, &nullable, &defaultValue, &comment, &precision, &scale, &length, &identity); err != nil {+ if err := rows.Scan(&resolvedSchema, &name, &dataType, &nullable, &defaultValue, &comment, &precision, &scale, &length, &identity, &generated); err != nil {+ return nil, err+ }+ column := columnInfo{Name: name, DataType: dataType, ResolvedSchema: stringPtr(resolvedSchema), IsNullable: nullable, ColumnDefault: nullStringPtr(defaultValue), Comment: nullStringPtr(comment), NumericPrecision: nullIntPtr(precision), NumericScale: nullIntPtr(scale), CharacterMaximumLength: nullIntPtr(length)}+ if err := applyColumnGenerationMetadata(&column, identity.String, generated.String, defaultValue); err != nil {return nil, err}- result = append(result, columnInfo{Name: name, DataType: dataType, ResolvedSchema: stringPtr(resolvedSchema), IsNullable: nullable, ColumnDefault: nullStringPtr(defaultValue), Extra: kingbaseIdentityClause(identity.String), Comment: nullStringPtr(comment), NumericPrecision: nullIntPtr(precision), NumericScale: nullIntPtr(scale), CharacterMaximumLength: nullIntPtr(length)})+ result = append(result, column)}if err := rows.Err(); err != nil {return nil, err@@ -1952,6 +1952,53 @@ WHERE %s AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum`, expression,return result, nil}+func (s *server) catalogColumnGenerationExpressions() (identity, generated string) {+ identity, generated = "a.attidentity", "a.attgenerated"+ if s.catalogIdentityUnsupported {+ identity = "CAST(NULL AS varchar(1)) AS attidentity"+ }+ if s.catalogGeneratedUnsupported {+ generated = "CAST(NULL AS varchar(1)) AS attgenerated"+ }+ return identity, generated+}++func (s *server) cacheMissingColumnGenerationCapability(err error) bool {+ switch {+ case !s.catalogIdentityUnsupported && isUndefinedColumn(err, "attidentity"):+ s.catalogIdentityUnsupported = true+ case !s.catalogGeneratedUnsupported && isUndefinedColumn(err, "attgenerated"):+ s.catalogGeneratedUnsupported = true+ default:+ return false+ }+ return true+}++func applyColumnGenerationMetadata(column *columnInfo, identity, generated string, expression sql.NullString) error {+ identity = strings.ToLower(strings.TrimSpace(identity))+ generated = strings.ToLower(strings.TrimSpace(generated))+ if generated != "" {+ if generated != "s" || identity != "" {+ return fmt.Errorf("column %q has unsupported generation metadata (identity=%q, generated=%q)", column.Name, identity, generated)+ }+ if !expression.Valid || strings.TrimSpace(expression.String) == "" {+ return fmt.Errorf("generated column %q has no generation expression", column.Name)+ }+ // attrdef stores both defaults and generated expressions. Keep generation+ // in the established extra clause, never in column_default.+ column.Extra = stringPtr("GENERATED ALWAYS AS (" + expression.String + ") STORED")+ column.ColumnDefault = nil+ } else if identity != "" {+ column.Extra = kingbaseIdentityClause(identity)+ if column.Extra == nil {+ return fmt.Errorf("column %q has unsupported identity metadata %q", column.Name, identity)+ }+ column.ColumnDefault = nil+ }+ return nil+}+func isUndefinedFunction(err error, functionName string) bool {var driverError *gokb.Errorundefined := errors.As(err, &driverError) && string(driverError.Code) == "42883"@@ -1978,6 +2025,9 @@ func (s *server) informationSchemaColumns(schema, table string, primary map[striif err == nil {return result, nil}+ if s.cacheMissingColumnGenerationCapability(err) {+ continue+ }switch {case includeColumnType && isUndefinedColumn(err, "column_type"):includeColumnType = false@@ -1985,6 +2035,8 @@ func (s *server) informationSchemaColumns(schema, table string, primary map[stricase includeUdtName && isUndefinedColumn(err, "udt_name"):includeUdtName = falses.infoUdtNameUnsupported = true+ case !s.mode.postgresCatalog && !s.usePgDefaultExpression && isUndefinedFunction(err, "sys_get_expr"):+ s.usePgDefaultExpression = truedefault:return nil, err}@@ -1992,6 +2044,17 @@ func (s *server) informationSchemaColumns(schema, table string, primary map[stri}func (s *server) queryInformationSchemaColumns(schema, table string, primary map[string]bool, includeColumnType, includeUdtName bool) ([]columnInfo, error) {+ catalog, prefix, expression := "sys_catalog", "sys", "sys_get_expr"+ if s.mode.postgresCatalog {+ catalog, prefix, expression = "pg_catalog", "pg", "pg_get_expr"+ } else if s.usePgDefaultExpression {+ expression = "pg_get_expr"+ }+ identityExpression, generatedExpression := s.catalogColumnGenerationExpressions()+ generationExpression := "NULL"+ if !s.catalogGeneratedUnsupported {+ generationExpression = fmt.Sprintf("CASE WHEN a.attgenerated <> '' THEN %s(ad.adbin, ad.adrelid) END", expression)+ }var fullDataTypeExpression stringswitch {case includeColumnType && includeUdtName:@@ -2011,12 +2074,14 @@ func (s *server) queryInformationSchemaColumns(schema, table string, primary mapfullDataTypeExpression = "NULL AS column_type"}query := fmt.Sprintf(`SELECT c.column_name, c.data_type, %s, c.is_nullable, c.column_default,- col_description(a.attrelid, a.attnum), c.numeric_precision, c.numeric_scale, c.character_maximum_length+ col_description(a.attrelid, a.attnum), c.numeric_precision, c.numeric_scale, c.character_maximum_length,+ %s, %s, %s AS generation_expressionFROM information_schema.columns c- LEFT JOIN sys_catalog.sys_namespace n ON n.nspname = c.table_schema- LEFT JOIN sys_catalog.sys_class rel ON rel.relnamespace = n.oid AND rel.relname = c.table_name- LEFT JOIN sys_catalog.sys_attribute a ON a.attrelid = rel.oid AND a.attname = c.column_name AND a.attnum > 0 AND NOT a.attisdropped- WHERE c.table_schema = %s AND c.table_name = %s ORDER BY c.ordinal_position`, fullDataTypeExpression, quoteLiteral(schema), quoteLiteral(table))+ LEFT JOIN %s.%s_namespace n ON n.nspname = c.table_schema+ LEFT JOIN %s.%s_class rel ON rel.relnamespace = n.oid AND rel.relname = c.table_name+ LEFT JOIN %s.%s_attribute a ON a.attrelid = rel.oid AND a.attname = c.column_name AND a.attnum > 0 AND NOT a.attisdropped+ LEFT JOIN %s.%s_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum+ WHERE c.table_schema = %s AND c.table_name = %s ORDER BY c.ordinal_position`, fullDataTypeExpression, identityExpression, generatedExpression, generationExpression, catalog, prefix, catalog, prefix, catalog, prefix, catalog, prefix, quoteLiteral(schema), quoteLiteral(table))rows, err := s.metadataQuery(query)if err != nil {return nil, err@@ -2025,9 +2090,9 @@ func (s *server) queryInformationSchemaColumns(schema, table string, primary mapresult := []columnInfo{}for rows.Next() {var name, dataType, nullable string- var fullDataType, defaultValue, comment sql.NullString+ var fullDataType, defaultValue, comment, identity, generated, generationValue sql.NullStringvar precision, scale, length sql.NullInt64- if err := rows.Scan(&name, &dataType, &fullDataType, &nullable, &defaultValue, &comment, &precision, &scale, &length); err != nil {+ if err := rows.Scan(&name, &dataType, &fullDataType, &nullable, &defaultValue, &comment, &precision, &scale, &length, &identity, &generated, &generationValue); err != nil {return nil, err}if !length.Valid || length.Int64 < 0 {@@ -2037,7 +2102,11 @@ func (s *server) queryInformationSchemaColumns(schema, table string, primary maplength = sql.NullInt64{Int64: int64(*parsed), Valid: true}}}- result = append(result, columnInfo{Name: name, DataType: resolvedInformationSchemaDataType(dataType, fullDataType.String), ResolvedSchema: stringPtr(schema), FullDataType: fullDataType.String, IsNullable: strings.EqualFold(nullable, "YES"), ColumnDefault: nullStringPtr(defaultValue), IsPrimaryKey: primary[strings.ToLower(name)], Comment: nullStringPtr(comment), NumericPrecision: nullIntPtr(precision), NumericScale: nullIntPtr(scale), CharacterMaximumLength: nullIntPtr(length)})+ column := columnInfo{Name: name, DataType: resolvedInformationSchemaDataType(dataType, fullDataType.String), ResolvedSchema: stringPtr(schema), FullDataType: fullDataType.String, IsNullable: strings.EqualFold(nullable, "YES"), ColumnDefault: nullStringPtr(defaultValue), IsPrimaryKey: primary[strings.ToLower(name)], Comment: nullStringPtr(comment), NumericPrecision: nullIntPtr(precision), NumericScale: nullIntPtr(scale), CharacterMaximumLength: nullIntPtr(length)}+ if err := applyColumnGenerationMetadata(&column, identity.String, generated.String, generationValue); err != nil {+ return nil, err+ }+ result = append(result, column)}return result, rows.Err()}@@ -3110,6 +3179,7 @@ WHERE n.nspname=` + quoteLiteral(schema) + ` AND c.relname=` + quoteLiteral(tablextra = "IDENTITY(" + seed.String + "," + increment.String + ")"}column.Extra = &extra+ column.ColumnDefault = nil}}}diff --git a/agents/drivers/kingbase-go/main.go b/agents/drivers/kingbase-go/main.goindex 6b287f4136..4ec70c8087 100644--- a/agents/drivers/kingbase-go/main.go+++ b/agents/drivers/kingbase-go/main.go@@ -143,6 +143,7 @@ type server struct {usePgFunctionDefinition booluseLegacyRoutineDefinition boolcatalogIdentityUnsupported bool+ catalogGeneratedUnsupported boolcatalogOIDUnsupported boolinfoColumnTypeUnsupported boolinfoUdtNameUnsupported bool@@ -488,6 +489,7 @@ func (s *server) connect(cp connectParams) error {s.usePgFunctionDefinition = falses.useLegacyRoutineDefinition = falses.catalogIdentityUnsupported = false+ s.catalogGeneratedUnsupported = falses.catalogOIDUnsupported = falses.infoColumnTypeUnsupported = falses.infoUdtNameUnsupported = false@@ -688,6 +690,7 @@ func (s *server) disconnect() error {s.usePgFunctionDefinition = falses.useLegacyRoutineDefinition = falses.catalogIdentityUnsupported = false+ s.catalogGeneratedUnsupported = falses.catalogOIDUnsupported = falses.infoColumnTypeUnsupported = falses.infoUdtNameUnsupported = falsediff --git a/agents/drivers/kingbase-go/main_test.go b/agents/drivers/kingbase-go/main_test.goindex 204eefd45b..2346d7870f 100644--- a/agents/drivers/kingbase-go/main_test.go+++ b/agents/drivers/kingbase-go/main_test.go@@ -214,8 +214,8 @@ func (connection *fallbackConn) QueryContext(_ context.Context, query string, _}if strings.Contains(query, "FROM information_schema.columns c") {return &valueRows{- columns: []string{"column_name", "data_type", "column_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length"},- rows: [][]driver.Value{{"id", "integer", "integer", "NO", nil, "primary key", int64(32), int64(0), nil}},+ columns: []string{"column_name", "data_type", "column_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length", "attidentity", "attgenerated", "generation_expression"},+ rows: [][]driver.Value{{"id", "integer", "integer", "NO", nil, "primary key", int64(32), int64(0), nil, nil, nil, nil}},}, nil}if connection.state.rejectAttidentity && strings.Contains(query, "a.attidentity") {@@ -230,8 +230,8 @@ func (connection *fallbackConn) QueryContext(_ context.Context, query string, _identity = nil}return &valueRows{- columns: []string{"resolved_schema", "column_name", "data_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length", "attidentity"},- rows: [][]driver.Value{{"public", "id", "integer", false, nil, nil, int64(32), int64(0), nil, identity}},+ columns: []string{"resolved_schema", "column_name", "data_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length", "attidentity", "attgenerated"},+ rows: [][]driver.Value{{"public", "id", "integer", false, nil, nil, int64(32), int64(0), nil, identity, nil}},}, nil}return nil, errors.New("unexpected query: " + query)@@ -1507,8 +1507,8 @@ func TestGetColumnsUsesResolvedSchemaAcrossCatalogMetadata(t *testing.T) {return nil, fmt.Errorf("explicit-schema columns query changed resolution behavior: %s", query)}return &valueRows{- columns: []string{"nspname", "attname", "format_type", "nullable", "default", "comment", "precision", "scale", "length", "identity"},- rows: [][]driver.Value{{test.resolvedSchema, "feearea", "character varying", false, nil, nil, nil, nil, nil, nil}},+ columns: []string{"nspname", "attname", "format_type", "nullable", "default", "comment", "precision", "scale", "length", "identity", "generated"},+ rows: [][]driver.Value{{test.resolvedSchema, "feearea", "character varying", false, nil, nil, nil, nil, nil, nil, nil}},}, nilcase strings.Contains(query, "FROM information_schema.table_constraints"):if !strings.Contains(query, "tc.table_schema='"+test.resolvedSchema+"'") {@@ -4030,12 +4030,12 @@ func TestInformationSchemaColumnsPreserveFullTypesInDDL(t *testing.T) {return nil, errors.New("full column type was not requested")}return &valueRows{- columns: []string{"column_name", "data_type", "column_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length"},+ columns: []string{"column_name", "data_type", "column_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length", "attidentity", "attgenerated", "generation_expression"},rows: [][]driver.Value{- {"count", "integer", "integer unsigned", "YES", nil, nil, int64(32), int64(0), nil},- {"status", "enum", "enum('new','done')", "YES", nil, nil, nil, nil, nil},- {"created_at", "datetime", "datetime(6)", "YES", nil, nil, nil, nil, nil},- {"mask", "bit", "bit(8)", "YES", nil, nil, nil, nil, nil},+ {"count", "integer", "integer unsigned", "YES", nil, nil, int64(32), int64(0), nil, nil, nil, nil},+ {"status", "enum", "enum('new','done')", "YES", nil, nil, nil, nil, nil, nil, nil, nil},+ {"created_at", "datetime", "datetime(6)", "YES", nil, nil, nil, nil, nil, nil, nil, nil},+ {"mask", "bit", "bit(8)", "YES", nil, nil, nil, nil, nil, nil, nil, nil},},}, nil}}@@ -4071,8 +4071,8 @@ func TestInformationSchemaColumnsResolveUserDefinedTypeWithoutColumnType(t *testreturn nil, errors.New("user-defined type name was not requested")}return &valueRows{- columns: []string{"column_name", "data_type", "column_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length"},- rows: [][]driver.Value{{"created_at", "USER-DEFINED", "datetime", "YES", nil, nil, nil, nil, nil}},+ columns: []string{"column_name", "data_type", "column_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length", "attidentity", "attgenerated", "generation_expression"},+ rows: [][]driver.Value{{"created_at", "USER-DEFINED", "datetime", "YES", nil, nil, nil, nil, nil, nil, nil, nil}},}, nil}}server := newServer()@@ -4139,8 +4139,8 @@ func TestInformationSchemaColumnsPreserveColumnTypeWithoutUdtName(t *testing.T)return nil, errors.New("column type fallback was not requested")}return &valueRows{- columns: []string{"column_name", "data_type", "column_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length"},- rows: [][]driver.Value{{"status", "enum", "enum('new','done')", "YES", nil, nil, nil, nil, nil}},+ columns: []string{"column_name", "data_type", "column_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length", "attidentity", "attgenerated", "generation_expression"},+ rows: [][]driver.Value{{"status", "enum", "enum('new','done')", "YES", nil, nil, nil, nil, nil, nil, nil, nil}},}, nil}}server := newServer()@@ -4178,8 +4178,8 @@ func TestInformationSchemaColumnsFallbackWithoutExtendedTypeColumns(t *testing.Treturn nil, errors.New("base type fallback was not requested")}return &valueRows{- columns: []string{"column_name", "data_type", "column_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length"},- rows: [][]driver.Value{{"label", "varchar", nil, "YES", nil, nil, nil, nil, int64(64)}},+ columns: []string{"column_name", "data_type", "column_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length", "attidentity", "attgenerated", "generation_expression"},+ rows: [][]driver.Value{{"label", "varchar", nil, "YES", nil, nil, nil, nil, int64(64), nil, nil, nil}},}, nil}}server := newServer()@@ -4223,8 +4223,8 @@ func TestInformationSchemaColumnsRetriesOnlyForMissingTypeMetadataColumns(t *tesreturn nil, errors.New("unexpected fallback query: " + query)}return &valueRows{- columns: []string{"column_name", "data_type", "column_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length"},- rows: [][]driver.Value{{"label", "varchar", nil, "YES", nil, nil, nil, nil, int64(64)}},+ columns: []string{"column_name", "data_type", "column_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length", "attidentity", "attgenerated", "generation_expression"},+ rows: [][]driver.Value{{"label", "varchar", nil, "YES", nil, nil, nil, nil, int64(64), nil, nil, nil}},}, nil}}server := newServer()@@ -5018,11 +5018,11 @@ func TestCatalogColumnsPreservesJsonAndJsonbTypes(t *testing.T) {return nil, fmt.Errorf("columns query must distinguish json from jsonb: %s", query)}return &valueRows{- columns: []string{"nspname", "attname", "data_type", "nullable", "default", "comment", "precision", "scale", "length", "identity"},+ columns: []string{"nspname", "attname", "data_type", "nullable", "default", "comment", "precision", "scale", "length", "identity", "generated"},rows: [][]driver.Value{- {"public", "data_json", "json", true, nil, nil, nil, nil, nil, nil},- {"public", "data_jsonb", "jsonb", true, nil, nil, nil, nil, nil, nil},- {"public", "data_json_arr", "json[]", true, nil, nil, nil, nil, nil, nil},+ {"public", "data_json", "json", true, nil, nil, nil, nil, nil, nil, nil},+ {"public", "data_jsonb", "jsonb", true, nil, nil, nil, nil, nil, nil, nil},+ {"public", "data_json_arr", "json[]", true, nil, nil, nil, nil, nil, nil, nil},},}, nil}}diff --git a/agents/drivers/kingbase-go/metadata_generation_test.go b/agents/drivers/kingbase-go/metadata_generation_test.gonew file mode 100644index 0000000000..36f5729595--- /dev/null+++ b/agents/drivers/kingbase-go/metadata_generation_test.go@@ -0,0 +1,255 @@+package main++import (+ "database/sql/driver"+ "encoding/json"+ "errors"+ "fmt"+ "strings"+ "testing"++ gokb "gitea.com/kingbase/gokb"+)++// Return the requested projection, so the reproduction also runs against the+// old query without failing merely because its column count is different.+func generationMetadataRows(query, identity, generated string, expression driver.Value) driver.Rows {+ if strings.Contains(query, "FROM information_schema.columns c") {+ columns := []string{"column_name", "data_type", "column_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length"}+ values := []driver.Value{"value", "integer", "integer", "YES", expression, nil, int64(32), int64(0), nil}+ if generated != "" {+ values[4] = nil+ }+ if strings.Contains(query, "AS generation_expression") {+ columns = append(columns, "attidentity", "attgenerated", "generation_expression")+ values = append(values, identity, generated, expression)+ }+ return &valueRows{columns: columns, rows: [][]driver.Value{values}}+ }+ columns := []string{"resolved_schema", "column_name", "data_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length", "attidentity"}+ values := []driver.Value{"public", "value", "integer", true, expression, nil, int64(32), int64(0), nil, identity}+ if strings.Contains(query, "a.attgenerated") || strings.Contains(query, "AS attgenerated") {+ columns = append(columns, "attgenerated")+ values = append(values, generated)+ }+ return &valueRows{columns: columns, rows: [][]driver.Value{values}}+}++func TestGenerationMetadataPreservesColumnSemanticsAcrossQueryPaths(t *testing.T) {+ for _, mode := range []string{"oracle", "mysql"} {+ for _, pgCatalog := range []bool{false, true} {+ for _, test := range []struct {+ name, identity, generated, expression, wantExtra string+ wantDefault bool+ }{+ {name: "ordinary default", expression: "42", wantDefault: true},+ {name: "serial default", expression: "nextval('public.value_seq'::regclass)", wantDefault: true},+ {name: "stored generated", generated: "s", expression: `(quantity * price)`, wantExtra: `GENERATED ALWAYS AS ((quantity * price)) STORED`},+ {name: "always identity", identity: "a", expression: "nextval('public.value_seq'::regclass)", wantExtra: "GENERATED ALWAYS AS IDENTITY"},+ {name: "default identity", identity: "d", expression: "nextval('public.value_seq'::regclass)", wantExtra: "GENERATED BY DEFAULT AS IDENTITY"},+ } {+ t.Run(fmt.Sprintf("%s/pgCatalog=%v/%s", mode, pgCatalog, test.name), func(t *testing.T) {+ catalog, prefix := "sys_catalog", "sys"+ if pgCatalog {+ catalog, prefix = "pg_catalog", "pg"+ }+ state := &metadataDriverState{query: func(query string) (driver.Rows, error) {+ if strings.Contains(query, "information_schema.table_constraints") || strings.Contains(query, "sys.identity_columns") {+ return &valueRows{columns: []string{"column_name"}}, nil+ }+ if !strings.Contains(query, catalog+"."+prefix+"_attribute") {+ return nil, errors.New("column query used the unavailable catalog: " + query)+ }+ return generationMetadataRows(query, test.identity, test.generated, test.expression), nil+ }}+ srv := newServer()+ srv.db = openMetadataDB(t, state)+ srv.mode = kingbaseMode{compatibilityMode: mode, postgresCatalog: pgCatalog, mysqlCompat: mode == "mysql"}+ columns, err := srv.getColumns("public", "orders")+ if err != nil {+ t.Fatal(err)+ }+ if len(columns) != 1 {+ t.Fatalf("unexpected columns: %#v", columns)+ }+ column := columns[0]+ if test.wantDefault {+ if column.ColumnDefault == nil || *column.ColumnDefault != test.expression || column.Extra != nil {+ t.Fatalf("ordinary/serial default was changed: %#v", column)+ }+ } else if column.ColumnDefault != nil || column.Extra == nil || *column.Extra != test.wantExtra {+ t.Fatalf("generation must not be represented as DEFAULT: %#v", column)+ }+ ddl := srv.renderTableDDL("public", "orders", columns, nil)+ if test.wantDefault && !strings.Contains(ddl, " DEFAULT "+test.expression) || !test.wantDefault && (!strings.Contains(ddl, test.wantExtra) || strings.Contains(ddl, " DEFAULT nextval") || strings.Contains(ddl, " DEFAULT (")) {+ t.Fatalf("column semantics lost in DDL: %s", ddl)+ }+ payload, err := json.Marshal(column)+ if err != nil {+ t.Fatal(err)+ }+ var decoded columnInfo+ if err := json.Unmarshal(payload, &decoded); err != nil || srv.columnDDLDefinition(decoded) != srv.columnDDLDefinition(column) {+ t.Fatalf("generation must survive the existing column wire protocol: %s (%v)", payload, err)+ }+ })+ }+ }+ }+}++func TestGeneratedCatalogCapabilityFallbackIsIndependentAndCached(t *testing.T) {+ for _, mysql := range []bool{false, true} {+ t.Run(fmt.Sprintf("mysql=%v", mysql), func(t *testing.T) {+ state := &metadataDriverState{query: func(query string) (driver.Rows, error) {+ if strings.Contains(query, "a.attidentity") {+ return nil, &gokb.Error{Code: "42703", Message: "column a.attidentity does not exist"}+ }+ if strings.Contains(query, "a.attgenerated") {+ return nil, &gokb.Error{Code: "42703", Message: "column a.attgenerated does not exist"}+ }+ return generationMetadataRows(query, "", "", "42"), nil+ }}+ srv := newServer()+ srv.db = openMetadataDB(t, state)+ srv.mode.mysqlCompat = mysql+ for call := 0; call < 2; call++ {+ var columns []columnInfo+ var err error+ if mysql {+ columns, err = srv.informationSchemaColumns("public", "orders", nil)+ } else {+ columns, err = srv.queryCatalogColumns("public", "orders", "sys_catalog", "sys", "sys_get_expr")+ }+ if err != nil || len(columns) != 1 || columns[0].ColumnDefault == nil || *columns[0].ColumnDefault != "42" || columns[0].Extra != nil {+ t.Fatalf("legacy column metadata failed: %#v (%v)", columns, err)+ }+ }+ queries := state.snapshotQueries()+ if len(queries) != 4 || strings.Contains(queries[3], "a.attidentity") || strings.Contains(queries[3], "a.attgenerated") {+ t.Fatalf("each missing capability must be probed only once: %v", queries)+ }+ })+ }+}++func TestGenerationMetadataDoesNotSilentlyDowngradeUnknownOrIncompleteColumns(t *testing.T) {+ for _, mysql := range []bool{false, true} {+ for _, test := range []struct {+ name, identity, generated string+ expression driver.Value+ }{+ {name: "unknown generation", generated: "future", expression: "a * 2"},+ {name: "missing expression", generated: "s"},+ {name: "blank expression", generated: "s", expression: " "},+ {name: "unknown identity", identity: "future"},+ } {+ t.Run(fmt.Sprintf("mysql=%v/%s", mysql, test.name), func(t *testing.T) {+ state := &metadataDriverState{query: func(query string) (driver.Rows, error) {+ return generationMetadataRows(query, test.identity, test.generated, test.expression), nil+ }}+ srv := newServer()+ srv.db = openMetadataDB(t, state)+ var err error+ if mysql {+ _, err = srv.informationSchemaColumns("public", "orders", nil)+ } else {+ _, err = srv.queryCatalogColumns("public", "orders", "sys_catalog", "sys", "sys_get_expr")+ }+ if err == nil || !strings.Contains(err.Error(), "value") {+ t.Fatalf("unsupported/incomplete generation must report the affected column: %v", err)+ }+ })+ }+ }+}++func TestMissingGeneratedCatalogFieldPreservesIdentity(t *testing.T) {+ for _, mysql := range []bool{false, true} {+ t.Run(fmt.Sprintf("mysql=%v", mysql), func(t *testing.T) {+ state := &metadataDriverState{query: func(query string) (driver.Rows, error) {+ if strings.Contains(query, "a.attgenerated") {+ return nil, &gokb.Error{Code: "42703", Message: "column a.attgenerated does not exist"}+ }+ return generationMetadataRows(query, "d", "", nil), nil+ }}+ srv := newServer()+ srv.db = openMetadataDB(t, state)+ for call := 0; call < 2; call++ {+ var columns []columnInfo+ var err error+ if mysql {+ columns, err = srv.informationSchemaColumns("public", "orders", nil)+ } else {+ columns, err = srv.queryCatalogColumns("public", "orders", "sys_catalog", "sys", "sys_get_expr")+ }+ if err != nil || len(columns) != 1 || columns[0].Extra == nil || *columns[0].Extra != "GENERATED BY DEFAULT AS IDENTITY" {+ t.Fatalf("missing generated-column support must not disable identity: %#v (%v)", columns, err)+ }+ }+ if len(state.snapshotQueries()) != 3 {+ t.Fatalf("missing attgenerated must be cached: %v", state.snapshotQueries())+ }+ })+ }+}++func TestInformationSchemaGeneratedExpressionFunctionFallbackIsCached(t *testing.T) {+ state := &metadataDriverState{query: func(query string) (driver.Rows, error) {+ if strings.Contains(query, "sys_get_expr(") {+ return nil, &gokb.Error{Code: "42883", Message: "function sys_get_expr(pg_node_tree, oid) does not exist"}+ }+ if !strings.Contains(query, "pg_get_expr(ad.adbin, ad.adrelid)") {+ return nil, errors.New("missing compatible generation expression function")+ }+ return generationMetadataRows(query, "", "s", "quantity * price"), nil+ }}+ srv := newServer()+ srv.db = openMetadataDB(t, state)+ for call := 0; call < 2; call++ {+ columns, err := srv.informationSchemaColumns("public", "orders", nil)+ if err != nil || len(columns) != 1 || columns[0].Extra == nil || *columns[0].Extra != "GENERATED ALWAYS AS (quantity * price) STORED" || columns[0].ColumnDefault != nil {+ t.Fatalf("generated expression lost in function fallback: %#v (%v)", columns, err)+ }+ }+ if queries := state.snapshotQueries(); len(queries) != 3 || !strings.Contains(queries[0], "sys_get_expr(") || strings.Contains(queries[2], "sys_get_expr(") {+ t.Fatalf("function fallback must be cached: %v", queries)+ }+}++func TestGenerationMetadataErrorsDoNotDisableCapabilities(t *testing.T) {+ for _, mysql := range []bool{false, true} {+ for _, failure := range []error{+ &gokb.Error{Code: "42501", Message: "permission denied for column attgenerated"},+ &gokb.Error{Code: "42703", Message: "column a.other_column does not exist"},+ errors.New("metadata connection reset"),+ } {+ t.Run(fmt.Sprintf("mysql=%v/%s", mysql, failure), func(t *testing.T) {+ state := &metadataDriverState{query: func(string) (driver.Rows, error) { return nil, failure }}+ srv := newServer()+ srv.db = openMetadataDB(t, state)+ var err error+ if mysql {+ _, err = srv.informationSchemaColumns("public", "orders", nil)+ } else {+ _, err = srv.queryCatalogColumns("public", "orders", "sys_catalog", "sys", "sys_get_expr")+ }+ if !errors.Is(err, failure) || len(state.snapshotQueries()) != 1 || srv.catalogIdentityUnsupported || srv.catalogGeneratedUnsupported {+ t.Fatalf("unrelated failure must propagate without downgrading metadata: %v", err)+ }+ })+ }+ }+}++func TestDisconnectResetsGeneratedColumnCapability(t *testing.T) {+ srv := newServer()+ srv.catalogGeneratedUnsupported = true+ if err := srv.disconnect(); err != nil {+ t.Fatal(err)+ }+ _, projection := srv.catalogColumnGenerationExpressions()+ if srv.catalogGeneratedUnsupported || projection != "a.attgenerated" {+ t.Fatal("new connections must reprobe generated-column support")+ }+}diff --git a/crates/dbx-sql-schema/src/schema_diff.rs b/crates/dbx-sql-schema/src/schema_diff.rsindex 4be4ba5300..f5d55cbb2a 100644--- a/crates/dbx-sql-schema/src/schema_diff.rs+++ b/crates/dbx-sql-schema/src/schema_diff.rs@@ -4262,6 +4262,20 @@ fn column_def(col: &ColumnInfo, db_type: DatabaseType, source_dialect: Option<Dicolumn_def_with_charset(col, db_type, source_dialect, false)}+fn kingbase_generation_clause(+ col: &ColumnInfo,+ db_type: DatabaseType,+ source_dialect: Option<DialectKind>,+) -> Option<&str> {+ if db_type != DatabaseType::Kingbase || !matches!(source_dialect, None | Some(DialectKind::Postgres)) {+ return None;+ }+ col.extra.as_deref().map(str::trim).filter(|extra| {+ let normalized = extra.to_ascii_lowercase();+ normalized.starts_with("generated ") || normalized == "identity" || normalized.starts_with("identity(")+ })+}+fn column_def_with_charset(col: &ColumnInfo,db_type: DatabaseType,@@ -4281,7 +4295,18 @@ fn column_def_with_charset(definition.push_str(&format!(" COLLATE {}", quote_id(collation, db_type)));}}- definition.push_str(&column_modifier_tail(&profile, col, &col.data_type, db_type, source_dialect, false));+ let generation = kingbase_generation_clause(col, db_type, source_dialect);+ if let Some(clause) = generation {+ definition.push_str(&format!(" {clause}"));+ }+ definition.push_str(&column_modifier_tail(+ &profile,+ col,+ &col.data_type,+ db_type,+ source_dialect,+ generation.is_some(),+ ));// Suffix-style auto-increment is only valid in MySQL-family ALTER clauses// (ADD/MODIFY/CHANGE). Other dialects' identity clauses are order-sensitive// inside ADD COLUMN, so keep omitting them outside the MySQL family.@@ -5749,6 +5774,15 @@ fn generate_create_table_sql(}continue;}+ if let Some(clause) = kingbase_generation_clause(col, db_type, source_dialect) {+ let mut definition = format!("{col_name} {mapped_type} {clause}");+ definition.push_str(&column_modifier_tail(&profile, col, &mapped_type, db_type, source_dialect, true));+ col_defs.push(definition);+ if col.is_primary_key {+ pk_cols.push(col_name);+ }+ continue;+ }let is_int = type_looks_integer(&mapped_type);let auto_build = apply_auto_inc_to_column_def(&profile, &col_name, &mapped_type, col, is_int);@@ -11762,6 +11796,66 @@ mod tests {assert!(sql.contains("AUTO_INCREMENT"), "MySQL ADD COLUMN must keep AUTO_INCREMENT: {sql}");}+ #[test]+ fn kingbase_agent_generation_metadata_survives_added_column_ddl() {+ for clause in [+ "GENERATED ALWAYS AS ((quantity * price)) STORED",+ "GENERATED ALWAYS AS IDENTITY",+ "GENERATED BY DEFAULT AS IDENTITY",+ "IDENTITY(10,5)",+ ] {+ let column: ColumnInfo = serde_json::from_value(serde_json::json!({+ "name": "value", "data_type": "integer", "is_nullable": false,+ "column_default": null, "is_primary_key": false, "extra": clause+ }))+ .unwrap();+ assert_eq!(+ column_def(&column, DatabaseType::Kingbase, None),+ format!("\"value\" integer {clause} NOT NULL")+ );+ let (ddl, missing) = generate_create_table_sql(+ "orders",+ &[ColumnDiff {+ diff_type: "added".to_string(),+ name: column.name.clone(),+ source: Some(column.clone()),+ target: None,+ changes: vec![],+ add_position: None,+ }],+ &[],+ &[],+ None,+ DatabaseType::Kingbase,+ Some("public"),+ Some(DialectKind::Postgres),+ &[],+ &[],+ );+ assert!(missing.is_empty());+ assert!(ddl.contains(&format!("{clause} NOT NULL")), "{ddl}");+ assert!(!ddl.contains(" DEFAULT nextval"), "{ddl}");+ }+ }++ #[test]+ fn kingbase_generation_preservation_does_not_change_other_dialects() {+ let mut column = column("value", "integer", None);+ column.extra = Some("GENERATED ALWAYS AS (price * quantity) STORED".to_string());+ for database_type in+ [DatabaseType::Postgres, DatabaseType::Mysql, DatabaseType::Oracle, DatabaseType::SqlServer]+ {+ assert!(kingbase_generation_clause(&column, database_type, None).is_none());+ }+ for source in [DialectKind::Mysql, DialectKind::Oracle, DialectKind::SqlServer] {+ assert!(kingbase_generation_clause(&column, DatabaseType::Kingbase, Some(source)).is_none());+ }+ column.column_default = Some("nextval('legacy_sequence')".to_string());+ let definition = column_def(&column, DatabaseType::Kingbase, None);+ assert!(definition.contains("GENERATED ALWAYS AS (price * quantity) STORED"));+ assert!(!definition.contains("legacy_sequence"), "{definition}");+ }+#[test]fn sqlserver_add_column_places_identity_after_type() {let mut source = column("seq", "int", None);