Skip to content

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

feat(diff): support to sync ENUM type (#3797)

rebelicee98696828539
diff --git a/plugin/parser/differ/pg/differ.go b/plugin/parser/differ/pg/differ.go
index 804f91386a..4216c3b144 100644
--- a/plugin/parser/differ/pg/differ.go
+++ b/plugin/parser/differ/pg/differ.go
@@ -40,10 +40,12 @@ type diffNode struct {
dropSequenceList []ast.Node
dropFunctionList []ast.Node
dropExtensionList []ast.Node
+ dropTypeList []ast.Node
dropSchemaList []ast.Node
// Create nodes
createSchemaList []ast.Node
+ createTypeList []ast.Node
createExtensionList []ast.Node
createFunctionList []ast.Node
createSequenceList []ast.Node
@@ -67,6 +69,7 @@ type sequenceMap map[string]*sequenceInfo
type extensionMap map[string]*extensionInfo
type functionMap map[string]*functionInfo
type triggerMap map[string]*triggerInfo
+type typeMap map[string]*typeInfo
type schemaInfo struct {
id int
@@ -77,6 +80,7 @@ type schemaInfo struct {
sequenceMap sequenceMap
extensionMap extensionMap
functionMap functionMap
+ typeMap typeMap
}
func newSchemaInfo(id int, createSchema *ast.CreateSchemaStmt) *schemaInfo {
@@ -89,6 +93,7 @@ func newSchemaInfo(id int, createSchema *ast.CreateSchemaStmt) *schemaInfo {
sequenceMap: make(sequenceMap),
extensionMap: make(extensionMap),
functionMap: make(functionMap),
+ typeMap: make(typeMap),
}
}
@@ -209,6 +214,20 @@ func newTriggerInfo(id int, createTrigger *ast.CreateTriggerStmt) *triggerInfo {
}
}
+type typeInfo struct {
+ id int
+ existsInNew bool
+ createType *ast.CreateTypeStmt
+}
+
+func newTypeInfo(id int, createType *ast.CreateTypeStmt) *typeInfo {
+ return &typeInfo{
+ id: id,
+ existsInNew: false,
+ createType: createType,
+ }
+}
+
func (m schemaMap) addTable(id int, table *ast.CreateTableStmt) error {
schema, exists := m[table.Name.Schema]
if !exists {
@@ -352,6 +371,23 @@ func (m schemaMap) getTrigger(schemaName string, tableName string, triggerName s
return table.triggerMap[triggerName]
}
+func (m schemaMap) addType(id int, createType *ast.CreateTypeStmt) error {
+ schema, exists := m[createType.Type.TypeName().Schema]
+ if !exists {
+ return errors.Errorf("failed to add type: schema %s not found", createType.Type.TypeName().Schema)
+ }
+ schema.typeMap[createType.Type.TypeName().Name] = newTypeInfo(id, createType)
+ return nil
+}
+
+func (m schemaMap) getType(schemaName string, typeName string) *typeInfo {
+ schema, exists := m[schemaName]
+ if !exists {
+ return nil
+ }
+ return schema.typeMap[typeName]
+}
+
func onlySetOwnedBy(sequence *ast.AlterSequenceStmt) bool {
return sequence.Type == nil &&
sequence.IncrementBy == nil &&
@@ -505,6 +541,10 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
if err := oldSchemaMap.addTrigger(i, stmt); err != nil {
return "", err
}
+ case *ast.CreateTypeStmt:
+ if err := oldSchemaMap.addType(i, stmt); err != nil {
+ return "", err
+ }
// TODO(rebelice): add default back here
}
}
@@ -639,6 +679,18 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
if err := diff.modifyTrigger(oldTrigger.createTrigger, stmt); err != nil {
return "", err
}
+ case *ast.CreateTypeStmt:
+ oldType := oldSchemaMap.getType(stmt.Type.TypeName().Schema, stmt.Type.TypeName().Name)
+ // Add the type.
+ if oldType == nil {
+ diff.createTypeList = append(diff.createTypeList, stmt)
+ continue
+ }
+ oldType.existsInNew = true
+ // Modify the type.
+ if err := diff.modifyType(oldType.createType, stmt); err != nil {
+ return "", err
+ }
}
}
@@ -748,6 +800,9 @@ func (diff *diffNode) dropObject(oldSchemaMap schemaMap) error {
// Drop the remaining old trigger.
diff.dropTriggerStmt(oldSchemaMap)
+ // Drop the remaining old type.
+ diff.dropTypeStmt(oldSchemaMap)
+
return nil
}
@@ -970,6 +1025,17 @@ func (diff *diffNode) modifyFunction(oldFunction *ast.CreateFunctionStmt, newFun
return nil
}
+func (diff *diffNode) modifyType(oldType *ast.CreateTypeStmt, newType *ast.CreateTypeStmt) error {
+ // TODO(rebelice): not use Text(), it only works for pg_dump.
+ if oldType.Text() != newType.Text() {
+ diff.dropTypeList = append(diff.dropTypeList, &ast.DropTypeStmt{
+ TypeNameList: []*ast.TypeNameDef{oldType.Type.TypeName()},
+ })
+ diff.createTypeList = append(diff.createTypeList, newType)
+ }
+ return nil
+}
+
func (diff *diffNode) modifyTrigger(oldTrigger *ast.CreateTriggerStmt, newTrigger *ast.CreateTriggerStmt) error {
// TODO(rebelice): not use Text(), it only works for pg_dump.
if oldTrigger.Text() != newTrigger.Text() {
@@ -1218,6 +1284,9 @@ func (diff *diffNode) deparse() (string, error) {
if err := printStmtSlice(&buf, diff.dropExtensionList); err != nil {
return "", err
}
+ if err := printStmtSlice(&buf, diff.dropTypeList); err != nil {
+ return "", err
+ }
if err := printStmtSlice(&buf, diff.dropSchemaList); err != nil {
return "", err
}
@@ -1226,6 +1295,9 @@ func (diff *diffNode) deparse() (string, error) {
if err := printStmtSlice(&buf, diff.createSchemaList); err != nil {
return "", err
}
+ if err := printStmtSliceByText(&buf, diff.createTypeList); err != nil {
+ return "", err
+ }
if err := printStmtSliceByText(&buf, diff.createExtensionList); err != nil {
return "", err
}
@@ -1394,6 +1466,31 @@ func dropFunction(m schemaMap) *ast.DropFunctionStmt {
return &ast.DropFunctionStmt{FunctionList: functionDefList}
}
+func (diff *diffNode) dropTypeStmt(m schemaMap) {
+ var typeList []*typeInfo
+ for _, schema := range m {
+ for _, tp := range schema.typeMap {
+ if tp.existsInNew {
+ // no need to drop
+ continue
+ }
+ typeList = append(typeList, tp)
+ }
+ }
+ if len(typeList) == 0 {
+ return
+ }
+ sort.Slice(typeList, func(i, j int) bool {
+ return typeList[i].id < typeList[j].id
+ })
+
+ for _, tp := range typeList {
+ diff.dropTypeList = append(diff.dropTypeList, &ast.DropTypeStmt{
+ TypeNameList: []*ast.TypeNameDef{tp.createType.Type.TypeName()},
+ })
+ }
+}
+
func (diff *diffNode) dropTriggerStmt(m schemaMap) {
var triggerList []*triggerInfo
for _, schema := range m {
diff --git a/plugin/parser/differ/pg/test-data/test_differ_data.yaml b/plugin/parser/differ/pg/test-data/test_differ_data.yaml
index 3d1a2698d9..f86e7fa3b4 100644
--- a/plugin/parser/differ/pg/test-data/test_differ_data.yaml
+++ b/plugin/parser/differ/pg/test-data/test_differ_data.yaml
@@ -315,3 +315,22 @@
DROP TRIGGER "update2" ON "public"."t";
CREATE TRIGGER update3 BEFORE UPDATE ON public.t FOR EACH ROW EXECUTE FUNCTION public.f2();
CREATE TRIGGER update4 BEFORE UPDATE ON public.t FOR EACH ROW EXECUTE FUNCTION public.f1();
+- oldSchema: |
+ CREATE TYPE public.tp1 AS ENUM ('a', 'b', 'c');
+ CREATE TYPE public.tp2 AS ENUM ('a', 'b', 'c');
+ CREATE TYPE public.tp3 AS ENUM ('a', 'b', 'c');
+ CREATE TABLE public.t1(a public.tp1, b public.tp2, c public.tp3)
+ newSchema: |
+ CREATE TYPE public.tp1 AS ENUM ('a', 'b', 'c');
+ CREATE TYPE public.tp3 AS ENUM ('a', 'b', 'c', 'd');
+ CREATE TYPE public.tp4 AS ENUM ('a', 'b', 'c');
+ CREATE TABLE public.t1(a public.tp1, b public.tp3, c public.tp4)
+ diff: |
+ DROP TYPE "public"."tp3";
+ DROP TYPE "public"."tp2";
+ CREATE TYPE public.tp3 AS ENUM ('a', 'b', 'c', 'd');
+ CREATE TYPE public.tp4 AS ENUM ('a', 'b', 'c');
+ ALTER TABLE "public"."t1"
+ ALTER COLUMN "b" SET DATA TYPE public.tp3;
+ ALTER TABLE "public"."t1"
+ ALTER COLUMN "c" SET DATA TYPE public.tp4;
diff --git a/plugin/parser/engine/pg/deparse.go b/plugin/parser/engine/pg/deparse.go
index 4c66876166..62308925fa 100644
--- a/plugin/parser/engine/pg/deparse.go
+++ b/plugin/parser/engine/pg/deparse.go
@@ -86,6 +86,11 @@ func deparse(context parser.DeparseContext, in ast.Node, buf *strings.Builder) e
return err
}
return buf.WriteByte(';')
+ case *ast.DropTypeStmt:
+ if err := deparseDropType(context, node, buf); err != nil {
+ return err
+ }
+ return buf.WriteByte(';')
}
return errors.Errorf("failed to deparse %T", in)
}
@@ -1175,6 +1180,36 @@ func deparseFunctionSignature(function *ast.FunctionDef, buf *strings.Builder) e
return buf.WriteByte(')')
}
+func deparseDropType(ctx parser.DeparseContext, in *ast.DropTypeStmt, buf *strings.Builder) error {
+ if _, err := buf.WriteString("DROP TYPE "); err != nil {
+ return err
+ }
+ if in.IfExists {
+ if _, err := buf.WriteString("IF EXISTS "); err != nil {
+ return err
+ }
+ }
+ for i, tp := range in.TypeNameList {
+ if i != 0 {
+ if _, err := buf.WriteString(", "); err != nil {
+ return err
+ }
+ }
+ if tp.Schema != "" {
+ if err := writeSurrounding(buf, tp.Schema, `"`); err != nil {
+ return err
+ }
+ if err := buf.WriteByte('.'); err != nil {
+ return err
+ }
+ }
+ if err := writeSurrounding(buf, tp.Name, `"`); err != nil {
+ return err
+ }
+ }
+ return deparseDropBehavior(ctx, in.Behavior, buf)
+}
+
func deparseDropTrigger(ctx parser.DeparseContext, in *ast.DropTriggerStmt, buf *strings.Builder) error {
if _, err := buf.WriteString("DROP TRIGGER "); err != nil {
return err
diff --git a/plugin/parser/engine/pg/deparse_test.go b/plugin/parser/engine/pg/deparse_test.go
index d6641828e5..7a7a60b642 100644
--- a/plugin/parser/engine/pg/deparse_test.go
+++ b/plugin/parser/engine/pg/deparse_test.go
@@ -77,6 +77,8 @@ func TestDeparse(t *testing.T) {
"test_function_data.yaml",
// Trigger
"test_trigger_data.yaml",
+ // Type
+ "test_type_data.yaml",
}
for _, test := range testFileList {
runDeparseTest(t, test, false /* record */)
diff --git a/plugin/parser/engine/pg/test-data/test_type_data.yaml b/plugin/parser/engine/pg/test-data/test_type_data.yaml
new file mode 100644
index 0000000000..9a9c69d5da
--- /dev/null
+++ b/plugin/parser/engine/pg/test-data/test_type_data.yaml
@@ -0,0 +1,4 @@
+- stmt: drop type if exists tp1, public.tp2;
+ want: DROP TYPE IF EXISTS "tp1", "public"."tp2";
+- stmt: drop type tp1 cascade;
+ want: DROP TYPE "tp1" CASCADE;