Skip to content

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

fix: default datasource (#14692)

p0nyff6a2111dacb
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index fdc8bc0ed3..ae256be29e 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -1624,28 +1624,43 @@ func getOffsetAndOriginTable(backupTable string) (int, string, error) {
}
func checkAndGetDataSourceQueriable(ctx context.Context, storeInstance *store.Store, database *store.DatabaseMessage, dataSourceID string) (*store.DataSourceMessage, error) {
- if dataSourceID == "" {
- readOnlyDataSources, err := storeInstance.ListDataSourcesV2(ctx, &store.FindDataSourceMessage{
+ dataSource, serr := func() (*store.DataSourceMessage, *status.Status) {
+ // dataSourceID unspecified, we find a readonly dataSource
+ // first and fallback to admin dataSource.
+ if dataSourceID == "" {
+ dataSources, err := storeInstance.ListDataSourcesV2(ctx, &store.FindDataSourceMessage{
+ InstanceID: &database.InstanceID,
+ })
+ if err != nil {
+ return nil, status.Newf(codes.Internal, "failed to list data sources: %v", err)
+ }
+ for _, ds := range dataSources {
+ if ds.Type == api.RO {
+ return ds, nil
+ }
+ }
+ for _, ds := range dataSources {
+ if ds.Type == api.Admin {
+ return ds, nil
+ }
+ }
+ return nil, status.Newf(codes.FailedPrecondition, "no data source found")
+ }
+
+ dataSource, err := storeInstance.GetDataSource(ctx, &store.FindDataSourceMessage{
InstanceID: &database.InstanceID,
+ Name: &dataSourceID,
})
if err != nil {
- return nil, status.Errorf(codes.Internal, "failed to list data sources: %v", err)
+ return nil, status.Newf(codes.Internal, "failed to get data source: %v", err)
}
- if len(readOnlyDataSources) == 0 {
- return nil, status.Errorf(codes.NotFound, "no data source found")
+ if dataSource == nil {
+ return nil, status.Newf(codes.NotFound, "data source %q not found", dataSourceID)
}
- return readOnlyDataSources[0], nil
- }
-
- dataSource, err := storeInstance.GetDataSource(ctx, &store.FindDataSourceMessage{
- InstanceID: &database.InstanceID,
- Name: &dataSourceID,
- })
- if err != nil {
- return nil, status.Errorf(codes.Internal, "failed to get data source: %v", err)
- }
- if dataSource == nil {
- return nil, status.Errorf(codes.NotFound, "data source %q not found", dataSourceID)
+ return dataSource, nil
+ }()
+ if serr != nil {
+ return nil, serr.Err()
}
// Always allow non-admin data source.