Skip to content

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

chore: support instance project filter (#9539)

Danny Xu1220ed039b92
diff --git a/backend/api/v1/instance_service.go b/backend/api/v1/instance_service.go
index 172e8ac314..6e85352754 100644
--- a/backend/api/v1/instance_service.go
+++ b/backend/api/v1/instance_service.go
@@ -65,9 +65,23 @@ func (s *InstanceService) GetInstance(ctx context.Context, request *v1pb.GetInst
// ListInstances lists all instances.
func (s *InstanceService) ListInstances(ctx context.Context, request *v1pb.ListInstancesRequest) (*v1pb.ListInstancesResponse, error) {
+ var project *store.ProjectMessage
+ if request.Parent != "" {
+ p, err := s.getProjectMessage(ctx, request.Parent)
+ if err != nil {
+ return nil, err
+ }
+ if p.Deleted {
+ return nil, status.Errorf(codes.NotFound, "project %q has been deleted", request.Parent)
+ }
+ project = p
+ }
find := &store.FindInstanceMessage{
ShowDeleted: request.ShowDeleted,
}
+ if project != nil {
+ find.ProjectUID = &project.UID
+ }
instances, err := s.store.ListInstancesV2(ctx, find)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
diff --git a/backend/store/instance.go b/backend/store/instance.go
index 132dde351a..7c1554c161 100644
--- a/backend/store/instance.go
+++ b/backend/store/instance.go
@@ -58,6 +58,7 @@ type FindInstanceMessage struct {
EnvironmentID *string
ResourceID *string
ShowDeleted bool
+ ProjectUID *int
}
// GetInstanceV2 gets an instance by the resource_id.
@@ -393,6 +394,9 @@ func (s *Store) listInstanceImplV2(ctx context.Context, tx *Tx, find *FindInstan
if v := find.UID; v != nil {
where, args = append(where, fmt.Sprintf("instance.id = $%d", len(args)+1)), append(args, *v)
}
+ if v := find.ProjectUID; v != nil {
+ where, args = append(where, fmt.Sprintf("instance.id IN (SELECT DISTINCT instance_id FROM db WHERE project_id = $%d)", len(args)+1)), append(args, *v)
+ }
if !find.ShowDeleted {
where, args = append(where, fmt.Sprintf("instance.row_status = $%d", len(args)+1)), append(args, api.Normal)
}
diff --git a/scripts/.air.toml b/scripts/.air.toml
index 5744a39e2d..75bec75ead 100644
--- a/scripts/.air.toml
+++ b/scripts/.air.toml
@@ -16,6 +16,7 @@ delay = 500
exclude_dir = [
".air",
"bytebase-build",
+ "backend/server/dist",
"docs",
"frontend",
"helm-charts",