Skip to content

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

refactor(parser): refactor schema info in pg differ and support DROP TABLE (#3312)

rebelicefdd9ada6092e
diff --git a/plugin/parser/differ/pg/differ.go b/plugin/parser/differ/pg/differ.go
index 85236f8481..ac3645ece0 100644
--- a/plugin/parser/differ/pg/differ.go
+++ b/plugin/parser/differ/pg/differ.go
@@ -4,6 +4,7 @@ package pg
import (
"bytes"
"io"
+ "sort"
"github.com/pkg/errors"
@@ -31,6 +32,56 @@ type diffNode struct {
dropNodeList []ast.Node
}
+type tableMap map[string]*tableInfo
+type schemaMap map[string]*schemaInfo
+
+type schemaInfo struct {
+ id int
+ existsInNew bool
+ createSchema *ast.CreateSchemaStmt
+ tableMap tableMap
+}
+
+func newSchemaInfo(id int, createSchema *ast.CreateSchemaStmt) *schemaInfo {
+ return &schemaInfo{
+ id: id,
+ existsInNew: false,
+ createSchema: createSchema,
+ tableMap: make(tableMap),
+ }
+}
+
+type tableInfo struct {
+ id int
+ existsInNew bool
+ createTable *ast.CreateTableStmt
+}
+
+func newTableInfo(id int, createTable *ast.CreateTableStmt) *tableInfo {
+ return &tableInfo{
+ id: id,
+ existsInNew: false,
+ createTable: createTable,
+ }
+}
+
+func (m schemaMap) addTable(id int, table *ast.CreateTableStmt) error {
+ schema, exists := m[table.Name.Schema]
+ if !exists {
+ return errors.Errorf("failed to add table: schema %s not found", table.Name.Schema)
+ }
+ schema.tableMap[table.Name.Name] = newTableInfo(id, table)
+ return nil
+}
+
+func (m schemaMap) getTable(schemaName string, tableName string) *tableInfo {
+ schema, exists := m[schemaName]
+ if !exists {
+ return nil
+ }
+ return schema.tableMap[tableName]
+}
+
// SchemaDiff computes the schema differences between old and new schema.
func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
oldNodes, err := parser.Parse(parser.Postgres, parser.ParseContext{}, oldStmt)
@@ -42,16 +93,16 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
return "", errors.Wrapf(err, "failed to parse new statement %q", newStmt)
}
- oldTableMap := make(map[string]*ast.CreateTableStmt)
- oldSchemaMap := make(map[string]*ast.CreateSchemaStmt)
- var oldSchemaList []*ast.CreateSchemaStmt
- for _, node := range oldNodes {
+ oldSchemaMap := make(schemaMap)
+ oldSchemaMap["public"] = newSchemaInfo(-1, &ast.CreateSchemaStmt{Name: "public"})
+ for i, node := range oldNodes {
switch stmt := node.(type) {
case *ast.CreateSchemaStmt:
- oldSchemaMap[stmt.Name] = stmt
- oldSchemaList = append(oldSchemaList, stmt)
+ oldSchemaMap[stmt.Name] = newSchemaInfo(i, stmt)
case *ast.CreateTableStmt:
- oldTableMap[stmt.Name.Name] = stmt
+ if err := oldSchemaMap.addTable(i, stmt); err != nil {
+ return "", err
+ }
}
}
@@ -59,35 +110,50 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string) (string, error) {
for _, node := range newNodes {
switch stmt := node.(type) {
case *ast.CreateTableStmt:
- oldTable, hasTable := oldTableMap[stmt.Name.Name]
+ oldTable := oldSchemaMap.getTable(stmt.Name.Schema, stmt.Name.Name)
// Add the new table.
- if !hasTable {
+ if oldTable == nil {
diff.newTableList = append(diff.newTableList, stmt)
continue
}
+ oldTable.existsInNew = true
// Modify the table.
- if err := diff.modifyTable(oldTable, stmt); err != nil {
+ if err := diff.modifyTable(oldTable.createTable, stmt); err != nil {
return "", err
}
case *ast.CreateSchemaStmt:
- if _, hasSchema := oldSchemaMap[stmt.Name]; !hasSchema {
+ schema, hasSchema := oldSchemaMap[stmt.Name]
+ if !hasSchema {
diff.newSchemaList = append(diff.newSchemaList, stmt)
continue
}
- delete(oldSchemaMap, stmt.Name)
+ schema.existsInNew = true
default:
return "", errors.Errorf("unsupported statement %+v", stmt)
}
}
- // Drop the remaining old schemata.
- if dropSchemaStmt := dropSchemata(oldSchemaMap, oldSchemaList); dropSchemaStmt != nil {
- diff.dropNodeList = append(diff.dropNodeList, dropSchemaStmt)
+ // Drop remaining old objects.
+ if err := diff.dropObject(oldSchemaMap); err != nil {
+ return "", err
}
return diff.deparse()
}
+func (diff *diffNode) dropObject(oldSchemaMap schemaMap) error {
+ // Drop the renaming old schema.
+ if dropSchemaStmt := dropSchema(oldSchemaMap); dropSchemaStmt != nil {
+ diff.dropNodeList = append(diff.dropNodeList, dropSchemaStmt)
+ }
+
+ // Drop the renaming old table.
+ if dropTableStmt := dropTable(oldSchemaMap); dropTableStmt != nil {
+ diff.dropNodeList = append(diff.dropNodeList, dropTableStmt)
+ }
+ return nil
+}
+
func (diff *diffNode) modifyTable(oldTable *ast.CreateTableStmt, newTable *ast.CreateTableStmt) error {
tableName := oldTable.Name
@@ -259,19 +325,61 @@ func (diff *diffNode) deparse() (string, error) {
return buf.String(), nil
}
-func dropSchemata(m map[string]*ast.CreateSchemaStmt, l []*ast.CreateSchemaStmt) *ast.DropSchemaStmt {
- var schemaNames []string
- for _, stmt := range l {
- if _, ok := m[stmt.Name]; ok {
- schemaNames = append(schemaNames, stmt.Name)
+func dropTable(m schemaMap) *ast.DropTableStmt {
+ var tableList []*tableInfo
+ for _, schema := range m {
+ if !schema.existsInNew {
+ // dropped by DROP SCHEMA ... CASCADE statements
+ continue
+ }
+ for _, table := range schema.tableMap {
+ if table.existsInNew {
+ // no need to drop
+ continue
+ }
+ tableList = append(tableList, table)
+ }
+ }
+ if len(tableList) == 0 {
+ return nil
+ }
+ sort.Slice(tableList, func(i, j int) bool {
+ return tableList[i].id < tableList[j].id
+ })
+
+ var tableDefList []*ast.TableDef
+ for _, table := range tableList {
+ tableDefList = append(tableDefList, table.createTable.Name)
+ }
+ return &ast.DropTableStmt{
+ TableList: tableDefList,
+ // TODO(rebelice): add CASCADE here.
+ }
+}
+
+func dropSchema(m schemaMap) *ast.DropSchemaStmt {
+ var schemaList []*schemaInfo
+ for _, schema := range m {
+ if schema.createSchema.Name == "public" || schema.existsInNew {
+ continue
}
+ schemaList = append(schemaList, schema)
}
- if len(schemaNames) == 0 {
+ if len(schemaList) == 0 {
return nil
}
+ sort.Slice(schemaList, func(i, j int) bool {
+ return schemaList[i].id < schemaList[j].id
+ })
+
+ var schemaNameList []string
+ for _, schema := range schemaList {
+ schemaNameList = append(schemaNameList, schema.createSchema.Name)
+ }
return &ast.DropSchemaStmt{
IfExists: true,
- SchemaList: schemaNames,
+ SchemaList: schemaNameList,
+ Behavior: ast.DropSchemaBehaviorCascade,
}
}
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 c5c9547e39..d9d6d570e4 100644
--- a/plugin/parser/differ/pg/test-data/test_differ_data.yaml
+++ b/plugin/parser/differ/pg/test-data/test_differ_data.yaml
@@ -1,51 +1,72 @@
-- oldSchema: CREATE TABLE projects ();
+- oldSchema: |
+ CREATE TABLE public.projects ();
newSchema: |
- CREATE TABLE users (
+ CREATE TABLE public.users (
id serial PRIMARY KEY,
username TEXT NOT NULL
);
- CREATE TABLE projects ();
- CREATE TABLE repositories (
+ CREATE TABLE public.projects ();
+ CREATE TABLE public.repositories (
id serial PRIMARY KEY
);
diff: |
- CREATE TABLE users (
+ CREATE TABLE public.users (
id serial PRIMARY KEY,
username TEXT NOT NULL
);
- CREATE TABLE repositories (
+ CREATE TABLE public.repositories (
id serial PRIMARY KEY
);
-- oldSchema: CREATE TABLE t1(a int, b char(20), c int);
- newSchema: CREATE TABLE t1(a int);
+- oldSchema: CREATE TABLE public.t1(a int, b char(20), c int);
+ newSchema: CREATE TABLE public.t1(a int);
diff: |
- ALTER TABLE "t1"
+ ALTER TABLE "public"."t1"
DROP COLUMN "b",
DROP COLUMN "c";
-- oldSchema: CREATE TABLE t1(a int);
- newSchema: CREATE TABLE t1(a int, b char(20), c int)
+- oldSchema: CREATE TABLE public.t1(a int);
+ newSchema: CREATE TABLE public.t1(a int, b char(20), c int)
diff: |
- ALTER TABLE "t1"
+ ALTER TABLE "public"."t1"
ADD COLUMN "b" character(20),
ADD COLUMN "c" integer;
-- oldSchema: create table t1(a int, b bigint, c serial);
- newSchema: create table t1(a varchar(20), b serial, c bigint, d char(30))
+- oldSchema: create table public.t1(a int, b bigint, c serial);
+ newSchema: create table public.t1(a varchar(20), b serial, c bigint, d char(30))
diff: |
- ALTER TABLE "t1"
+ ALTER TABLE "public"."t1"
ALTER COLUMN "a" SET DATA TYPE character varying(20),
ALTER COLUMN "b" SET DATA TYPE serial,
ALTER COLUMN "c" SET DATA TYPE bigint,
ADD COLUMN "d" character(30);
-- oldSchema: create table t1(a int, b int NOT NULL)
- newSchema: create table t1(a int NOT NULL, b int)
+- oldSchema: create table public.t1(a int, b int NOT NULL)
+ newSchema: create table public.t1(a int NOT NULL, b int)
diff: |
- ALTER TABLE "t1"
+ ALTER TABLE "public"."t1"
ALTER COLUMN "a" SET NOT NULL,
ALTER COLUMN "b" DROP NOT NULL;
-- oldSchema: create table t1(a int, b int DEFAULT 1+2, c int DEFAULT 1)
- newSchema: create table t1(a int DEFAULT 1, b int, c int DEFAULT 1+2+3+4)
+- oldSchema: create table public.t1(a int, b int DEFAULT 1+2, c int DEFAULT 1)
+ newSchema: create table public.t1(a int DEFAULT 1, b int, c int DEFAULT 1+2+3+4)
diff: |
- ALTER TABLE "t1"
+ ALTER TABLE "public"."t1"
ALTER COLUMN "a" SET DEFAULT 1,
ALTER COLUMN "b" DROP DEFAULT,
ALTER COLUMN "c" SET DEFAULT ((1 + 2) + 3) + 4;
+- oldSchema: |
+ create schema s1;
+ create table s1.t1(a int);
+ create table s1.t2(a int, b int);
+ create schema s2;
+ create table s2.t3(a int);
+ create table s2.t4(a int);
+ newSchema: |
+ create schema s1;
+ create table s1.t2(a int);
+ create table s1.t3(a int, b int);
+ create schema s2;
+ create table s2.t2(a int);
+ create table s2.t3(a int);
+ diff: |
+ create table s1.t3(a int, b int);
+ create table s2.t2(a int);
+ ALTER TABLE "s1"."t2"
+ DROP COLUMN "b";
+ DROP TABLE "s1"."t1", "s2"."t4";
diff --git a/plugin/parser/differ/pg/test-data/test_differ_schema.yaml b/plugin/parser/differ/pg/test-data/test_differ_schema.yaml
index f4d1081c16..903bb9fcf3 100644
--- a/plugin/parser/differ/pg/test-data/test_differ_schema.yaml
+++ b/plugin/parser/differ/pg/test-data/test_differ_schema.yaml
@@ -1,7 +1,7 @@
- oldSchema: CREATE SCHEMA s1; CREATE SCHEMA s2;
newSchema: CREATE SCHEMA s1;
diff: |
- DROP SCHEMA IF EXISTS "s2";
+ DROP SCHEMA IF EXISTS "s2" CASCADE;
- oldSchema: CREATE SCHEMA s1;
newSchema: CREATE SCHEMA s1; CREATE SCHEMA s2;
diff: |
@@ -10,4 +10,4 @@
newSchema: CREATE SCHEMA s2; CREATE SCHEMA s3;
diff: |
CREATE SCHEMA IF NOT EXISTS "s3";
- DROP SCHEMA IF EXISTS "s1";
+ DROP SCHEMA IF EXISTS "s1" CASCADE;