Skip to content

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

feat: flatten the parition tables in build table metadata (#9583)

h3n4lace2e4251089
diff --git a/backend/store/model/database.go b/backend/store/model/database.go
index 90caaccfce..c8f62034bd 100644
--- a/backend/store/model/database.go
+++ b/backend/store/model/database.go
@@ -242,14 +242,10 @@ func NewDatabaseMetadata(metadata *storepb.DatabaseSchemaMetadata) *DatabaseMeta
internalViews: make(map[string]*ViewMetadata),
}
for _, table := range schema.Tables {
- tableMetadata := &TableMetadata{
- internal: make(map[string]*storepb.ColumnMetadata),
+ tables, names := buildTablesMetadata(table)
+ for i, table := range tables {
+ schemaMetadata.internalTables[names[i]] = table
}
- for _, column := range table.Columns {
- tableMetadata.internal[column.Name] = column
- tableMetadata.columns = append(tableMetadata.columns, column)
- }
- schemaMetadata.internalTables[table.Name] = tableMetadata
}
for _, view := range schema.Views {
schemaMetadata.internalViews[view.Name] = &ViewMetadata{}
@@ -297,8 +293,65 @@ func (s *SchemaMetadata) ListViewNames() []string {
return result
}
+func buildTablesMetadata(table *storepb.TableMetadata) ([]*TableMetadata, []string) {
+ if table == nil {
+ return nil, nil
+ }
+ var result []*TableMetadata
+ var name []string
+ tableMetadata := &TableMetadata{
+ internal: make(map[string]*storepb.ColumnMetadata),
+ }
+ for _, column := range table.Columns {
+ tableMetadata.internal[column.Name] = column
+ tableMetadata.columns = append(tableMetadata.columns, column)
+ }
+ result = append(result, tableMetadata)
+ name = append(name, table.Name)
+
+ if table.Partitions != nil {
+ partitionTables, partitionNames := buildTablesMetadataRecursive(table.Columns, table.Partitions, tableMetadata)
+ result = append(result, partitionTables...)
+ name = append(name, partitionNames...)
+ }
+ return result, name
+}
+
+// buildTablesMetadataRecursive builds the partition tables recursively,
+// returns the table metadata and the partition names, the length of them must be the same.
+func buildTablesMetadataRecursive(originalColumn []*storepb.ColumnMetadata, partitions []*storepb.TablePartitionMetadata, root *TableMetadata) ([]*TableMetadata, []string) {
+ if partitions == nil {
+ return nil, nil
+ }
+
+ var tables []*TableMetadata
+ var names []string
+
+ for _, partition := range partitions {
+ partitionMetadata := &TableMetadata{
+ partitionOf: root,
+ internal: make(map[string]*storepb.ColumnMetadata),
+ }
+ for _, column := range originalColumn {
+ partitionMetadata.internal[column.Name] = column
+ partitionMetadata.columns = append(partitionMetadata.columns, column)
+ }
+ tables = append(tables, partitionMetadata)
+ names = append(names, partition.Name)
+ if partition.Subpartitions != nil {
+ subTables, subNames := buildTablesMetadataRecursive(originalColumn, partition.Subpartitions, partitionMetadata)
+ tables = append(tables, subTables...)
+ names = append(names, subNames...)
+ }
+ }
+ return tables, names
+}
+
// TableMetadata is the metadata for a table.
type TableMetadata struct {
+ // If partitionOf is not nil, it means this table is a partition table.
+ partitionOf *TableMetadata
+
internal map[string]*storepb.ColumnMetadata
columns []*storepb.ColumnMetadata
}
diff --git a/backend/store/model/database_test.go b/backend/store/model/database_test.go
new file mode 100644
index 0000000000..a18b1d6927
--- /dev/null
+++ b/backend/store/model/database_test.go
@@ -0,0 +1,96 @@
+package model
+
+import (
+ "sort"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+func TestBuildTablesMetadata(t *testing.T) {
+ testCases := []struct {
+ input *storepb.TableMetadata
+ wantNames []string
+ wantColumns []*storepb.ColumnMetadata
+ }{
+ // No partitions.
+ {
+ input: &storepb.TableMetadata{
+ Name: "orders",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ },
+ },
+ },
+ wantNames: []string{"orders"},
+ wantColumns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ },
+ },
+ },
+ // Nested partitions.
+ {
+ input: &storepb.TableMetadata{
+ Name: "orders",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ },
+ },
+ Partitions: []*storepb.TablePartitionMetadata{
+ {
+ Name: "orders_0_100",
+ Subpartitions: []*storepb.TablePartitionMetadata{
+ {
+ Name: "orders_0_50",
+ },
+ {
+ Name: "orders_50_100",
+ },
+ },
+ },
+ {
+ Name: "orders_100_200",
+ Subpartitions: []*storepb.TablePartitionMetadata{
+ {
+ Name: "orders_100_150",
+ },
+ {
+ Name: "orders_150_200",
+ },
+ },
+ },
+ },
+ },
+ wantNames: []string{"orders", "orders_0_100", "orders_0_50", "orders_50_100", "orders_100_200", "orders_100_150", "orders_150_200"},
+ wantColumns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ },
+ },
+ },
+ }
+
+ a := require.New(t)
+ for _, tc := range testCases {
+ tables, names := buildTablesMetadata(tc.input)
+
+ // The length of the tables should be the same as the length of the names.
+ a.Equal(len(tables), len(names))
+
+ // The names should be the same as the expected names.
+ a.Equal(sort.StringSlice(names), sort.StringSlice(tc.wantNames))
+
+ // Each table should have the same columns as the input.
+ for _, table := range tables {
+ a.Equal(len(table.GetColumns()), len(tc.wantColumns))
+ for _, column := range tc.wantColumns {
+ a.NotNil(table.GetColumn(column.Name))
+ }
+ }
+ }
+}