Skip to content

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

chore: support query with given data source ID (#9339)

Danny Xuf8501462fa5c
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index e6881c8953..82be067f17 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -351,7 +351,7 @@ func (s *SQLService) doExport(ctx context.Context, request *v1pb.ExportRequest,
sensitiveSchemaInfo = nil
}
- driver, err := s.dbFactory.GetReadOnlyDatabaseDriver(ctx, instance, database)
+ driver, err := s.dbFactory.GetReadOnlyDatabaseDriver(ctx, instance, database, "" /* dataSourceID */)
if err != nil {
return nil, 0, err
}
@@ -979,7 +979,7 @@ func (s *SQLService) postQuery(ctx context.Context, activity *store.ActivityMess
}
func (s *SQLService) doQuery(ctx context.Context, request *v1pb.QueryRequest, instance *store.InstanceMessage, database *store.DatabaseMessage, sensitiveSchemaInfo *base.SensitiveSchemaInfo) ([]*v1pb.QueryResult, int64, error) {
- driver, err := s.dbFactory.GetReadOnlyDatabaseDriver(ctx, instance, database)
+ driver, err := s.dbFactory.GetReadOnlyDatabaseDriver(ctx, instance, database, request.DataSourceId)
if err != nil {
return nil, 0, err
}
diff --git a/backend/component/dbfactory/dbfactory.go b/backend/component/dbfactory/dbfactory.go
index b44f4a6627..e9078e1d48 100644
--- a/backend/component/dbfactory/dbfactory.go
+++ b/backend/component/dbfactory/dbfactory.go
@@ -68,12 +68,22 @@ func (d *DBFactory) GetAdminDatabaseDriver(ctx context.Context, instance *store.
// GetReadOnlyDatabaseDriver gets the read-only database driver using the instance's read-only data source.
// If the read-only data source is not defined, we will fallback to admin data source.
// Upon successful return, caller must call driver.Close(). Otherwise, it will leak the database connection.
-func (d *DBFactory) GetReadOnlyDatabaseDriver(ctx context.Context, instance *store.InstanceMessage, database *store.DatabaseMessage) (db.Driver, error) {
- dataSource := utils.DataSourceFromInstanceWithType(instance, api.RO)
- adminDataSource := utils.DataSourceFromInstanceWithType(instance, api.Admin)
- // If there are no read-only data source, fall back to admin data source.
- if dataSource == nil {
- dataSource = adminDataSource
+func (d *DBFactory) GetReadOnlyDatabaseDriver(ctx context.Context, instance *store.InstanceMessage, database *store.DatabaseMessage, dataSourceID string) (db.Driver, error) {
+ var dataSource *store.DataSourceMessage
+ if dataSourceID == "" {
+ dataSource = utils.DataSourceFromInstanceWithType(instance, api.RO)
+ adminDataSource := utils.DataSourceFromInstanceWithType(instance, api.Admin)
+ // If there are no read-only data source, fall back to admin data source.
+ if dataSource == nil {
+ dataSource = adminDataSource
+ }
+ } else {
+ for _, ds := range instance.DataSources {
+ if ds.ID == dataSourceID {
+ dataSource = ds
+ break
+ }
+ }
}
if dataSource == nil {
return nil, common.Errorf(common.Internal, "data source not found for instance %q", instance.Title)