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: not fail sync datashare database schema (#6748)
Danny Xu54d9f1d82242
diff --git a/backend/api/v1/instance_service.go b/backend/api/v1/instance_service.goindex cf98b6f31d..ba8d290fc3 100644--- a/backend/api/v1/instance_service.go+++ b/backend/api/v1/instance_service.go@@ -101,7 +101,7 @@ func (s *InstanceService) CreateInstance(ctx context.Context, request *v1pb.Creaif request.ValidateOnly {for _, ds := range instanceMessage.DataSources {err := func() error {- driver, err := s.dbFactory.GetDataSourceDriver(ctx, instanceMessage.Engine, ds, "", "", 0, ds.Type == api.RO)+ driver, err := s.dbFactory.GetDataSourceDriver(ctx, instanceMessage.Engine, ds, "", "", 0, false /* datashare */, ds.Type == api.RO)if err != nil {return err}@@ -416,7 +416,7 @@ func (s *InstanceService) AddDataSource(ctx context.Context, request *v1pb.AddDa// Test connection.if request.ValidateOnly {err := func() error {- driver, err := s.dbFactory.GetDataSourceDriver(ctx, instance.Engine, dataSource, "", "", 0, dataSource.Type == api.RO)+ driver, err := s.dbFactory.GetDataSourceDriver(ctx, instance.Engine, dataSource, "", "", 0, false /* datashare */, dataSource.Type == api.RO)if err != nil {return err}@@ -555,7 +555,7 @@ func (s *InstanceService) UpdateDataSource(ctx context.Context, request *v1pb.Up// Test connection.if request.ValidateOnly {err := func() error {- driver, err := s.dbFactory.GetDataSourceDriver(ctx, instance.Engine, &dataSource, "", "", 0, dataSource.Type == api.RO)+ driver, err := s.dbFactory.GetDataSourceDriver(ctx, instance.Engine, &dataSource, "", "", 0, false /* datashare */, dataSource.Type == api.RO)if err != nil {return err}diff --git a/backend/component/dbfactory/dbfactory.go b/backend/component/dbfactory/dbfactory.goindex 7e3a53defa..6fdb20f7a0 100644--- a/backend/component/dbfactory/dbfactory.go+++ b/backend/component/dbfactory/dbfactory.go@@ -42,7 +42,11 @@ func (d *DBFactory) GetAdminDatabaseDriver(ctx context.Context, instance *store.if database != nil {databaseName = database.DatabaseName}- return d.GetDataSourceDriver(ctx, instance.Engine, dataSource, databaseName, instance.ResourceID, instance.UID, false /* readOnly */)+ datashare := false+ if database != nil && database.DataShare {+ datashare = true+ }+ return d.GetDataSourceDriver(ctx, instance.Engine, dataSource, databaseName, instance.ResourceID, instance.UID, datashare, false /* readOnly */)}// GetReadOnlyDatabaseDriver gets the read-only database driver using the instance's read-only data source.@@ -63,11 +67,11 @@ func (d *DBFactory) GetReadOnlyDatabaseDriver(ctx context.Context, instance *stoif database != nil {databaseName = database.DatabaseName}- return d.GetDataSourceDriver(ctx, instance.Engine, dataSource, databaseName, instance.ResourceID, instance.UID, true /* readOnly */)+ return d.GetDataSourceDriver(ctx, instance.Engine, dataSource, databaseName, instance.ResourceID, instance.UID, database.DataShare, true /* readOnly */)}// GetDataSourceDriver returns the database driver for a data source.-func (d *DBFactory) GetDataSourceDriver(ctx context.Context, engine db.Type, dataSource *store.DataSourceMessage, databaseName, instanceID string, instanceUID int, readOnly bool) (db.Driver, error) {+func (d *DBFactory) GetDataSourceDriver(ctx context.Context, engine db.Type, dataSource *store.DataSourceMessage, databaseName, instanceID string, instanceUID int, datashare, readOnly bool) (db.Driver, error) {dbBinDir := ""switch engine {case db.MySQL, db.TiDB, db.MariaDB, db.OceanBase:@@ -82,6 +86,10 @@ func (d *DBFactory) GetDataSourceDriver(ctx context.Context, engine db.Type, datif databaseName == "" {databaseName = dataSource.Database}+ connectionDatabase := ""+ if datashare {+ connectionDatabase = dataSource.Database+ }password, err := common.Unobfuscate(dataSource.ObfuscatedPassword, d.secret)if err != nil {return nil, err@@ -131,6 +139,7 @@ func (d *DBFactory) GetDataSourceDriver(ctx context.Context, engine db.Type, datHost: dataSource.Host,Port: dataSource.Port,Database: databaseName,+ ConnectionDatabase: connectionDatabase,SRV: dataSource.SRV,AuthenticationDatabase: dataSource.AuthenticationDatabase,SID: dataSource.SID,diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.goindex 0844428850..ca97da2128 100644--- a/backend/plugin/db/driver.go+++ b/backend/plugin/db/driver.go@@ -407,12 +407,15 @@ type MigrationHistoryFind struct {// ConnectionConfig is the configuration for connections.type ConnectionConfig struct {- Host string- Port string- Username string- Password string- Database string- TLSConfig TLSConfig+ Host string+ Port string+ Username string+ Password string+ Database string+ // The database used to connect.+ // It's only set for Redshift datashare database.+ ConnectionDatabase string+ TLSConfig TLSConfig// ReadOnly is only supported for Postgres at the moment.ReadOnly bool// StrictUseDb will only set as true if the user gives only a database instead of a whole instance to access.diff --git a/backend/plugin/db/redshift/redshift.go b/backend/plugin/db/redshift/redshift.goindex 543bf0a1b9..78581544e8 100644--- a/backend/plugin/db/redshift/redshift.go+++ b/backend/plugin/db/redshift/redshift.go@@ -54,6 +54,7 @@ type Driver struct {// Unregister connectionString if we don't need it.connectionString stringdatabaseName string+ datashare bool}func newDriver(db.DriverConfig) db.Driver {@@ -80,6 +81,9 @@ func (driver *Driver) Open(_ context.Context, _ db.Type, config db.ConnectionConconnConfig.Config.User = config.UsernameconnConfig.Config.Password = config.PasswordconnConfig.Config.Database = config.Database+ if config.ConnectionDatabase != "" {+ connConfig.Config.Database = config.ConnectionDatabase+ }if config.TLSConfig.SslCert != "" {cfg, err := config.TLSConfig.GetSslConfig()if err != nil {@@ -107,14 +111,7 @@ func (driver *Driver) Open(_ context.Context, _ db.Type, config db.ConnectionCon}driver.databaseName = config.Database- if config.Database == "" {- databaseName, cfg, err := guessDSN(connConfig, config.Username)- if err != nil {- return nil, err- }- connConfig = cfg- driver.databaseName = databaseName- }+ driver.datashare = config.ConnectionDatabase != ""driver.config = configdriver.connectionString = stdlib.RegisterConnConfig(connConfig)@@ -132,33 +129,6 @@ func (*noDeadlineConn) SetDeadline(time.Time) error { return nil }func (*noDeadlineConn) SetReadDeadline(time.Time) error { return nil }func (*noDeadlineConn) SetWriteDeadline(time.Time) error { return nil }-// guessDSN will guess a valid DB connection and its database name.-func guessDSN(baseConnConfig *pgx.ConnConfig, username string) (string, *pgx.ConnConfig, error) {- // Some postgres server default behavior is to use username as the database name if not specified,- // while some postgres server explicitly requires the database name to be present (e.g. render.com).- guesses := []string{"postgres", username, "template1"}- // dsn+" dbname=bytebase"- for _, guessDatabase := range guesses {- connConfig := *baseConnConfig- connConfig.Database = guessDatabase- if err := func() error {- connectionString := stdlib.RegisterConnConfig(&connConfig)- defer stdlib.UnregisterConnConfig(connectionString)- db, err := sql.Open(driverName, connectionString)- if err != nil {- return err- }- defer db.Close()- return db.Ping()- }(); err != nil {- log.Debug("guessDSN attempt failed", zap.Error(err))- continue- }- return guessDatabase, &connConfig, nil- }- return "", nil, errors.Errorf("cannot connect to the instance, make sure the connection info is correct")-}-// Close closes the database and prevents new queries from starting.// Close then waits for all queries that have started processing on the server to finish.func (driver *Driver) Close(context.Context) error {@@ -189,6 +159,9 @@ func (driver *Driver) GetDB() *sql.DB {// Execute will execute the statement. For CREATE DATABASE statement, some types of databases such as Postgres// will not use transactions to execute the statement but will still use transactions to execute the rest of statements.func (driver *Driver) Execute(ctx context.Context, statement string, createDatabase bool, _ db.ExecuteOptions) (int64, error) {+ if driver.datashare {+ return 0, errors.Errorf("datashare database cannot be updated")+ }if createDatabase {databases, err := driver.getDatabases(ctx)if err != nil {diff --git a/backend/plugin/db/redshift/sync.go b/backend/plugin/db/redshift/sync.goindex ae2164800c..cb7f1db170 100644--- a/backend/plugin/db/redshift/sync.go+++ b/backend/plugin/db/redshift/sync.go@@ -135,6 +135,9 @@ func (driver *Driver) getInstanceRoles(ctx context.Context) ([]*storepb.Instance// SyncDBSchema syncs a single database schema.func (driver *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseMetadata, error) {+ if driver.datashare {+ return &storepb.DatabaseMetadata{}, nil+ }// Query db infodatabases, err := driver.getDatabases(ctx)if err != nil {