Skip to content

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

feat: write semicolon in PostgreSQL deparse procedure (#3211)

h3n4l2b50590ee500
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 dc778c21a0..8150cb4090 100644
--- a/plugin/parser/differ/pg/test-data/test_differ_data.yaml
+++ b/plugin/parser/differ/pg/test-data/test_differ_data.yaml
@@ -18,24 +18,21 @@
);
- oldSchema: CREATE TABLE t1(a int, b char(20), c int);
newSchema: CREATE TABLE t1(a int);
- diff: |+
+ diff: |
ALTER TABLE "t1"
DROP COLUMN "b",
DROP COLUMN "c";
-
- oldSchema: CREATE TABLE t1(a int);
newSchema: CREATE TABLE t1(a int, b char(20), c int)
- diff: |+
+ diff: |
ALTER TABLE "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))
- diff: |+
+ diff: |
ALTER TABLE "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);
-
diff --git a/plugin/parser/engine/pg/deparse.go b/plugin/parser/engine/pg/deparse.go
index adccb423f0..6b0a5f7f8c 100644
--- a/plugin/parser/engine/pg/deparse.go
+++ b/plugin/parser/engine/pg/deparse.go
@@ -16,19 +16,30 @@ func deparse(context parser.DeparseContext, in ast.Node, buf *strings.Builder) e
case ast.DataType:
return deparseDataType(context, node, buf)
case *ast.CreateTableStmt:
- return deparseCreateTable(context, node, buf)
+ if err := deparseCreateTable(context, node, buf); err != nil {
+ return err
+ }
+ return buf.WriteByte(';')
case *ast.AlterTableStmt:
- return deparseAlterTable(context, node, buf)
+ if err := deparseAlterTable(context, node, buf); err != nil {
+ return err
+ }
+ return buf.WriteByte(';')
case *ast.TableDef:
return deparseTableDef(context, node, buf)
case *ast.ColumnDef:
return deparseColumnDef(context, node, buf)
case *ast.CreateSchemaStmt:
- return deparseCreateSchema(context, node, buf)
+ if err := deparseCreateSchema(context, node, buf); err != nil {
+ return err
+ }
+ return buf.WriteByte(';')
case *ast.DropSchemaStmt:
- return deparseDropSchema(context, node, buf)
+ if err := deparseDropSchema(context, node, buf); err != nil {
+ return err
+ }
+ return buf.WriteByte(';')
}
-
return errors.Errorf("failed to deparse %T", in)
}
@@ -66,9 +77,6 @@ func deparseAlterTable(context parser.DeparseContext, in *ast.AlterTableStmt, bu
}
}
}
- if _, err := buf.WriteString(";\n"); err != nil {
- return err
- }
return nil
}
diff --git a/plugin/parser/engine/pg/test-data/test_alter_table_data.yaml b/plugin/parser/engine/pg/test-data/test_alter_table_data.yaml
index 90099a38d2..f335bab1bf 100644
--- a/plugin/parser/engine/pg/test-data/test_alter_table_data.yaml
+++ b/plugin/parser/engine/pg/test-data/test_alter_table_data.yaml
@@ -1,16 +1,16 @@
- stmt: alter table t add column a int
- want: |
+ want: |-
ALTER TABLE "t"
ADD COLUMN "a" integer;
- stmt: alter table t alter column a type varchar(20)
- want: |
+ want: |-
ALTER TABLE "t"
ALTER COLUMN "a" SET DATA TYPE character varying(20);
- stmt: |-
alter table t
add column a integer,
add column b char(20)
- want: |
+ want: |-
ALTER TABLE "t"
ADD COLUMN "a" integer,
ADD COLUMN "b" character(20);
@@ -18,7 +18,7 @@
alter table t
drop column a,
drop column b
- want: |
+ want: |-
ALTER TABLE "t"
DROP COLUMN "a",
DROP COLUMN "b";
diff --git a/plugin/parser/engine/pg/test-data/test_create_schema_data.yaml b/plugin/parser/engine/pg/test-data/test_create_schema_data.yaml
index e1242e0e57..87f8979b5a 100644
--- a/plugin/parser/engine/pg/test-data/test_create_schema_data.yaml
+++ b/plugin/parser/engine/pg/test-data/test_create_schema_data.yaml
@@ -2,12 +2,12 @@
want: |-
CREATE SCHEMA "myschema" AUTHORIZATION "bytebase" CREATE TABLE "tbl" (
"id" integer
- )
+ );
- stmt: create schema if not exists myschema authorization bytebase;
- want: CREATE SCHEMA IF NOT EXISTS "myschema" AUTHORIZATION "bytebase"
+ want: CREATE SCHEMA IF NOT EXISTS "myschema" AUTHORIZATION "bytebase";
- stmt: create schema if not exists myschema
- want: CREATE SCHEMA IF NOT EXISTS "myschema"
+ want: CREATE SCHEMA IF NOT EXISTS "myschema";
- stmt: create schema if not exists "myschema" authorization bytebase
- want: CREATE SCHEMA IF NOT EXISTS "myschema" AUTHORIZATION "bytebase"
+ want: CREATE SCHEMA IF NOT EXISTS "myschema" AUTHORIZATION "bytebase";
- stmt: create schema if not exists authorization bytebase
- want: CREATE SCHEMA IF NOT EXISTS AUTHORIZATION "bytebase"
+ want: CREATE SCHEMA IF NOT EXISTS AUTHORIZATION "bytebase";
diff --git a/plugin/parser/engine/pg/test-data/test_create_table_data.yaml b/plugin/parser/engine/pg/test-data/test_create_table_data.yaml
index f7c284a677..31c831e51f 100644
--- a/plugin/parser/engine/pg/test-data/test_create_table_data.yaml
+++ b/plugin/parser/engine/pg/test-data/test_create_table_data.yaml
@@ -2,7 +2,7 @@
want: |-
CREATE TABLE "TechBook" (
"a" "user defined data type"
- )
+ );
- stmt: |-
CREATE TABLE tech_book(
a char(20),
@@ -10,7 +10,7 @@
c varchar(330),
d character varying(400),
e text
- )
+ );
want: |-
CREATE TABLE "tech_book" (
"a" character(20),
@@ -18,7 +18,7 @@
"c" character varying(330),
"d" character varying(400),
"e" text
- )
+ );
- stmt: |-
CREATE TABLE tech_book(
a smallint,
@@ -63,4 +63,4 @@
"r" smallserial,
"s" serial,
"t" numeric
- )
+ );
diff --git a/plugin/parser/engine/pg/test-data/test_drop_schema_data.yaml b/plugin/parser/engine/pg/test-data/test_drop_schema_data.yaml
index 0591160a65..10770fb73b 100644
--- a/plugin/parser/engine/pg/test-data/test_drop_schema_data.yaml
+++ b/plugin/parser/engine/pg/test-data/test_drop_schema_data.yaml
@@ -1,4 +1,4 @@
- stmt: drop schema myschema
- want: DROP SCHEMA "myschema" RESTRICT
+ want: DROP SCHEMA "myschema" RESTRICT;
- stmt: drop schema if exists s1, s2 cascade
- want: DROP SCHEMA IF EXISTS "s1", "s2" CASCADE
+ want: DROP SCHEMA IF EXISTS "s1", "s2" CASCADE;