Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: initial definition for database schema API (#3751)
Danny Xuda9e2cd67a87
diff --git a/.golangci.yaml b/.golangci.yamlindex 1fb87971e6..9f318f6391 100644--- a/.golangci.yaml+++ b/.golangci.yaml@@ -32,6 +32,8 @@ run:timeout: 5mbuild-tags:- mysql+ skip-dirs:+ - proto/generated-golinters-settings:goimports:diff --git a/api/db_schema.go b/api/db_schema.goindex 0dffbbc75e..f63fad4db6 100644--- a/api/db_schema.go+++ b/api/db_schema.go@@ -19,8 +19,6 @@ type DBSchemaUpsert struct {// Standard fields// Value is assigned from the jwt subject field passed by the client.UpdatorID int- CreatedTs int64- UpdatedTs int64// Related fieldsDatabaseID intdiff --git a/go.mod b/go.modindex 00dfca192d..a347379a57 100644--- a/go.mod+++ b/go.mod@@ -41,6 +41,7 @@ require (golang.org/x/crypto v0.1.0golang.org/x/sys v0.1.0golang.org/x/text v0.4.0+ google.golang.org/protobuf v1.28.1gopkg.in/yaml.v3 v3.0.1)@@ -155,7 +156,6 @@ require (golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirectgoogle.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c // indirectgoogle.golang.org/grpc v1.50.1 // indirect- google.golang.org/protobuf v1.28.1 // indirectgopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect)diff --git a/proto/generate.sh b/proto/generate.shnew file mode 100755index 0000000000..95863c925e--- /dev/null+++ b/proto/generate.sh@@ -0,0 +1,4 @@+# https://developers.google.com/protocol-buffers/docs/gotutorial++# store package belongs to storage related proto's.+protoc --proto_path=store --go_out=. store/database.protodiff --git a/proto/generated-go/store/database.pb.go b/proto/generated-go/store/database.pb.gonew file mode 100644index 0000000000..e2f9c4cabd--- /dev/null+++ b/proto/generated-go/store/database.pb.go@@ -0,0 +1,224 @@+// Code generated by protoc-gen-go. DO NOT EDIT.+// versions:+// protoc-gen-go v1.28.1+// protoc v3.21.9+// source: database.proto++package store++import (+ protoreflect "google.golang.org/protobuf/reflect/protoreflect"+ protoimpl "google.golang.org/protobuf/runtime/protoimpl"+ reflect "reflect"+ sync "sync"+)++const (+ // Verify that this generated code is sufficiently up-to-date.+ _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)+ // Verify that runtime/protoimpl is sufficiently up-to-date.+ _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)+)++// SchemaMetadata defines the schema for database schema metadata.+type SchemaMetadata struct {+ state protoimpl.MessageState+ sizeCache protoimpl.SizeCache+ unknownFields protoimpl.UnknownFields++ // The table_metadata is the metadata for a list of tables in a database.+ TableMetadata []*TableMetadata `protobuf:"bytes,1,rep,name=table_metadata,json=tableMetadata,proto3" json:"table_metadata,omitempty"`+}++func (x *SchemaMetadata) Reset() {+ *x = SchemaMetadata{}+ if protoimpl.UnsafeEnabled {+ mi := &file_database_proto_msgTypes[0]+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ ms.StoreMessageInfo(mi)+ }+}++func (x *SchemaMetadata) String() string {+ return protoimpl.X.MessageStringOf(x)+}++func (*SchemaMetadata) ProtoMessage() {}++func (x *SchemaMetadata) ProtoReflect() protoreflect.Message {+ mi := &file_database_proto_msgTypes[0]+ if protoimpl.UnsafeEnabled && x != nil {+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ if ms.LoadMessageInfo() == nil {+ ms.StoreMessageInfo(mi)+ }+ return ms+ }+ return mi.MessageOf(x)+}++// Deprecated: Use SchemaMetadata.ProtoReflect.Descriptor instead.+func (*SchemaMetadata) Descriptor() ([]byte, []int) {+ return file_database_proto_rawDescGZIP(), []int{0}+}++func (x *SchemaMetadata) GetTableMetadata() []*TableMetadata {+ if x != nil {+ return x.TableMetadata+ }+ return nil+}++// TableMetadata is the metadata for a database table.+type TableMetadata struct {+ state protoimpl.MessageState+ sizeCache protoimpl.SizeCache+ unknownFields protoimpl.UnknownFields++ // The schema is the schema name of a table. It is an empty string for databases without such concept such as MySQL.+ Schema string `protobuf:"bytes,1,opt,name=schema,proto3" json:"schema,omitempty"`+ // The name is the name of a table.+ Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`+}++func (x *TableMetadata) Reset() {+ *x = TableMetadata{}+ if protoimpl.UnsafeEnabled {+ mi := &file_database_proto_msgTypes[1]+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ ms.StoreMessageInfo(mi)+ }+}++func (x *TableMetadata) String() string {+ return protoimpl.X.MessageStringOf(x)+}++func (*TableMetadata) ProtoMessage() {}++func (x *TableMetadata) ProtoReflect() protoreflect.Message {+ mi := &file_database_proto_msgTypes[1]+ if protoimpl.UnsafeEnabled && x != nil {+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ if ms.LoadMessageInfo() == nil {+ ms.StoreMessageInfo(mi)+ }+ return ms+ }+ return mi.MessageOf(x)+}++// Deprecated: Use TableMetadata.ProtoReflect.Descriptor instead.+func (*TableMetadata) Descriptor() ([]byte, []int) {+ return file_database_proto_rawDescGZIP(), []int{1}+}++func (x *TableMetadata) GetSchema() string {+ if x != nil {+ return x.Schema+ }+ return ""+}++func (x *TableMetadata) GetName() string {+ if x != nil {+ return x.Name+ }+ return ""+}++var File_database_proto protoreflect.FileDescriptor++var file_database_proto_rawDesc = []byte{+ 0x0a, 0x0e, 0x64, 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,+ 0x12, 0x0e, 0x62, 0x79, 0x74, 0x65, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x65,+ 0x22, 0x56, 0x0a, 0x0e, 0x53, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61,+ 0x74, 0x61, 0x12, 0x44, 0x0a, 0x0e, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x6d, 0x65, 0x74, 0x61,+ 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1d, 0x2e, 0x62, 0x79, 0x74,+ 0x65, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x2e, 0x54, 0x61, 0x62, 0x6c,+ 0x65, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x52, 0x0d, 0x74, 0x61, 0x62, 0x6c, 0x65,+ 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x22, 0x3b, 0x0a, 0x0d, 0x54, 0x61, 0x62, 0x6c,+ 0x65, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63, 0x68,+ 0x65, 0x6d, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x63, 0x68, 0x65, 0x6d,+ 0x61, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,+ 0x04, 0x6e, 0x61, 0x6d, 0x65, 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 (+ file_database_proto_rawDescOnce sync.Once+ file_database_proto_rawDescData = file_database_proto_rawDesc+)++func file_database_proto_rawDescGZIP() []byte {+ file_database_proto_rawDescOnce.Do(func() {+ file_database_proto_rawDescData = protoimpl.X.CompressGZIP(file_database_proto_rawDescData)+ })+ return file_database_proto_rawDescData+}++var file_database_proto_msgTypes = make([]protoimpl.MessageInfo, 2)+var file_database_proto_goTypes = []interface{}{+ (*SchemaMetadata)(nil), // 0: bytebase.store.SchemaMetadata+ (*TableMetadata)(nil), // 1: bytebase.store.TableMetadata+}+var file_database_proto_depIdxs = []int32{+ 1, // 0: bytebase.store.SchemaMetadata.table_metadata:type_name -> bytebase.store.TableMetadata+ 1, // [1:1] is the sub-list for method output_type+ 1, // [1:1] is the sub-list for method input_type+ 1, // [1:1] is the sub-list for extension type_name+ 1, // [1:1] is the sub-list for extension extendee+ 0, // [0:1] is the sub-list for field type_name+}++func init() { file_database_proto_init() }+func file_database_proto_init() {+ if File_database_proto != nil {+ return+ }+ if !protoimpl.UnsafeEnabled {+ file_database_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {+ switch v := v.(*SchemaMetadata); i {+ case 0:+ return &v.state+ case 1:+ return &v.sizeCache+ case 2:+ return &v.unknownFields+ default:+ return nil+ }+ }+ file_database_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {+ switch v := v.(*TableMetadata); i {+ case 0:+ return &v.state+ case 1:+ return &v.sizeCache+ case 2:+ return &v.unknownFields+ default:+ return nil+ }+ }+ }+ type x struct{}+ out := protoimpl.TypeBuilder{+ File: protoimpl.DescBuilder{+ GoPackagePath: reflect.TypeOf(x{}).PkgPath(),+ RawDescriptor: file_database_proto_rawDesc,+ NumEnums: 0,+ NumMessages: 2,+ NumExtensions: 0,+ NumServices: 0,+ },+ GoTypes: file_database_proto_goTypes,+ DependencyIndexes: file_database_proto_depIdxs,+ MessageInfos: file_database_proto_msgTypes,+ }.Build()+ File_database_proto = out.File+ file_database_proto_rawDesc = nil+ file_database_proto_goTypes = nil+ file_database_proto_depIdxs = nil+}diff --git a/proto/store/database.proto b/proto/store/database.protonew file mode 100644index 0000000000..6a0f5ec264--- /dev/null+++ b/proto/store/database.proto@@ -0,0 +1,18 @@+syntax = "proto3";++package bytebase.store;+option go_package = "generated-go/store";++// SchemaMetadata defines the schema for database schema metadata.+message SchemaMetadata {+ // The table_metadata is the metadata for a list of tables in a database.+ repeated TableMetadata table_metadata = 1;+}++// TableMetadata is the metadata for a database table.+message TableMetadata {+ // The schema is the schema name of a table. It is an empty string for databases without such concept such as MySQL.+ string schema = 1;+ // The name is the name of a table.+ string name = 2;+}\ No newline at end of filediff --git a/server/runner/schemasync/syncer.go b/server/runner/schemasync/syncer.goindex 3667a4a645..48b7b7bf8e 100644--- a/server/runner/schemasync/syncer.go+++ b/server/runner/schemasync/syncer.go@@ -11,11 +11,14 @@ import ("github.com/labstack/echo/v4""github.com/pkg/errors""go.uber.org/zap"+ "google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/api""github.com/bytebase/bytebase/common""github.com/bytebase/bytebase/common/log""github.com/bytebase/bytebase/plugin/db"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"+ "github.com/bytebase/bytebase/server/component/config""github.com/bytebase/bytebase/server/component/dbfactory""github.com/bytebase/bytebase/server/component/state""github.com/bytebase/bytebase/server/utils"@@ -27,11 +30,12 @@ const ()// NewSyncer creates a schema syncer.-func NewSyncer(store *store.Store, dbFactory *dbfactory.DBFactory, stateCfg *state.State) *Syncer {+func NewSyncer(store *store.Store, dbFactory *dbfactory.DBFactory, stateCfg *state.State, profile config.Profile) *Syncer {return &Syncer{store: store,dbFactory: dbFactory,stateCfg: stateCfg,+ profile: profile,}}@@ -40,6 +44,7 @@ type Syncer struct {store *store.StoredbFactory *dbfactory.DBFactorystateCfg *state.State+ profile config.Profile}// Run will run the schema syncer once.@@ -387,6 +392,12 @@ func (s *Syncer) SyncDatabaseSchema(ctx context.Context, instance *api.Instance,}database = createdDatabase}+ // Sync database schema+ if s.profile.Mode == common.ReleaseModeDev {+ if err := syncDBSchema(ctx, s.store, database, schema); err != nil {+ return err+ }+ }if err := syncTableSchema(ctx, s.store, database, schema); err != nil {return err}@@ -407,3 +418,33 @@ func syncViewSchema(ctx context.Context, store *store.Store, database *api.Databfunc syncDBExtensionSchema(ctx context.Context, store *store.Store, database *api.Database, schema *db.Schema) error {return store.SetDBExtensionList(ctx, schema, database.ID)}++func syncDBSchema(ctx context.Context, store *store.Store, database *api.Database, schema *db.Schema) error {+ dbSchema, err := store.GetDBSchema(ctx, database.ID)+ if err != nil {+ return err+ }+ // TODO(d): finish the implementation.+ metadata := &storepb.SchemaMetadata{}+ for _, t := range schema.TableList {+ metadata.TableMetadata = append(metadata.TableMetadata, &storepb.TableMetadata{+ Name: t.Name,+ })+ }+ bytes, err := protojson.Marshal(metadata)+ if err != nil {+ return err+ }+ metadataString := string(bytes)+ if dbSchema == nil || dbSchema.Metadata != metadataString {+ if _, err := store.UpsertDBSchema(ctx, api.DBSchemaUpsert{+ UpdatorID: api.SystemBotID,+ DatabaseID: database.ID,+ Metadata: metadataString,+ RawDump: "",+ }); err != nil {+ return err+ }+ }+ return nil+}diff --git a/server/server.go b/server/server.goindex b8b8272104..3b41a09808 100644--- a/server/server.go+++ b/server/server.go@@ -297,7 +297,7 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {}if !profile.Readonly {- s.SchemaSyncer = schemasync.NewSyncer(storeInstance, s.dbFactory, s.stateCfg)+ s.SchemaSyncer = schemasync.NewSyncer(storeInstance, s.dbFactory, s.stateCfg, profile)// TODO(p0ny): enable Feishu provider only when it is needed.s.feishuProvider = feishu.NewProvider(profile.FeishuAPIURL)s.ApplicationRunner = apprun.NewRunner(storeInstance, s.ActivityManager, s.feishuProvider, profile)