Skip to content

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

chore: add convert schema unit test (#3859)

Danny Xu999c4289744d
diff --git a/proto/generated-go/store/database.pb.go b/proto/generated-go/store/database.pb.go
index 9f1c6e4893..74e9c22c4e 100644
--- a/proto/generated-go/store/database.pb.go
+++ b/proto/generated-go/store/database.pb.go
@@ -1,7 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.1
-// protoc v3.21.9
+// protoc v3.21.11
// source: database.proto
package store
@@ -614,10 +614,12 @@ type ExtensionMetadata struct {
// The name is the name of an extension.
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
+ // The schema is the extension that is installed to. But the extension usage is not limited to the schema.
+ Schema string `protobuf:"bytes,2,opt,name=schema,proto3" json:"schema,omitempty"`
// The version is the version of an extension.
- Version string `protobuf:"bytes,2,opt,name=version,proto3" json:"version,omitempty"`
+ Version string `protobuf:"bytes,3,opt,name=version,proto3" json:"version,omitempty"`
// The description is the description of an extension.
- Description string `protobuf:"bytes,3,opt,name=description,proto3" json:"description,omitempty"`
+ Description string `protobuf:"bytes,4,opt,name=description,proto3" json:"description,omitempty"`
}
func (x *ExtensionMetadata) Reset() {
@@ -659,6 +661,13 @@ func (x *ExtensionMetadata) GetName() string {
return ""
}
+func (x *ExtensionMetadata) GetSchema() string {
+ if x != nil {
+ return x.Schema
+ }
+ return ""
+}
+
func (x *ExtensionMetadata) GetVersion() string {
if x != nil {
return x.Version
@@ -760,15 +769,17 @@ var file_database_proto_rawDesc = []byte{
0x07, 0x70, 0x72, 0x69, 0x6d, 0x61, 0x72, 0x79, 0x12, 0x18, 0x0a, 0x07, 0x76, 0x69, 0x73, 0x69,
0x62, 0x6c, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x76, 0x69, 0x73, 0x69, 0x62,
0x6c, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x07, 0x20,
- 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x22, 0x63, 0x0a, 0x11,
+ 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x22, 0x7b, 0x0a, 0x11,
0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74,
0x61, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
- 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
- 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12,
- 0x20, 0x0a, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x03,
- 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f,
- 0x6e, 0x42, 0x14, 0x5a, 0x12, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x64, 0x2d, 0x67,
- 0x6f, 0x2f, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
+ 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x18,
+ 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x12, 0x18, 0x0a,
+ 0x07, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07,
+ 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x20, 0x0a, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72,
+ 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x64, 0x65,
+ 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x42, 0x14, 0x5a, 0x12, 0x67, 0x65, 0x6e,
+ 0x65, 0x72, 0x61, 0x74, 0x65, 0x64, 0x2d, 0x67, 0x6f, 0x2f, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x62,
+ 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
diff --git a/proto/store/database.proto b/proto/store/database.proto
index 83362d4311..d049fc4f9a 100644
--- a/proto/store/database.proto
+++ b/proto/store/database.proto
@@ -147,9 +147,12 @@ message ExtensionMetadata {
// The name is the name of an extension.
string name = 1;
+ // The schema is the extension that is installed to. But the extension usage is not limited to the schema.
+ string schema = 2;
+
// The version is the version of an extension.
- string version = 2;
+ string version = 3;
// The description is the description of an extension.
- string description = 3;
+ string description = 4;
}
diff --git a/server/runner/schemasync/syncer.go b/server/runner/schemasync/syncer.go
index 95714322bc..c6b99254b9 100644
--- a/server/runner/schemasync/syncer.go
+++ b/server/runner/schemasync/syncer.go
@@ -489,6 +489,7 @@ func convertDBSchema(schema *db.Schema) *storepb.DatabaseMetadata {
RowCount: table.RowCount,
DataSize: table.DataSize,
DataFree: table.DataFree,
+ IndexSize: table.IndexSize,
CreateOptions: table.CreateOptions,
Comment: table.Comment,
}
@@ -563,6 +564,7 @@ func convertDBSchema(schema *db.Schema) *storepb.DatabaseMetadata {
for _, extension := range schema.ExtensionList {
databaseMetadata.Extensions = append(databaseMetadata.Extensions, &storepb.ExtensionMetadata{
Name: extension.Name,
+ Schema: extension.Schema,
Version: extension.Version,
Description: extension.Description,
})
diff --git a/server/runner/schemasync/syncer_test.go b/server/runner/schemasync/syncer_test.go
new file mode 100644
index 0000000000..caf28b6558
--- /dev/null
+++ b/server/runner/schemasync/syncer_test.go
@@ -0,0 +1,296 @@
+package schemasync
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+
+ "github.com/bytebase/bytebase/plugin/db"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+func TestConvertDBSchema(t *testing.T) {
+ defaultString := "Jay"
+
+ dbSchema := &db.Schema{
+ Name: "db1",
+ CharacterSet: "charset1",
+ Collation: "collation1",
+ TableList: []db.Table{
+ {
+ Name: "public.hello",
+ ShortName: "hello",
+ Schema: "public",
+ CreatedTs: 20210113,
+ UpdatedTs: 20221214,
+ Type: "BASE TABLE",
+ Engine: "btree",
+ Collation: "collation-hello",
+ RowCount: 123,
+ DataSize: 4542,
+ IndexSize: 2113,
+ DataFree: 123,
+ CreateOptions: "create option hello",
+ Comment: "comment hello",
+ ColumnList: []db.Column{
+ {
+ Name: "name",
+ Position: 1,
+ Default: &defaultString,
+ Nullable: false,
+ Type: "varchar(10)",
+ CharacterSet: "charset1",
+ Collation: "collation1",
+ Comment: "col1",
+ },
+ {
+ Name: "id",
+ Position: 0,
+ Default: nil,
+ Nullable: false,
+ Type: "int",
+ CharacterSet: "charset1",
+ Collation: "collation1",
+ Comment: "col1",
+ },
+ {
+ Name: "age",
+ Position: 2,
+ Default: nil,
+ Nullable: false,
+ Type: "int",
+ CharacterSet: "charset1",
+ Collation: "collation1",
+ Comment: "col1",
+ },
+ },
+ IndexList: []db.Index{
+ {
+ Name: "id-primary",
+ // This could refer to a column or an expression.
+ Expression: "id",
+ Position: 0,
+ Type: "index type",
+ Unique: true,
+ Primary: true,
+ Visible: true,
+ Comment: "comment index",
+ },
+ {
+ Name: "name-age-unique",
+ // This could refer to a column or an expression.
+ Expression: "age",
+ Position: 1,
+ Type: "index type2",
+ Unique: true,
+ Primary: false,
+ Visible: true,
+ Comment: "comment index2",
+ },
+ {
+ Name: "name-age-unique",
+ // This could refer to a column or an expression.
+ Expression: "name",
+ Position: 0,
+ Type: "index type2",
+ Unique: true,
+ Primary: false,
+ Visible: true,
+ Comment: "comment index2",
+ },
+ },
+ },
+ {
+ Name: "hello",
+ ShortName: "hello",
+ Schema: "",
+ CreatedTs: 20210113,
+ UpdatedTs: 20221214,
+ Type: "BASE TABLE",
+ Engine: "btree",
+ Collation: "collation-hello",
+ RowCount: 123,
+ DataSize: 4542,
+ IndexSize: 2113,
+ DataFree: 123,
+ CreateOptions: "create option hello",
+ Comment: "comment hello",
+ ColumnList: nil,
+ IndexList: nil,
+ },
+ {
+ Name: "public.world",
+ ShortName: "world",
+ Schema: "public",
+ CreatedTs: 20210114,
+ UpdatedTs: 20221215,
+ Type: "BASE TABLE",
+ Engine: "btree",
+ Collation: "collation-world",
+ RowCount: 123,
+ DataSize: 4542,
+ IndexSize: 2113,
+ DataFree: 123,
+ CreateOptions: "create option world",
+ Comment: "comment world",
+ ColumnList: nil,
+ IndexList: nil,
+ },
+ },
+ ViewList: []db.View{
+ {
+ Name: "public.nice_view",
+ ShortName: "nice_view",
+ Schema: "public",
+ CreatedTs: 2313,
+ UpdatedTs: 21124,
+ Definition: "select 1",
+ Comment: "comment view",
+ },
+ },
+ ExtensionList: []db.Extension{
+ {
+ Name: "hstore",
+ Version: "v2021",
+ Schema: "public",
+ Description: "awesome hstore",
+ },
+ {
+ Name: "atom",
+ Version: "v2022",
+ Schema: "public",
+ Description: "awesome sphere",
+ },
+ },
+ }
+ want := &storepb.DatabaseMetadata{
+ Name: "db1",
+ CharacterSet: "charset1",
+ Collation: "collation1",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "hello",
+ Engine: "btree",
+ Collation: "collation-hello",
+ RowCount: 123,
+ DataSize: 4542,
+ IndexSize: 2113,
+ DataFree: 123,
+ CreateOptions: "create option hello",
+ Comment: "comment hello",
+ },
+ },
+ },
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "hello",
+ Engine: "btree",
+ Collation: "collation-hello",
+ RowCount: 123,
+ DataSize: 4542,
+ IndexSize: 2113,
+ DataFree: 123,
+ CreateOptions: "create option hello",
+ Comment: "comment hello",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Position: 0,
+ HasDefault: false,
+ Default: "",
+ Nullable: false,
+ Type: "int",
+ CharacterSet: "charset1",
+ Collation: "collation1",
+ Comment: "col1",
+ },
+ {
+ Name: "name",
+ Position: 1,
+ HasDefault: true,
+ Default: "Jay",
+ Nullable: false,
+ Type: "varchar(10)",
+ CharacterSet: "charset1",
+ Collation: "collation1",
+ Comment: "col1",
+ },
+ {
+ Name: "age",
+ Position: 2,
+ HasDefault: false,
+ Default: "",
+ Nullable: false,
+ Type: "int",
+ CharacterSet: "charset1",
+ Collation: "collation1",
+ Comment: "col1",
+ },
+ },
+ Indexes: []*storepb.IndexMetadata{
+ {
+ Name: "id-primary",
+ // This could refer to a column or an expression.
+ Expressions: []string{"id"},
+ Type: "index type",
+ Unique: true,
+ Primary: true,
+ Visible: true,
+ Comment: "comment index",
+ },
+ {
+ Name: "name-age-unique",
+ // This could refer to a column or an expression.
+ Expressions: []string{"name", "age"},
+ Type: "index type2",
+ Unique: true,
+ Primary: false,
+ Visible: true,
+ Comment: "comment index2",
+ },
+ },
+ },
+ {
+ Name: "world",
+ Engine: "btree",
+ Collation: "collation-world",
+ RowCount: 123,
+ DataSize: 4542,
+ IndexSize: 2113,
+ DataFree: 123,
+ CreateOptions: "create option world",
+ Comment: "comment world",
+ },
+ },
+ Views: []*storepb.ViewMetadata{
+ {
+ Name: "nice_view",
+ Definition: "select 1",
+ Comment: "comment view",
+ },
+ },
+ },
+ },
+ Extensions: []*storepb.ExtensionMetadata{
+ {
+ Name: "atom",
+ Version: "v2022",
+ Schema: "public",
+ Description: "awesome sphere",
+ },
+ {
+ Name: "hstore",
+ Version: "v2021",
+ Schema: "public",
+ Description: "awesome hstore",
+ },
+ },
+ }
+ got := convertDBSchema(dbSchema)
+ assert.Equal(t, want, got)
+}