Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore(parser): set text deparsed by pg_query_go to unconverted data type (#3500)
rebelice3557cc6f5903
diff --git a/go.mod b/go.modindex 9e8b24b8d0..f7cb5f00ab 100644--- a/go.mod+++ b/go.mod@@ -170,4 +170,4 @@ replace github.com/pingcap/tidb => github.com/bytebase/tidb v0.0.0-2022102803595replace github.com/pingcap/tidb/parser => github.com/bytebase/tidb/parser v0.0.0-20221028035959-5d3b71eadf24-replace github.com/pganalyze/pg_query_go/v2 => github.com/bytebase/pg_query_go/v2 v2.0.0-20221115090145-10eaaf3c22b6+replace github.com/pganalyze/pg_query_go/v2 => github.com/bytebase/pg_query_go/v2 v2.0.0-20221123093650-17425d1d18b7diff --git a/go.sum b/go.sumindex 3f38a2a325..bbbed95e7f 100644--- a/go.sum+++ b/go.sum@@ -145,8 +145,8 @@ github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRgithub.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=github.com/bytebase/gh-ost v1.1.3-0.20220728080340-11d9c9027651 h1:NpoTuRY6Ckj37zTlEax/UTfvHCOdanI5/5dVCarOD18=github.com/bytebase/gh-ost v1.1.3-0.20220728080340-11d9c9027651/go.mod h1:/HsnX0+34LXVz3MxtZoW15GiwEOXz/PrEhutjb3icfE=-github.com/bytebase/pg_query_go/v2 v2.0.0-20221115090145-10eaaf3c22b6 h1:cdcvc3OYUFLylc9EKFnexiRixHryry5MG7d4jqsxBIA=-github.com/bytebase/pg_query_go/v2 v2.0.0-20221115090145-10eaaf3c22b6/go.mod h1:XAxmVqz1tEGqizcQ3YSdN90vCOHBWjJi8URL1er5+cA=+github.com/bytebase/pg_query_go/v2 v2.0.0-20221123093650-17425d1d18b7 h1:RDN6UnjTq4HR4LKrN8Yav4XfwUoGRjTocvWmzGr6Usc=+github.com/bytebase/pg_query_go/v2 v2.0.0-20221123093650-17425d1d18b7/go.mod h1:XAxmVqz1tEGqizcQ3YSdN90vCOHBWjJi8URL1er5+cA=github.com/bytebase/tidb v0.0.0-20221028035959-5d3b71eadf24 h1:XR25hLtR8TtpW/4mgP7EcOlzi8ewNY5EVan97WzDQxc=github.com/bytebase/tidb v0.0.0-20221028035959-5d3b71eadf24/go.mod h1:xrVXE+0V54QRGgrD9o+tuZmBDDx8vJpQgrPtRQvodWo=github.com/bytebase/tidb/parser v0.0.0-20221028035959-5d3b71eadf24 h1:DZPxIt2aZJ6L/gh8tH9jspTTIufBt7Wvgae1T7TmXAg=diff --git a/plugin/parser/ast/unconverted_data_type.go b/plugin/parser/ast/unconverted_data_type.goindex b02a2a98d4..81eee2ba8f 100644--- a/plugin/parser/ast/unconverted_data_type.go+++ b/plugin/parser/ast/unconverted_data_type.go@@ -1,7 +1,5 @@package ast-import "strings"-// UnconvertedDataType is the struct for unconverted data type.// TODO(rebelice): remove it.// We define this because we cannot convert all data types now.@@ -13,6 +11,5 @@ type UnconvertedDataType struct {// EquivalentType implements the DataType interface.func (u *UnconvertedDataType) EquivalentType(tp string) bool {- tp = strings.ToLower(tp)- return strings.ToLower(strings.Join(u.Name, ".")) == tp+ return u.Text() == tp}diff --git a/plugin/parser/differ/pg/test-data/test_differ_data.yaml b/plugin/parser/differ/pg/test-data/test_differ_data.yamlindex 4a4051a44e..6e7e8ee414 100644--- a/plugin/parser/differ/pg/test-data/test_differ_data.yaml+++ b/plugin/parser/differ/pg/test-data/test_differ_data.yaml@@ -128,3 +128,25 @@DROP INDEX IF EXISTS "public"."idx1" CASCADE;CREATE INDEX "idx2" ON "public"."t1" USING btree (a);CREATE INDEX "idx3" ON "public"."t1" USING btree (a, b);+- oldSchema: |+ CREATE TABLE public.t (a int, b varchar(20), c boolean, d bit(2), e jsonb);+ newSchema: |+ CREATE TABLE public.t (a integer, b varchar(20), c boolean, d bit(2), e jsonb);+ diff: ""+- oldSchema: |+ CREATE TABLE public.t (a int8, b varchar(220), c date, d varbit(2), e json, f bit(2));+ newSchema: |+ CREATE TABLE public.t (a integer, b varchar(20), c boolean, d bit(2), e jsonb, f bit(4));+ diff: |+ ALTER TABLE "public"."t"+ ALTER COLUMN "a" SET DATA TYPE integer;+ ALTER TABLE "public"."t"+ ALTER COLUMN "b" SET DATA TYPE character varying(20);+ ALTER TABLE "public"."t"+ ALTER COLUMN "c" SET DATA TYPE boolean;+ ALTER TABLE "public"."t"+ ALTER COLUMN "d" SET DATA TYPE pg_catalog.bit(2);+ ALTER TABLE "public"."t"+ ALTER COLUMN "e" SET DATA TYPE jsonb;+ ALTER TABLE "public"."t"+ ALTER COLUMN "f" SET DATA TYPE pg_catalog.bit(4);diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.goindex 2a750d1667..58080dc6e7 100644--- a/plugin/parser/engine/pg/convert.go+++ b/plugin/parser/engine/pg/convert.go@@ -104,10 +104,14 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, errif !ok {return nil, parser.NewConvertErrorf("expected ColumnDef but found %t", alterCmd.Def.Node)}+ dataType, err := convertDataType(column.ColumnDef.TypeName)+ if err != nil {+ return nil, err+ }alterColumType := &ast.AlterColumnTypeStmt{Table: alterTable.Table,ColumnName: alterCmd.Name,- Type: convertDataType(column.ColumnDef.TypeName),+ Type: dataType,}alterTable.AlterItemList = append(alterTable.AlterItemList, alterColumType)@@ -1064,7 +1068,10 @@ func convertColumnDef(in *pgquery.Node_ColumnDef) (*ast.ColumnDef, error) {column := &ast.ColumnDef{ColumnName: in.ColumnDef.Colname,}- columnType := convertDataType(in.ColumnDef.TypeName)+ columnType, err := convertDataType(in.ColumnDef.TypeName)+ if err != nil {+ return nil, err+ }column.Type = columnTypefor _, cons := range in.ColumnDef.Constraints {@@ -1104,50 +1111,63 @@ func stripPgCatalogPrefix(tp *pgquery.TypeName) *pgquery.TypeName {return tp}-func convertDataType(tp *pgquery.TypeName) ast.DataType {- tp = stripPgCatalogPrefix(tp)- if len(tp.Names) == 1 {- name, ok := tp.Names[0].Node.(*pgquery.Node_String_)- if !ok {- return &ast.UnconvertedDataType{}- }- s := name.String_.Str- switch {- case strings.HasPrefix(s, "int"):- size, err := strconv.Atoi(s[3:])- if err != nil {- return convertToUnconvertedDataType(tp)- }- return &ast.Integer{Size: size}- case strings.HasPrefix(s, "float"):- size, err := strconv.Atoi(s[5:])- if err != nil {- return convertToUnconvertedDataType(tp)+func convertDataType(tp *pgquery.TypeName) (ast.DataType, error) {+ text, err := pgquery.DeparseNode(pgquery.DeparseTypeDataType, &pgquery.Node{Node: &pgquery.Node_TypeName{TypeName: tp}})+ if err != nil {+ return nil, err+ }++ dataType := func() ast.DataType {+ tp = stripPgCatalogPrefix(tp)+ if len(tp.Names) == 1 {+ name, ok := tp.Names[0].Node.(*pgquery.Node_String_)+ if !ok {+ return &ast.UnconvertedDataType{}}- return &ast.Float{Size: size}- case s == "serial":- return &ast.Serial{Size: 4}- case s == "smallserial":- return &ast.Serial{Size: 2}- case s == "bigserial":- return &ast.Serial{Size: 8}- case strings.HasPrefix(s, "serial"):- size, err := strconv.Atoi(s[6:])- if err != nil {- return convertToUnconvertedDataType(tp)+ s := name.String_.Str+ switch {+ case strings.HasPrefix(s, "int"):+ size, err := strconv.Atoi(s[3:])+ if err != nil {+ return convertToUnconvertedDataType(tp)+ }+ return &ast.Integer{Size: size}+ case strings.HasPrefix(s, "float"):+ size, err := strconv.Atoi(s[5:])+ if err != nil {+ return convertToUnconvertedDataType(tp)+ }+ return &ast.Float{Size: size}+ case s == "serial":+ return &ast.Serial{Size: 4}+ case s == "smallserial":+ return &ast.Serial{Size: 2}+ case s == "bigserial":+ return &ast.Serial{Size: 8}+ case strings.HasPrefix(s, "serial"):+ size, err := strconv.Atoi(s[6:])+ if err != nil {+ return convertToUnconvertedDataType(tp)+ }+ return &ast.Serial{Size: size}+ case s == "numeric":+ return convertToDecimal(tp.Typmods)+ case s == "bpchar":+ return convertToCharacter(tp.Typmods)+ case s == "varchar":+ return convertToVarchar(tp.Typmods)+ case s == "text":+ return &ast.Text{}}- return &ast.Serial{Size: size}- case s == "numeric":- return convertToDecimal(tp.Typmods)- case s == "bpchar":- return convertToCharacter(tp.Typmods)- case s == "varchar":- return convertToVarchar(tp.Typmods)- case s == "text":- return &ast.Text{}}+ return convertToUnconvertedDataType(tp)+ }()++ // For UnconvertedDataType, we use the text deparsed by pg_query_go+ if _, ok := dataType.(*ast.UnconvertedDataType); ok {+ dataType.SetText(text)}- return convertToUnconvertedDataType(tp)+ return dataType, nil}func convertToUnconvertedDataType(tp *pgquery.TypeName) ast.DataType {@@ -1259,7 +1279,10 @@ func convertDefElemToSeqType(defElem *pgquery.DefElem) (*ast.Integer, error) {if len(typeNameNode.TypeName.Names) != 2 {return nil, parser.NewConvertErrorf("expected TypeName with 2 names but found %d", len(typeNameNode.TypeName.Names))}- dataType := convertDataType(typeNameNode.TypeName)+ dataType, err := convertDataType(typeNameNode.TypeName)+ if err != nil {+ return nil, err+ }// Sequence type should be int2(smallint), int4(integer), or int8(bigint)intType, ok := dataType.(*ast.Integer)if !ok {diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.goindex f0a8a38981..e541d9312e 100644--- a/plugin/parser/engine/pg/convert_test.go+++ b/plugin/parser/engine/pg/convert_test.go@@ -48,6 +48,12 @@ func runTests(t *testing.T, tests []testData) {}}+func newUnconvertedDataType(name []string, text string) *ast.UnconvertedDataType {+ tp := &ast.UnconvertedDataType{Name: name}+ tp.SetText(text)+ return tp+}+func TestPGConvertCreateTableStmt(t *testing.T) {tests := []testData{{@@ -220,7 +226,7 @@ func TestPGConvertCreateTableStmt(t *testing.T) {},{ColumnName: "u",- Type: &ast.UnconvertedDataType{Name: []string{"user defined data type"}},+ Type: newUnconvertedDataType([]string{"user defined data type"}, `"user defined data type"`),},},},diff --git a/plugin/parser/engine/pg/deparse.go b/plugin/parser/engine/pg/deparse.goindex c006230d4a..68d98bfee6 100644--- a/plugin/parser/engine/pg/deparse.go+++ b/plugin/parser/engine/pg/deparse.go@@ -908,11 +908,7 @@ func deparseDataType(_ parser.DeparseContext, in ast.DataType, buf *strings.Builreturn err}case *ast.UnconvertedDataType:- var nameList []string- for _, name := range node.Name {- nameList = append(nameList, fmt.Sprintf(`"%s"`, name))- }- if _, err := buf.WriteString(strings.Join(nameList, ".")); err != nil {+ if _, err := buf.WriteString(node.Text()); err != nil {return err}default: