Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: check environments used by databases (#15821)
p0ny58f77e0d9090
diff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.goindex 9605a091fa..05ba417e88 100644--- a/backend/api/v1/setting_service.go+++ b/backend/api/v1/setting_service.go@@ -623,6 +623,13 @@ func (s *SettingService) UpdateSetting(ctx context.Context, request *v1pb.Updateif count > 0 {return nil, status.Errorf(codes.FailedPrecondition, "all instances in the environment %v should be deleted first", env.Id)}+ uses, err := s.store.CheckDatabaseUseEnvironment(ctx, env.Id)+ if err != nil {+ return nil, status.Error(codes.Internal, err.Error())+ }+ if uses {+ return nil, status.Errorf(codes.FailedPrecondition, "all databases in the environment %v should be deleted first", env.Id)+ }}}diff --git a/backend/store/environment.go b/backend/store/environment.goindex 6c687577b3..1c4da1764c 100644--- a/backend/store/environment.go+++ b/backend/store/environment.go@@ -3,6 +3,8 @@ package storeimport ("context"+ "github.com/pkg/errors"+storepb "github.com/bytebase/bytebase/proto/generated-go/store")@@ -18,3 +20,19 @@ func (s *Store) GetEnvironmentByID(ctx context.Context, id string) (*storepb.Env}return nil, nil}++func (s *Store) CheckDatabaseUseEnvironment(ctx context.Context, id string) (bool, error) {+ var exists bool++ if err := s.db.QueryRowContext(ctx, `+ SELECT EXISTS (+ SELECT 1+ FROM db+ WHERE db.environment = $1+ )+ `, id).Scan(&exists); err != nil {+ return false, errors.Wrapf(err, "failed to check if databases uses environment %q", id)+ }++ return exists, nil+}