Skip to content

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

fix/fix-multi-schema (#8698)

rebelicecf2421839b48
diff --git a/backend/plugin/schema-engine/pg/structurer.go b/backend/plugin/schema-engine/pg/structurer.go
index df14849c54..4765e3c76b 100644
--- a/backend/plugin/schema-engine/pg/structurer.go
+++ b/backend/plugin/schema-engine/pg/structurer.go
@@ -259,11 +259,25 @@ func (s *databaseState) convertToDatabaseMetadata() *v1pb.DatabaseMetadata {
}
type schemaState struct {
- id int
+ id int
+ // ignore means CREATE SCHEMA statement for this schema is already in the target schema info.
+ // But we need the schemaState to deal with the other objects.
+ // So we cannot delete the schemaState, instead we set ignore to true.
+ ignore bool
name string
tables map[string]*tableState
}
+func (s *schemaState) printCreateSchema(buf *strings.Builder) error {
+ if s.ignore || s.name == "public" {
+ return nil
+ }
+
+ _, err := buf.WriteString(fmt.Sprintf("\nCREATE SCHEMA \"%s\";\n\n", s.name))
+
+ return err
+}
+
func newSchemaState(id int, name string) *schemaState {
return &schemaState{
id: id,
@@ -742,6 +756,9 @@ func GetDesignSchema(baselineSchema string, to *v1pb.DatabaseMetadata) (string,
if !exists {
continue
}
+ if err := schemaState.printCreateSchema(&listener.result); err != nil {
+ return "", err
+ }
// Follow the order of the input table.
for _, table := range schema.Tables {
tableState, exists := schemaState.tables[table.Name]
@@ -1146,6 +1163,44 @@ func (g *designSchemaGenerator) EnterTablelikeclause(ctx *postgres.Tablelikeclau
}
}
+func (g *designSchemaGenerator) EnterCreateschemastmt(ctx *postgres.CreateschemastmtContext) {
+ if g.err != nil {
+ return
+ }
+ if _, err := g.result.WriteString(ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: g.lastTokenIndex,
+ Stop: ctx.GetStart().GetTokenIndex() - 1,
+ })); err != nil {
+ g.err = err
+ return
+ }
+
+ g.lastTokenIndex = skipFollowingSemiIndex(ctx.GetParser().GetTokenStream(), ctx.GetStop().GetTokenIndex()+1)
+ endTokenIndex := g.lastTokenIndex - 1
+
+ var schemaName string
+ if ctx.Colid() != nil {
+ schemaName = pgparser.NormalizePostgreSQLColid(ctx.Colid())
+ } else if ctx.Optschemaname() != nil && ctx.Optschemaname().Colid() != nil {
+ schemaName = pgparser.NormalizePostgreSQLColid(ctx.Optschemaname().Colid())
+ }
+
+ schema, exists := g.to.schemas[schemaName]
+ if !exists {
+ // Skip not found schema.
+ return
+ }
+
+ schema.ignore = true
+ if _, err := g.result.WriteString(ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.Interval{
+ Start: ctx.GetStart().GetTokenIndex(),
+ Stop: endTokenIndex,
+ })); err != nil {
+ g.err = err
+ return
+ }
+}
+
func (g *designSchemaGenerator) EnterCommentstmt(ctx *postgres.CommentstmtContext) {
if g.err != nil {
return
diff --git a/backend/plugin/schema-engine/pg/testdata/get_design_schema.yaml b/backend/plugin/schema-engine/pg/testdata/get_design_schema.yaml
index 04551aad49..b8b5837f61 100644
--- a/backend/plugin/schema-engine/pg/testdata/get_design_schema.yaml
+++ b/backend/plugin/schema-engine/pg/testdata/get_design_schema.yaml
@@ -20,6 +20,37 @@
target:
name: ""
schemas:
+ - name: s1
+ tables:
+ - name: t
+ columns:
+ - name: a
+ position: 0
+ default: null
+ nullable: false
+ type: int
+ characterset: ""
+ collation: ""
+ comment: ""
+ classification: ""
+ usercomment: ""
+ effectivemaskinglevel: 0
+ indexes: []
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ classification: ""
+ usercomment: ""
+ foreignkeys: []
+ views: []
+ functions: []
+ streams: []
+ tasks: []
- name: public
tables:
- name: t
@@ -204,6 +235,13 @@
COMMENT ON COLUMN public.t4.c IS 'comment with ''; test escape';
ALTER TABLE public.t4 ADD CONSTRAINT pk_t4_a_c_d PRIMARY KEY ("a", "b", "c");
ALTER TABLE public.t4 ADD CONSTRAINT fk_t4_a_t2_b FOREIGN KEY ("b") REFERENCES "public"."t2"("b");
+ CREATE SCHEMA "s1";
+
+ CREATE TABLE "s1"."t" (
+ "a" int NOT NULL
+ );
+
+
COMMENT ON COLUMN "public"."t4"."a" IS 'this is comment'', aha';