Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(parser): add EqualTypeName function to compare DataType with type name string (#2990)
rebelice614eee9dbc31
diff --git a/plugin/parser/ast/data_type.go b/plugin/parser/ast/data_type.goindex c0f4df9653..a1157285c4 100644--- a/plugin/parser/ast/data_type.go+++ b/plugin/parser/ast/data_type.go@@ -9,6 +9,7 @@ var (type DataType interface {Node+ EquivalentType(string) booldataTypeInterface()}@@ -18,6 +19,10 @@ type dataType struct {func (*dataType) dataTypeInterface() {}+func (*dataType) EquivalentType(_ string) bool {+ return false+}+// NumericType is the interface for numeric type.type NumericType interface {DataTypediff --git a/plugin/parser/ast/decimal.go b/plugin/parser/ast/decimal.goindex 2cba79209f..19f1c2ceaf 100644--- a/plugin/parser/ast/decimal.go+++ b/plugin/parser/ast/decimal.go@@ -1,5 +1,7 @@package ast+import "strings"+// Decimal is the struct for decimal.type Decimal struct {numericType@@ -8,3 +10,9 @@ type Decimal struct {Precision intScale int}++// EquivalentType implements the DataType interface.+func (*Decimal) EquivalentType(tp string) bool {+ tp = strings.ToLower(tp)+ return strings.HasPrefix(tp, "decimal") || strings.HasPrefix(tp, "numeric")+}diff --git a/plugin/parser/ast/float.go b/plugin/parser/ast/float.goindex a3a6648fc5..c4292f1e59 100644--- a/plugin/parser/ast/float.go+++ b/plugin/parser/ast/float.go@@ -1,5 +1,7 @@package ast+import "strings"+// Float is the struct for floating-point.type Float struct {numericType@@ -7,3 +9,15 @@ type Float struct {// the storage size(bytes)Size int}++// EquivalentType implements the DataType interface.+func (f *Float) EquivalentType(tp string) bool {+ tp = strings.ToLower(tp)+ switch f.Size {+ case 4:+ return tp == "float4" || tp == "real"+ case 8:+ return tp == "float" || tp == "float8" || tp == "double precision"+ }+ return false+}diff --git a/plugin/parser/ast/integer.go b/plugin/parser/ast/integer.goindex e2f7c71dd8..8ee2d6a85b 100644--- a/plugin/parser/ast/integer.go+++ b/plugin/parser/ast/integer.go@@ -1,5 +1,7 @@package ast+import "strings"+// Integer is the struct for integer.type Integer struct {numericType@@ -7,3 +9,17 @@ type Integer struct {// the storage size(bytes)Size int}++// EquivalentType implements the DataType interface.+func (i *Integer) EquivalentType(tp string) bool {+ tp = strings.ToLower(tp)+ switch i.Size {+ case 2:+ return tp == "smallint" || tp == "int2"+ case 4:+ return tp == "int" || tp == "int4" || tp == "integer"+ case 8:+ return tp == "bigint" || tp == "int8"+ }+ return false+}diff --git a/plugin/parser/ast/unconverted_data_type.go b/plugin/parser/ast/unconverted_data_type.goindex f5685b9046..b02a2a98d4 100644--- a/plugin/parser/ast/unconverted_data_type.go+++ b/plugin/parser/ast/unconverted_data_type.go@@ -1,5 +1,7 @@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.@@ -8,3 +10,9 @@ type UnconvertedDataType struct {Name []string}++// EquivalentType implements the DataType interface.+func (u *UnconvertedDataType) EquivalentType(tp string) bool {+ tp = strings.ToLower(tp)+ return strings.ToLower(strings.Join(u.Name, ".")) == tp+}diff --git a/plugin/parser/engine/pg/data_type_test.go b/plugin/parser/engine/pg/data_type_test.gonew file mode 100644index 0000000000..1bbb1393f4--- /dev/null+++ b/plugin/parser/engine/pg/data_type_test.go@@ -0,0 +1,58 @@+package pg++import (+ "fmt"+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/plugin/parser"+ "github.com/bytebase/bytebase/plugin/parser/ast"+)++func TestEquivalentType(t *testing.T) {+ type testData struct {+ originType string+ matchType string+ }+ tests := []testData{+ {+ originType: "int",+ matchType: "INTEGER",+ },+ {+ originType: "int",+ matchType: "int4",+ },+ {+ originType: "decimal(10, 2)",+ matchType: "decimal",+ },+ {+ originType: "float4",+ matchType: "real",+ },+ {+ originType: "float",+ matchType: "double precision",+ },+ }++ p := &PostgreSQLParser{}++ for _, test := range tests {+ stmt := fmt.Sprintf("ALTER TABLE t ADD COLUMN a %s", test.originType)+ nodeList, err := p.Parse(parser.ParseContext{}, stmt)+ require.NoError(t, err)+ require.Len(t, nodeList, 1)+ node := nodeList[0]+ alterTable, ok := node.(*ast.AlterTableStmt)+ require.True(t, ok)+ require.Len(t, alterTable.AlterItemList, 1)+ addColumn, ok := alterTable.AlterItemList[0].(*ast.AddColumnListStmt)+ require.True(t, ok)+ require.Len(t, addColumn.ColumnList, 1)+ column := addColumn.ColumnList[0]+ require.True(t, column.Type.EquivalentType(test.matchType))+ }+}