Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(databricks): fix sync (#12370)
Azusainc31f169c5087
diff --git a/backend/plugin/db/databricks/databricks.go b/backend/plugin/db/databricks/databricks.goindex e8a2d984b3..ac63daa4f9 100644--- a/backend/plugin/db/databricks/databricks.go+++ b/backend/plugin/db/databricks/databricks.go@@ -8,6 +8,9 @@ import ("github.com/databricks/databricks-sdk-go/service/workspace""github.com/pkg/errors"+ // Databricks SQL.+ dbsql "github.com/databricks/databricks-sdk-go/service/sql"+"github.com/bytebase/bytebase/backend/plugin/db"storepb "github.com/bytebase/bytebase/proto/generated-go/store"@@ -21,8 +24,9 @@ func init() {var _ db.Driver = (*Driver)(nil)type Driver struct {- curCatalog string- client *databricks.WorkspaceClient+ curCatalog string+ WarehouseID string+ Client *databricks.WorkspaceClient}func NewDatabricksDriver(db.DriverConfig) db.Driver {@@ -49,7 +53,9 @@ func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionCif err != nil {return nil, err}- d.client = client++ d.Client = client+ d.WarehouseID = config.WarehouseIDd.curCatalog = config.Databasereturn d, nil}@@ -59,7 +65,9 @@ func (*Driver) Close(_ context.Context) error {}func (d *Driver) Ping(ctx context.Context) error {- _, err := d.client.Workspace.ListAll(ctx, workspace.ListWorkspaceRequest{})+ _, err := d.Client.Workspace.ListAll(ctx, workspace.ListWorkspaceRequest{+ Path: "/",+ })if err != nil {return errors.Wrapf(err, "failed to ping instance")}@@ -89,3 +97,18 @@ func (*Driver) Execute(_ context.Context, _ string, _ db.ExecuteOptions) (int64,func (*Driver) CheckSlowQueryLogEnabled(_ context.Context) error {return nil}++// Execute SQL statement synchronously and return row data or error.+func (d *Driver) execSQLSync(ctx context.Context, statement string) ([][]string, error) {+ resp, err := d.Client.StatementExecution.ExecuteAndWait(ctx, dbsql.ExecuteStatementRequest{+ Statement: statement,+ WarehouseId: d.WarehouseID,+ })+ if err != nil {+ return nil, err+ }+ if resp.Result == nil {+ return nil, errors.New("no response")+ }+ return resp.Result.DataArray, nil+}diff --git a/backend/plugin/db/databricks/role.go b/backend/plugin/db/databricks/role.goindex 94e59a9c42..ce04425f7d 100644--- a/backend/plugin/db/databricks/role.go+++ b/backend/plugin/db/databricks/role.go@@ -7,21 +7,22 @@ import ()func (*Driver) CreateRole(_ context.Context, _ *db.DatabaseRoleUpsertMessage) (*db.DatabaseRoleMessage, error) {- panic("unimplemented")+ return nil, nil}func (*Driver) DeleteRole(_ context.Context, _ string) error {- panic("unimplemented")+ return nil}func (*Driver) FindRole(_ context.Context, _ string) (*db.DatabaseRoleMessage, error) {- panic("unimplemented")+ return nil, nil}func (*Driver) UpdateRole(_ context.Context, _ string, _ *db.DatabaseRoleUpsertMessage) (*db.DatabaseRoleMessage, error) {- panic("unimplemented")+ return nil, nil}+// TODO(tommy): maybe implement this when Permissions API is implemented.func (*Driver) ListRole(_ context.Context) ([]*db.DatabaseRoleMessage, error) {- panic("unimplemented")+ return nil, nil}diff --git a/backend/plugin/db/databricks/sync.go b/backend/plugin/db/databricks/sync.goindex 19bc5a405a..11cb2773bb 100644--- a/backend/plugin/db/databricks/sync.go+++ b/backend/plugin/db/databricks/sync.go@@ -2,6 +2,7 @@ package databricksimport ("context"+ "strings""time""github.com/databricks/databricks-sdk-go/service/catalog"@@ -30,7 +31,7 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetad}// fetch table data from databricks.- catalogMap, err := d.listTables(ctx)+ catalogMap, err := d.listAllTables(ctx)if err != nil {return nil, err}@@ -51,8 +52,22 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetadfunc (d *Driver) SyncInstance(ctx context.Context) (*db.InstanceMetadata, error) {instanceMetadata := &db.InstanceMetadata{}+ // fetch version.+ versionData, err := d.execSQLSync(ctx, "SELECT VERSION()")+ if err != nil {+ return nil, err+ }+ if len(versionData) != 1 || len(versionData[0]) != 1 {+ return nil, errors.New("invalid version format")+ }+ splitVersion := strings.Split(versionData[0][0], " ")+ if len(splitVersion) != 2 {+ return nil, errors.New("invalid version format")+ }+ instanceMetadata.Version = splitVersion[0]+// fetch table data from databricks.- catalogMap, err := d.listTables(ctx)+ catalogMap, err := d.listAllTables(ctx)if err != nil {return nil, err}@@ -65,6 +80,9 @@ func (d *Driver) SyncInstance(ctx context.Context) (*db.InstanceMetadata, error)instanceMetadata.Databases = append(instanceMetadata.Databases, &dbSchemaMeta)}+ // fetch workspace users.+ // TODO(tommy): complete this part when Permissions API for Golang is implemented.+return instanceMetadata, nil}@@ -72,14 +90,38 @@ func (*Driver) SyncSlowQuery(_ context.Context, _ time.Time) (map[string]*storepreturn nil, nil}-func (d *Driver) listTables(ctx context.Context) (databricksCatalogMap, error) {- tablesInfo, err := d.client.Tables.ListAll(ctx, catalog.ListTablesRequest{})+func (d *Driver) listAllTables(ctx context.Context) (databricksCatalogMap, error) {+ catalogMap := make(databricksCatalogMap)++ catalogsInfo, err := d.Client.Catalogs.ListAll(ctx)if err != nil {return nil, err}- catalogMap := make(databricksCatalogMap)+ for _, catalogInfo := range catalogsInfo {+ schemasInfo, err := d.Client.Schemas.ListAll(ctx, catalog.ListSchemasRequest{+ CatalogName: catalogInfo.Name,+ })+ if err != nil {+ return nil, err+ }+ for _, schemaInfo := range schemasInfo {+ tablesInfo, err := d.Client.Tables.ListAll(ctx, catalog.ListTablesRequest{+ CatalogName: catalogInfo.Name,+ SchemaName: schemaInfo.Name,+ })+ if err != nil {+ return nil, err+ }+ appendSchemaTables(catalogMap, tablesInfo)+ }+ }++ return catalogMap, nil+}+// extract tables' metadata from 'tablesInfo' and store it in 'catalogMap'.+func appendSchemaTables(catalogMap databricksCatalogMap, tablesInfo []catalog.TableInfo) {for _, tableInfo := range tablesInfo {table := tableUnion{typeName: tableInfo.TableType,@@ -103,13 +145,15 @@ func (d *Driver) listTables(ctx context.Context) (databricksCatalogMap, error) {table.externalTable = &storepb.ExternalTableMetadata{Name: tableInfo.Name,}- // TODO(tommy): find the responding string for the normal table type.- default:+ case catalog.TableTypeManaged:table.table = &storepb.TableMetadata{Name: tableInfo.Name,Columns: convertToColumnMetadata(tableInfo.Columns),Comment: tableInfo.Comment,}+ default:+ // we do not sync streaming table.+ continue}if schemaMap, ok := catalogMap[tableInfo.CatalogName]; !ok {@@ -125,8 +169,6 @@ func (d *Driver) listTables(ctx context.Context) (databricksCatalogMap, error) {}}}-- return catalogMap, nil}func convertToColumnMetadata(columnInfo []catalog.ColumnInfo) []*storepb.ColumnMetadata {@@ -170,9 +212,10 @@ func convertToStorepbSchemas(schemaMap databricksSchemaMap) []*storepb.SchemaMetschemaMetadata.MaterializedViews = append(schemaMetadata.MaterializedViews, table.materialView)case catalog.TableTypeExternal:schemaMetadata.ExternalTables = append(schemaMetadata.ExternalTables, table.externalTable)- default:- // TODO(tommy): find the responding string for the normal table type.+ case catalog.TableTypeManaged:schemaMetadata.Tables = append(schemaMetadata.Tables, table.table)+ default:+ // we do not sync streaming table.}}schemas = append(schemas, schemaMetadata)diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.goindex 16b7dd2a29..4d5304043d 100644--- a/backend/plugin/db/driver.go+++ b/backend/plugin/db/driver.go@@ -206,6 +206,9 @@ type ConnectionConfig struct {// AccountID is used by Databricks.AccountID string++ // WarehouseID is used by Databricks.+ WarehouseID string}// SSHConfig is the configuration for connection over SSH.