Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: ignore Postgres system schemas for differ (#7548)
rebelice1feb22202591
diff --git a/backend/plugin/db/pg/sync.go b/backend/plugin/db/pg/sync.goindex d2913bf24a..286ff5fec5 100644--- a/backend/plugin/db/pg/sync.go+++ b/backend/plugin/db/pg/sync.go@@ -22,6 +22,19 @@ import (storepb "github.com/bytebase/bytebase/proto/generated-go/store")+// SystemSchemaList is the list of system schemas that we will exclude from the schema sync.+var SystemSchemaList = []string{+ "information_schema",+ "pg_catalog",+ "pg_toast",+ "_timescaledb_cache",+ "_timescaledb_catalog",+ "_timescaledb_internal",+ "_timescaledb_config",+ "timescaledb_information",+ "timescaledb_experimental",+}+const systemSchemas = "'information_schema', 'pg_catalog', 'pg_toast', '_timescaledb_cache', '_timescaledb_catalog', '_timescaledb_internal', '_timescaledb_config', 'timescaledb_information', 'timescaledb_experimental'"// SyncInstance syncs the instance.diff --git a/backend/plugin/parser/sql/differ/pg/differ.go b/backend/plugin/parser/sql/differ/pg/differ.goindex 3bfe2e13e3..64cca489b7 100644--- a/backend/plugin/parser/sql/differ/pg/differ.go+++ b/backend/plugin/parser/sql/differ/pg/differ.go@@ -8,6 +8,7 @@ import ("github.com/pkg/errors"+ "github.com/bytebase/bytebase/backend/plugin/db/pg"parser "github.com/bytebase/bytebase/backend/plugin/parser/sql""github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"@@ -15,7 +16,8 @@ import ()var (- _ differ.SchemaDiffer = (*SchemaDiffer)(nil)+ _ differ.SchemaDiffer = (*SchemaDiffer)(nil)+ systemSchemas map[string]bool)func init() {@@ -231,6 +233,9 @@ func newTypeInfo(id int, createType *ast.CreateTypeStmt) *typeInfo {}func (m schemaMap) addTable(id int, table *ast.CreateTableStmt) error {+ if _, exists := systemSchemas[table.Name.Schema]; exists {+ return nil+ }schema, exists := m[table.Name.Schema]if !exists {return errors.Errorf("failed to add table: schema %s not found", table.Name.Schema)@@ -248,6 +253,9 @@ func (m schemaMap) getTable(schemaName string, tableName string) *tableInfo {}func (m schemaMap) addConstraint(id int, addConstraint *ast.AddConstraintStmt) error {+ if _, exists := systemSchemas[addConstraint.Table.Schema]; exists {+ return nil+ }schema, exists := m[addConstraint.Table.Schema]if !exists {return errors.Errorf("failed to add constraint: schema %s not found", addConstraint.Table.Schema)@@ -277,6 +285,9 @@ func (m schemaMap) getConstraint(schemaName string, tableName string, constraint}func (m schemaMap) addExtension(id int, extension *ast.CreateExtensionStmt) error {+ if _, exists := systemSchemas[extension.Schema]; exists {+ return nil+ }schema, exists := m[extension.Schema]if !exists {return errors.Errorf("failed to add extension: schema %s not found", extension.Schema)@@ -294,6 +305,9 @@ func (m schemaMap) getExtension(schemaName string, extensionName string) *extens}func (m schemaMap) addFunction(id int, function *ast.CreateFunctionStmt) error {+ if _, exists := systemSchemas[function.Function.Schema]; exists {+ return nil+ }schema, exists := m[function.Function.Schema]if !exists {return errors.Errorf("failed to add function: schema %s not found", function.Function.Schema)@@ -315,6 +329,9 @@ func (m schemaMap) getFunction(schemaName string, signature string) *functionInf}func (m schemaMap) addIndex(id int, index *ast.CreateIndexStmt) error {+ if _, exists := systemSchemas[index.Index.Table.Schema]; exists {+ return nil+ }schema, exists := m[index.Index.Table.Schema]if !exists {return errors.Errorf("failed to add table: schema %s not found", index.Index.Table.Schema)@@ -332,6 +349,9 @@ func (m schemaMap) getIndex(schemaName string, indexName string) *indexInfo {}func (m schemaMap) addSequence(id int, sequence *ast.CreateSequenceStmt) error {+ if _, exists := systemSchemas[sequence.SequenceDef.SequenceName.Schema]; exists {+ return nil+ }schema, exists := m[sequence.SequenceDef.SequenceName.Schema]if !exists {return errors.Errorf("failed to add sequence: schema %s not found", sequence.SequenceDef.SequenceName.Schema)@@ -349,6 +369,9 @@ func (m schemaMap) getSequence(schemaName string, sequenceName string) *sequence}func (m schemaMap) addTrigger(id int, trigger *ast.CreateTriggerStmt) error {+ if _, exists := systemSchemas[trigger.Trigger.Table.Schema]; exists {+ return nil+ }schema, exists := m[trigger.Trigger.Table.Schema]if !exists {return errors.Errorf("failed to add trigger: schema %s not found", trigger.Trigger.Table.Schema)@@ -374,6 +397,9 @@ func (m schemaMap) getTrigger(schemaName string, tableName string, triggerName s}func (m schemaMap) addType(id int, createType *ast.CreateTypeStmt) error {+ if _, exists := systemSchemas[createType.Type.TypeName().Schema]; exists {+ return nil+ }schema, exists := m[createType.Type.TypeName().Schema]if !exists {return errors.Errorf("failed to add type: schema %s not found", createType.Type.TypeName().Schema)@@ -412,6 +438,9 @@ func (m schemaMap) addSequenceOwnedBy(id int, alterStmt *ast.AlterSequenceStmt)return errors.Errorf("expect OwnedBy only, but found %v", alterStmt)}+ if _, exists := systemSchemas[alterStmt.Name.Schema]; exists {+ return nil+ }schema, exists := m[alterStmt.Name.Schema]if !exists {return errors.Errorf("failed to add sequence owned by: schema %s not found", alterStmt.Name.Schema)@@ -490,6 +519,10 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)if err != nil {return "", errors.Wrapf(err, "failed to and preprocess new statements %q", newStmt)}+ systemSchemas = make(map[string]bool)+ for _, schema := range pg.SystemSchemaList {+ systemSchemas[schema] = true+ }oldSchemaMap := make(schemaMap)oldSchemaMap["public"] = newSchemaInfo(-1, &ast.CreateSchemaStmt{Name: "public"})oldSchemaMap["public"].existsInNew = true@@ -555,6 +588,9 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)for _, node := range newNodes {switch stmt := node.(type) {case *ast.CreateTableStmt:+ if _, exists := systemSchemas[stmt.Name.Schema]; exists {+ continue+ }oldTable := oldSchemaMap.getTable(stmt.Name.Schema, stmt.Name.Name)// Add the new table.if oldTable == nil {@@ -567,6 +603,9 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)return "", err}case *ast.CreateSchemaStmt:+ if _, exists := systemSchemas[stmt.Name]; exists {+ continue+ }schema, hasSchema := oldSchemaMap[stmt.Name]if !hasSchema {diff.createSchemaList = append(diff.createSchemaList, stmt)@@ -574,6 +613,9 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)}schema.existsInNew = truecase *ast.AlterTableStmt:+ if _, exists := systemSchemas[stmt.Table.Schema]; exists {+ continue+ }for _, alterItem := range stmt.AlterItemList {switch item := alterItem.(type) {case *ast.AddConstraintStmt:@@ -604,6 +646,9 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)}}case *ast.CreateIndexStmt:+ if _, exists := systemSchemas[stmt.Index.Table.Schema]; exists {+ continue+ }oldIndex := oldSchemaMap.getIndex(stmt.Index.Table.Schema, stmt.Index.Name)// Add the new index.if oldIndex == nil {@@ -616,6 +661,9 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)return "", err}case *ast.CreateSequenceStmt:+ if _, exists := systemSchemas[stmt.SequenceDef.SequenceName.Schema]; exists {+ continue+ }oldSequence := oldSchemaMap.getSequence(stmt.SequenceDef.SequenceName.Schema, stmt.SequenceDef.SequenceName.Name)// Add the new sequence.if oldSequence == nil {@@ -631,6 +679,9 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)if !onlySetOwnedBy(stmt) {return "", errors.Errorf("expect OwnedBy only, but found %v", stmt)}+ if _, exists := systemSchemas[stmt.Name.Schema]; exists {+ continue+ }oldSequence := oldSchemaMap.getSequence(stmt.Name.Schema, stmt.Name.Name)// Add the new sequence owned by.if oldSequence == nil || oldSequence.ownedByInfo == nil {@@ -642,6 +693,9 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)return "", err}case *ast.CreateExtensionStmt:+ if _, exists := systemSchemas[stmt.Schema]; exists {+ continue+ }oldExtension := oldSchemaMap.getExtension(stmt.Schema, stmt.Name)// Add the extension.if oldExtension == nil {@@ -654,6 +708,9 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)return "", err}case *ast.CreateFunctionStmt:+ if _, exists := systemSchemas[stmt.Function.Schema]; exists {+ continue+ }signature, err := functionSignature(stmt.Function)if err != nil {return "", err@@ -670,6 +727,9 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)return "", err}case *ast.CreateTriggerStmt:+ if _, exists := systemSchemas[stmt.Trigger.Table.Schema]; exists {+ continue+ }oldTrigger := oldSchemaMap.getTrigger(stmt.Trigger.Table.Schema, stmt.Trigger.Table.Name, stmt.Trigger.Name)// Add the trigger.if oldTrigger == nil {@@ -682,6 +742,9 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)return "", err}case *ast.CreateTypeStmt:+ if _, exists := systemSchemas[stmt.Type.TypeName().Schema]; exists {+ continue+ }oldType := oldSchemaMap.getType(stmt.Type.TypeName().Schema, stmt.Type.TypeName().Name)// Add the type.if oldType == nil {diff --git a/backend/plugin/parser/sql/differ/pg/test-data/test_differ_data.yaml b/backend/plugin/parser/sql/differ/pg/test-data/test_differ_data.yamlindex 04f5f4f59d..a86cfb9da6 100644--- a/backend/plugin/parser/sql/differ/pg/test-data/test_differ_data.yaml+++ b/backend/plugin/parser/sql/differ/pg/test-data/test_differ_data.yaml@@ -438,3 +438,14 @@ALTER TYPE "public"."enum1" ADD VALUE '7';+- oldSchema: |+ create extension abc with schema pg_catalog;+ create table "public"."t1" ("a" int);+ newSchema: create table "public"."t2" ("a" int);+ diff: |++ DROP TABLE "public"."t1";++ CREATE TABLE "public"."t2" (+ "a" integer+ );+