Skip to content

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

fix: data source id is optional in export (#16220)

h3n4lbe4dd65fc8f4
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index ffc21ca0cf..97679c62cc 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -180,7 +180,7 @@ func (s *SQLService) Query(ctx context.Context, request *v1pb.QueryRequest) (*v1
}
}
- dataSource, err := checkAndGetDataSourceQueriable(ctx, s.store, database, request.DataSourceId)
+ dataSource, err := checkAndGetDataSourceQueriable(ctx, s.store, database, request.DataSourceId, false)
if err != nil {
return nil, err
}
@@ -825,7 +825,7 @@ func DoExport(
optionalAccessCheck accessCheckFunc,
schemaSyncer *schemasync.Syncer,
) ([]byte, time.Duration, error) {
- dataSource, err := checkAndGetDataSourceQueriable(ctx, stores, database, request.DataSourceId)
+ dataSource, err := checkAndGetDataSourceQueriable(ctx, stores, database, request.DataSourceId, true)
if err != nil {
return nil, 0, err
}
@@ -1880,7 +1880,7 @@ func (*SQLService) Pretty(_ context.Context, request *v1pb.PrettyRequest) (*v1pb
}, nil
}
-func checkAndGetDataSourceQueriable(ctx context.Context, storeInstance *store.Store, database *store.DatabaseMessage, dataSourceID string) (*storepb.DataSource, error) {
+func checkAndGetDataSourceQueriable(ctx context.Context, storeInstance *store.Store, database *store.DatabaseMessage, dataSourceID string, export bool) (*storepb.DataSource, error) {
instance, err := storeInstance.GetInstanceV2(ctx, &store.FindInstanceMessage{ResourceID: &database.InstanceID})
if err != nil {
return nil, errors.Errorf("failed to get instance: %v", err)
@@ -1888,10 +1888,26 @@ func checkAndGetDataSourceQueriable(ctx context.Context, storeInstance *store.St
if instance == nil {
return nil, errors.Errorf("instance %q not found", database.InstanceID)
}
- if dataSourceID == "" {
+ if dataSourceID == "" && !export {
return nil, status.Errorf(codes.InvalidArgument, "data source id is required")
}
dataSource, serr := func() (*storepb.DataSource, *status.Status) {
+ if dataSourceID == "" && export {
+ var adminDataSource *storepb.DataSource
+ var readOnlyDataSource *storepb.DataSource
+ for _, ds := range instance.Metadata.GetDataSources() {
+ if ds.GetType() == storepb.DataSourceType_ADMIN && adminDataSource == nil {
+ adminDataSource = ds
+ }
+ if ds.GetType() == storepb.DataSourceType_READ_ONLY && readOnlyDataSource == nil {
+ readOnlyDataSource = ds
+ }
+ }
+ if readOnlyDataSource != nil {
+ return readOnlyDataSource, nil
+ }
+ return adminDataSource, nil
+ }
for _, ds := range instance.Metadata.GetDataSources() {
if ds.GetId() == dataSourceID {
return ds, nil
diff --git a/proto/gen/grpc-doc/openapi.yaml b/proto/gen/grpc-doc/openapi.yaml
index 875ec99a00..762ec202c1 100644
--- a/proto/gen/grpc-doc/openapi.yaml
+++ b/proto/gen/grpc-doc/openapi.yaml
@@ -11700,6 +11700,7 @@ components:
QueryRequest:
required:
- name
+ - dataSourceId
type: object
properties:
name:
diff --git a/proto/generated-go/v1/sql_service.pb.go b/proto/generated-go/v1/sql_service.pb.go
index c65d751490..bdb9dcd0cb 100644
--- a/proto/generated-go/v1/sql_service.pb.go
+++ b/proto/generated-go/v1/sql_service.pb.go
@@ -2686,13 +2686,13 @@ const file_v1_sql_service_proto_rawDesc = "" +
"\n" +
"_containerJ\x04\b\x02\x10\x03\"J\n" +
"\x14AdminExecuteResponse\x122\n" +
- "\aresults\x18\x01 \x03(\v2\x18.bytebase.v1.QueryResultR\aresults\"\xd2\x02\n" +
+ "\aresults\x18\x01 \x03(\v2\x18.bytebase.v1.QueryResultR\aresults\"\xd8\x02\n" +
"\fQueryRequest\x122\n" +
"\x04name\x18\x01 \x01(\tB\x1e\xe2A\x01\x02\xfaA\x17\n" +
"\x15bytebase.com/DatabaseR\x04name\x12\x1c\n" +
"\tstatement\x18\x03 \x01(\tR\tstatement\x12\x14\n" +
- "\x05limit\x18\x04 \x01(\x05R\x05limit\x12$\n" +
- "\x0edata_source_id\x18\x06 \x01(\tR\fdataSourceId\x12\x18\n" +
+ "\x05limit\x18\x04 \x01(\x05R\x05limit\x12*\n" +
+ "\x0edata_source_id\x18\x06 \x01(\tB\x04\xe2A\x01\x02R\fdataSourceId\x12\x18\n" +
"\aexplain\x18\a \x01(\bR\aexplain\x12\x1b\n" +
"\x06schema\x18\b \x01(\tH\x00R\x06schema\x88\x01\x01\x12;\n" +
"\fquery_option\x18\t \x01(\v2\x18.bytebase.v1.QueryOptionR\vqueryOption\x12!\n" +
diff --git a/proto/v1/v1/sql_service.proto b/proto/v1/v1/sql_service.proto
index 8e5bb26394..b300957467 100644
--- a/proto/v1/v1/sql_service.proto
+++ b/proto/v1/v1/sql_service.proto
@@ -160,7 +160,7 @@ message QueryRequest {
// It is used for querying admin data source even if the instance has
// read-only data sources. Or it can be used to query a specific read-only
// data source.
- string data_source_id = 6;
+ string data_source_id = 6 [(google.api.field_behavior) = REQUIRED];
// Explain the statement.
bool explain = 7;