Skip to content

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

chore: keep the object order as imput for schema design (#7439)

rebelicecc8be157607f
diff --git a/backend/api/v1/schema_design_service.go b/backend/api/v1/schema_design_service.go
index fc31255357..bd26ca037a 100644
--- a/backend/api/v1/schema_design_service.go
+++ b/backend/api/v1/schema_design_service.go
@@ -508,13 +508,17 @@ func convertToDatabaseState(database *v1pb.DatabaseMetadata) *databaseState {
}
func (s *databaseState) convertToDatabaseMetadata() *v1pb.DatabaseMetadata {
- schemas := []*v1pb.SchemaMetadata{}
+ schemaStates := []*schemaState{}
for _, schema := range s.schemas {
- schemas = append(schemas, schema.convertToSchemaMetadata())
+ schemaStates = append(schemaStates, schema)
}
- sort.Slice(schemas, func(i, j int) bool {
- return schemas[i].Name < schemas[j].Name
+ sort.Slice(schemaStates, func(i, j int) bool {
+ return schemaStates[i].id < schemaStates[j].id
})
+ schemas := []*v1pb.SchemaMetadata{}
+ for _, schema := range schemaStates {
+ schemas = append(schemas, schema.convertToSchemaMetadata())
+ }
return &v1pb.DatabaseMetadata{
Name: s.name,
Schemas: schemas,
@@ -524,6 +528,7 @@ func (s *databaseState) convertToDatabaseMetadata() *v1pb.DatabaseMetadata {
}
type schemaState struct {
+ id int
name string
tables map[string]*tableState
}
@@ -537,20 +542,24 @@ func newSchemaState() *schemaState {
func convertToSchemaState(schema *v1pb.SchemaMetadata) *schemaState {
state := newSchemaState()
state.name = schema.Name
- for _, table := range schema.Tables {
- state.tables[table.Name] = convertToTableState(table)
+ for i, table := range schema.Tables {
+ state.tables[table.Name] = convertToTableState(i, table)
}
return state
}
func (s *schemaState) convertToSchemaMetadata() *v1pb.SchemaMetadata {
- tables := []*v1pb.TableMetadata{}
+ tableStates := []*tableState{}
for _, table := range s.tables {
- tables = append(tables, table.convertToTableMetadata())
+ tableStates = append(tableStates, table)
}
- sort.Slice(tables, func(i, j int) bool {
- return tables[i].Name < tables[j].Name
+ sort.Slice(tableStates, func(i, j int) bool {
+ return tableStates[i].id < tableStates[j].id
})
+ tables := []*v1pb.TableMetadata{}
+ for _, table := range tableStates {
+ tables = append(tables, table.convertToTableMetadata())
+ }
return &v1pb.SchemaMetadata{
Name: s.name,
Tables: tables,
@@ -563,6 +572,7 @@ func (s *schemaState) convertToSchemaMetadata() *v1pb.SchemaMetadata {
}
type tableState struct {
+ id int
name string
columns map[string]*columnState
indexes map[string]*indexState
@@ -635,8 +645,9 @@ func (t *tableState) toString(buf *strings.Builder) error {
return nil
}
-func newTableState(name string) *tableState {
+func newTableState(id int, name string) *tableState {
return &tableState{
+ id: id,
name: name,
columns: make(map[string]*columnState),
indexes: make(map[string]*indexState),
@@ -644,44 +655,56 @@ func newTableState(name string) *tableState {
}
}
-func convertToTableState(table *v1pb.TableMetadata) *tableState {
- state := newTableState(table.Name)
- for _, column := range table.Columns {
- state.columns[column.Name] = convertToColumnState(column)
+func convertToTableState(id int, table *v1pb.TableMetadata) *tableState {
+ state := newTableState(id, table.Name)
+ for i, column := range table.Columns {
+ state.columns[column.Name] = convertToColumnState(i, column)
}
- for _, index := range table.Indexes {
- state.indexes[index.Name] = convertToIndexState(index)
+ for i, index := range table.Indexes {
+ state.indexes[index.Name] = convertToIndexState(i, index)
}
- for _, fk := range table.ForeignKeys {
- state.foreignKeys[fk.Name] = convertToForeignKeyState(fk)
+ for i, fk := range table.ForeignKeys {
+ state.foreignKeys[fk.Name] = convertToForeignKeyState(i, fk)
}
return state
}
func (t *tableState) convertToTableMetadata() *v1pb.TableMetadata {
- columns := []*v1pb.ColumnMetadata{}
+ columnStates := []*columnState{}
for _, column := range t.columns {
- columns = append(columns, column.convertToColumnMetadata())
+ columnStates = append(columnStates, column)
}
- sort.Slice(columns, func(i, j int) bool {
- return columns[i].Name < columns[j].Name
+ sort.Slice(columnStates, func(i, j int) bool {
+ return columnStates[i].id < columnStates[j].id
})
+ columns := []*v1pb.ColumnMetadata{}
+ for _, column := range columnStates {
+ columns = append(columns, column.convertToColumnMetadata())
+ }
- indexes := []*v1pb.IndexMetadata{}
+ indexStates := []*indexState{}
for _, index := range t.indexes {
- indexes = append(indexes, index.convertToIndexMetadata())
+ indexStates = append(indexStates, index)
}
- sort.Slice(indexes, func(i, j int) bool {
- return indexes[i].Name < indexes[j].Name
+ sort.Slice(indexStates, func(i, j int) bool {
+ return indexStates[i].id < indexStates[j].id
})
+ indexes := []*v1pb.IndexMetadata{}
+ for _, index := range indexStates {
+ indexes = append(indexes, index.convertToIndexMetadata())
+ }
- fks := []*v1pb.ForeignKeyMetadata{}
+ fkStates := []*foreignKeyState{}
for _, fk := range t.foreignKeys {
- fks = append(fks, fk.convertToForeignKeyMetadata())
+ fkStates = append(fkStates, fk)
}
- sort.Slice(fks, func(i, j int) bool {
- return fks[i].Name < fks[j].Name
+ sort.Slice(fkStates, func(i, j int) bool {
+ return fkStates[i].id < fkStates[j].id
})
+ fks := []*v1pb.ForeignKeyMetadata{}
+ for _, fk := range fkStates {
+ fks = append(fks, fk.convertToForeignKeyMetadata())
+ }
return &v1pb.TableMetadata{
Name: t.name,
@@ -692,6 +715,7 @@ func (t *tableState) convertToTableMetadata() *v1pb.TableMetadata {
}
type foreignKeyState struct {
+ id int
name string
columns []string
referencedTable string
@@ -707,8 +731,9 @@ func (f *foreignKeyState) convertToForeignKeyMetadata() *v1pb.ForeignKeyMetadata
}
}
-func convertToForeignKeyState(foreignKey *v1pb.ForeignKeyMetadata) *foreignKeyState {
+func convertToForeignKeyState(id int, foreignKey *v1pb.ForeignKeyMetadata) *foreignKeyState {
return &foreignKeyState{
+ id: id,
name: foreignKey.Name,
columns: foreignKey.Columns,
referencedTable: foreignKey.ReferencedTable,
@@ -774,6 +799,7 @@ func (f *foreignKeyState) toString(buf *strings.Builder) error {
}
type indexState struct {
+ id int
name string
keys []string
primary bool
@@ -791,8 +817,9 @@ func (i *indexState) convertToIndexMetadata() *v1pb.IndexMetadata {
}
}
-func convertToIndexState(index *v1pb.IndexMetadata) *indexState {
+func convertToIndexState(id int, index *v1pb.IndexMetadata) *indexState {
return &indexState{
+ id: id,
name: index.Name,
keys: index.Expressions,
primary: index.Primary,
@@ -824,6 +851,7 @@ func (i *indexState) toString(buf *strings.Builder) error {
}
type columnState struct {
+ id int
name string
tp string
defaultValue *string
@@ -870,8 +898,9 @@ func (c *columnState) convertToColumnMetadata() *v1pb.ColumnMetadata {
return result
}
-func convertToColumnState(column *v1pb.ColumnMetadata) *columnState {
+func convertToColumnState(id int, column *v1pb.ColumnMetadata) *columnState {
result := &columnState{
+ id: id,
name: column.Name,
tp: column.Type,
nullable: column.Nullable,
@@ -904,7 +933,7 @@ func (t *mysqlTransformer) EnterCreateTable(ctx *mysql.CreateTableContext) {
return
}
- schema.tables[tableName] = newTableState(tableName)
+ schema.tables[tableName] = newTableState(len(schema.tables), tableName)
t.currentTable = tableName
}
diff --git a/backend/api/v1/testdata/check.yaml b/backend/api/v1/testdata/check.yaml
index ab9d720fab..4abc58592e 100644
--- a/backend/api/v1/testdata/check.yaml
+++ b/backend/api/v1/testdata/check.yaml
@@ -15,6 +15,8 @@
characterset: ""
collation: ""
comment: abcdefg
+ classification: ""
+ usercomment: ""
- name: b
position: 0
default:
@@ -24,6 +26,8 @@
characterset: ""
collation: ""
comment: ""
+ classification: ""
+ usercomment: ""
indexes:
- name: PRIMARY
expressions:
@@ -42,6 +46,8 @@
datafree: 0
createoptions: ""
comment: ""
+ classification: ""
+ usercomment: ""
foreignkeys:
- name: fk1
columns:
@@ -64,6 +70,8 @@
characterset: ""
collation: ""
comment: ""
+ classification: ""
+ usercomment: ""
indexes:
- name: PRIMARY
expressions:
@@ -81,6 +89,8 @@
datafree: 0
createoptions: ""
comment: ""
+ classification: ""
+ usercomment: ""
foreignkeys: []
views: []
functions: []
@@ -107,6 +117,8 @@
characterset: ""
collation: ""
comment: abcdefg
+ classification: ""
+ usercomment: ""
- name: b
position: 0
default:
@@ -116,6 +128,8 @@
characterset: ""
collation: ""
comment: ""
+ classification: ""
+ usercomment: ""
indexes:
- name: PRIMARY
expressions:
@@ -134,6 +148,8 @@
datafree: 0
createoptions: ""
comment: ""
+ classification: ""
+ usercomment: ""
foreignkeys:
- name: fk1
columns:
@@ -156,6 +172,8 @@
characterset: ""
collation: ""
comment: ""
+ classification: ""
+ usercomment: ""
indexes:
- name: PRIMARY
expressions:
@@ -173,6 +191,8 @@
datafree: 0
createoptions: ""
comment: ""
+ classification: ""
+ usercomment: ""
foreignkeys: []
views: []
functions: []
@@ -199,6 +219,8 @@
characterset: ""
collation: ""
comment: abcdefg
+ classification: ""
+ usercomment: ""
- name: b
position: 0
default:
@@ -208,6 +230,8 @@
characterset: ""
collation: ""
comment: ""
+ classification: ""
+ usercomment: ""
indexes:
- name: PRIMARY
expressions:
@@ -226,6 +250,8 @@
datafree: 0
createoptions: ""
comment: ""
+ classification: ""
+ usercomment: ""
foreignkeys:
- name: fk1
columns:
@@ -248,6 +274,8 @@
characterset: ""
collation: ""
comment: ""
+ classification: ""
+ usercomment: ""
indexes:
- name: PRIMARY
expressions:
@@ -265,6 +293,8 @@
datafree: 0
createoptions: ""
comment: ""
+ classification: ""
+ usercomment: ""
foreignkeys: []
views: []
functions: []
@@ -291,6 +321,8 @@
characterset: ""
collation: ""
comment: abcdefg
+ classification: ""
+ usercomment: ""
- name: b
position: 0
default:
@@ -300,6 +332,8 @@
characterset: ""
collation: ""
comment: ""
+ classification: ""
+ usercomment: ""
indexes:
- name: PRIMARY
expressions:
@@ -318,6 +352,8 @@
datafree: 0
createoptions: ""
comment: ""
+ classification: ""
+ usercomment: ""
foreignkeys:
- name: fk1
columns:
@@ -340,6 +376,8 @@
characterset: ""
collation: ""
comment: ""
+ classification: ""
+ usercomment: ""
indexes:
- name: PRIMARY
expressions:
@@ -357,6 +395,8 @@
datafree: 0
createoptions: ""
comment: ""
+ classification: ""
+ usercomment: ""
foreignkeys: []
views: []
functions: []
@@ -383,6 +423,8 @@
characterset: ""
collation: ""
comment: abcdefg
+ classification: ""
+ usercomment: ""
- name: b
position: 0
default:
@@ -392,6 +434,8 @@
characterset: ""
collation: ""
comment: ""
+ classification: ""
+ usercomment: ""
indexes:
- name: PRIMARY
expressions:
@@ -410,6 +454,8 @@
datafree: 0
createoptions: ""
comment: ""
+ classification: ""
+ usercomment: ""
foreignkeys:
- name: fk1
columns:
@@ -432,6 +478,8 @@
characterset: ""
collation: ""
comment: ""
+ classification: ""
+ usercomment: ""
indexes:
- name: PRIMARY
expressions:
@@ -449,6 +497,8 @@
datafree: 0
createoptions: ""
comment: ""
+ classification: ""
+ usercomment: ""
foreignkeys: []
views: []
functions: []
diff --git a/backend/api/v1/testdata/design.yaml b/backend/api/v1/testdata/design.yaml
index 9d9602f3fe..fcdca2bf79 100644
--- a/backend/api/v1/testdata/design.yaml
+++ b/backend/api/v1/testdata/design.yaml
@@ -31,6 +31,8 @@
characterset: ""
collation: ""
comment: ""
+ classification: ""
+ usercomment: ""
indexes: []
engine: ""
collation: ""
@@ -40,6 +42,8 @@
datafree: 0
createoptions: ""
comment: ""
+ classification: ""
+ usercomment: ""
foreignkeys: []
- name: t4
columns:
@@ -52,6 +56,8 @@
characterset: ""
collation: ""
comment: this is comment, aha
+ classification: ""
+ usercomment: ""
- name: b
position: 0
default: null
@@ -60,6 +66,8 @@
characterset: ""
collation: ""
comment: ""
+ classification: ""
+ usercomment: ""
- name: c
position: 0
default: null
@@ -68,6 +76,8 @@
characterset: ""
collation: ""
comment: ""
+ classification: ""
+ usercomment: ""
- name: e
position: 0
default: null
@@ -76,6 +86,8 @@
characterset: ""
collation: ""
comment: ""
+ classification: ""
+ usercomment: ""
indexes:
- name: PRIMARY
expressions:
@@ -95,6 +107,8 @@
datafree: 0
createoptions: ""
comment: ""
+ classification: ""
+ usercomment: ""
foreignkeys:
- name: fk1
columns:
@@ -116,6 +130,8 @@
characterset: ""
collation: ""
comment: ""
+ classification: ""
+ usercomment: ""
indexes:
- name: PRIMARY
expressions:
@@ -133,6 +149,8 @@
datafree: 0
createoptions: ""
comment: ""
+ classification: ""
+ usercomment: ""
foreignkeys:
- name: fk1
columns:
diff --git a/backend/api/v1/testdata/schema.yaml b/backend/api/v1/testdata/schema.yaml
index 98e8ba7fd2..df6e924c65 100644
--- a/backend/api/v1/testdata/schema.yaml
+++ b/backend/api/v1/testdata/schema.yaml
@@ -2,6 +2,7 @@
schema: |
create table t
(
+ c int,
a int default 1 comment 'abcdefg',
b varchar(20) default 'NULL' comment '',
primary key (a, b),
@@ -11,100 +12,110 @@
metadata:
name: ""
schemas:
- - name: ""
- tables:
- - name: t
- columns:
- - name: a
- position: 0
- default:
- value: "1"
- nullable: true
- type: int
- characterset: ""
- collation: ""
- comment: abcdefg
- classification: ""
- usercomment: abcdefg
- - name: b
- position: 0
- default:
- value: "'NULL'"
- nullable: true
- type: varchar(20)
- characterset: ""
- collation: ""
- comment: ""
- classification: ""
- usercomment: ""
- indexes:
- - name: PRIMARY
- expressions:
- - a
- - b
- type: ""
- unique: true
- primary: true
- visible: true
- comment: ""
- engine: ""
- collation: ""
- rowcount: 0
- datasize: 0
- indexsize: 0
- datafree: 0
- createoptions: ""
- comment: ""
- classification: ""
- usercomment: ""
- foreignkeys:
- - name: fk1
- columns:
- - a
- referencedschema: ""
- referencedtable: t2
- referencedcolumns:
- - b
- ondelete: ""
- onupdate: ""
- matchtype: ""
- - name: t2
- columns:
- - name: b
- position: 0
- default:
- value: "NULL"
- nullable: false
- type: int
- characterset: ""
- collation: ""
- comment: ""
- classification: ""
- usercomment: ""
- indexes:
- - name: PRIMARY
- expressions:
- - b
- type: ""
- unique: true
- primary: true
- visible: true
- comment: ""
- engine: ""
- collation: ""
- rowcount: 0
- datasize: 0
- indexsize: 0
- datafree: 0
- createoptions: ""
- comment: ""
- classification: ""
- usercomment: ""
- foreignkeys: []
- views: []
- functions: []
- streams: []
- tasks: []
+ - name: ""
+ tables:
+ - name: t
+ columns:
+ - name: c
+ position: 0
+ default: null
+ nullable: true
+ type: int
+ characterset: ""
+ collation: ""
+ comment: ""
+ classification: ""
+ usercomment: ""
+ - name: a
+ position: 0
+ default:
+ value: "1"
+ nullable: true
+ type: int
+ characterset: ""
+ collation: ""
+ comment: abcdefg
+ classification: ""
+ usercomment: abcdefg
+ - name: b
+ position: 0
+ default:
+ value: '''NULL'''
+ nullable: true
+ type: varchar(20)
+ characterset: ""
+ collation: ""
+ comment: ""
+ classification: ""
+ usercomment: ""
+ indexes:
+ - name: PRIMARY
+ expressions:
+ - a
+ - b
+ type: ""
+ unique: true
+ primary: true
+ visible: true
+ comment: ""
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ classification: ""
+ usercomment: ""
+ foreignkeys:
+ - name: fk1
+ columns:
+ - a
+ referencedschema: ""
+ referencedtable: t2
+ referencedcolumns:
+ - b
+ ondelete: ""
+ onupdate: ""
+ matchtype: ""
+ - name: t2
+ columns:
+ - name: b
+ position: 0
+ default:
+ value: "NULL"
+ nullable: false
+ type: int
+ characterset: ""
+ collation: ""
+ comment: ""
+ classification: ""
+ usercomment: ""
+ indexes:
+ - name: PRIMARY
+ expressions:
+ - b
+ type: ""
+ unique: true
+ primary: true
+ visible: true
+ comment: ""
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ classification: ""
+ usercomment: ""
+ foreignkeys: []
+ views: []
+ functions: []
+ streams: []
+ tasks: []
characterset: ""
collation: ""
extensions: []