Skip to content

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

chore: set default data source (#13230)

boojackdb7a5d207fc0
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index e1c3fed1aa..841bf42016 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -672,9 +672,34 @@ func (s *SQLService) Query(ctx context.Context, request *v1pb.QueryRequest) (*v1
return nil, err
}
+ dataSourceID := request.DataSourceId
+ if dataSourceID == "" {
+ dataSources, err := s.store.ListDataSourcesV2(ctx, &store.FindDataSourceMessage{
+ InstanceID: &database.InstanceID,
+ })
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to list data sources: %v", err)
+ }
+ if len(dataSources) == 0 {
+ return nil, status.Errorf(codes.NotFound, "no data source found")
+ }
+ readOnlyDataSources := make([]*store.DataSourceMessage, 0)
+ for _, dataSource := range dataSources {
+ if dataSource.Type == api.RO {
+ readOnlyDataSources = append(readOnlyDataSources, dataSource)
+ }
+ }
+ // First try to use read-only data source if available.
+ if len(readOnlyDataSources) > 0 {
+ dataSourceID = readOnlyDataSources[0].ID
+ } else {
+ dataSourceID = dataSources[0].ID
+ }
+ }
+
dataSource, err := s.store.GetDataSource(ctx, &store.FindDataSourceMessage{
InstanceID: &database.InstanceID,
- Name: &request.DataSourceId,
+ Name: &dataSourceID,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get data source: %v", err)
diff --git a/backend/store/data_source.go b/backend/store/data_source.go
index 4b1a75b70d..17f21d3bc7 100644
--- a/backend/store/data_source.go
+++ b/backend/store/data_source.go
@@ -158,7 +158,7 @@ type UpdateDataSourceMessage struct {
MasterObfuscatedPassword *string
}
-func (*Store) listDataSourceV2(ctx context.Context, tx *Tx, find *FindDataSourceMessage) (map[string][]*DataSourceMessage, error) {
+func (*Store) listInstanceDataSourceMap(ctx context.Context, tx *Tx, find *FindDataSourceMessage) (map[string][]*DataSourceMessage, error) {
where, args := []string{"TRUE"}, []any{}
if find.ID != nil {
where, args = append(where, fmt.Sprintf("data_source.id = $%d", len(args)+1)), append(args, *find.ID)
@@ -256,51 +256,36 @@ func (*Store) listDataSourceV2(ctx context.Context, tx *Tx, find *FindDataSource
return instanceDataSourcesMap, nil
}
-func (s *Store) ListDataSourcesV2(ctx context.Context, find *FindDataSourceMessage) (map[string][]*DataSourceMessage, error) {
+func (s *Store) ListDataSourcesV2(ctx context.Context, find *FindDataSourceMessage) ([]*DataSourceMessage, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, errors.New("Failed to begin transaction")
}
defer tx.Rollback()
- dataSources, err := s.listDataSourceV2(ctx, tx, find)
+ dataSourceMap, err := s.listInstanceDataSourceMap(ctx, tx, find)
if err != nil {
return nil, err
}
-
+ dataSources := []*DataSourceMessage{}
+ for _, list := range dataSourceMap {
+ dataSources = append(dataSources, list...)
+ }
return dataSources, nil
}
func (s *Store) GetDataSource(ctx context.Context, find *FindDataSourceMessage) (*DataSourceMessage, error) {
- tx, err := s.db.BeginTx(ctx, nil)
- if err != nil {
- return nil, errors.New("Failed to begin transaction")
- }
- defer tx.Rollback()
-
- dataSources, err := s.listDataSourceV2(ctx, tx, find)
+ dataSources, err := s.ListDataSourcesV2(ctx, find)
if err != nil {
return nil, err
}
-
if len(dataSources) == 0 {
return nil, nil
}
if len(dataSources) > 1 {
return nil, errors.Errorf("found %d data sources, but expected 1", len(dataSources))
}
-
- for _, dataSources := range dataSources {
- if len(dataSources) == 0 {
- return nil, nil
- }
- if len(dataSources) > 1 {
- return nil, errors.Errorf("found %d data sources, but expected 1", len(dataSources))
- }
- return dataSources[0], nil
- }
-
- return nil, nil
+ return dataSources[0], nil
}
// AddDataSourceToInstanceV2 adds a RO data source to an instance and return the instance where the data source is added.
diff --git a/backend/store/instance.go b/backend/store/instance.go
index 99e3429708..ec79b39644 100644
--- a/backend/store/instance.go
+++ b/backend/store/instance.go
@@ -196,7 +196,7 @@ func (s *Store) CreateInstanceV2(ctx context.Context, instanceCreate *InstanceMe
}
}
- instanceDataSourcesMap, err := s.listDataSourceV2(ctx, tx, &FindDataSourceMessage{
+ instanceDataSourcesMap, err := s.listInstanceDataSourceMap(ctx, tx, &FindDataSourceMessage{
InstanceID: &instanceCreate.ResourceID,
})
if err != nil {
@@ -347,7 +347,7 @@ func (s *Store) UpdateInstanceV2(ctx context.Context, patch *UpdateInstanceMessa
}
}
instance.Deleted = convertRowStatusToDeleted(rowStatus)
- instanceDataSourcesMap, err := s.listDataSourceV2(ctx, tx, &FindDataSourceMessage{
+ instanceDataSourcesMap, err := s.listInstanceDataSourceMap(ctx, tx, &FindDataSourceMessage{
InstanceID: &patch.ResourceID,
})
if err != nil {
@@ -464,7 +464,7 @@ func (s *Store) listInstanceImplV2(ctx context.Context, tx *Tx, find *FindInstan
if len(instanceMessages) == 1 {
dataSourceFind.InstanceID = &instanceMessages[0].ResourceID
}
- instanceDataSourcesMap, err := s.listDataSourceV2(ctx, tx, dataSourceFind)
+ instanceDataSourcesMap, err := s.listInstanceDataSourceMap(ctx, tx, dataSourceFind)
if err != nil {
return nil, err
}