Skip to content

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

feat: add archive checkers for environment/project (#1454)

qsliu8fd5a13ec003
diff --git a/api/instance.go b/api/instance.go
index ac8e754c74..9f4b5e5921 100644
--- a/api/instance.go
+++ b/api/instance.go
@@ -67,6 +67,9 @@ type InstanceFind struct {
// Standard fields
RowStatus *RowStatus
+
+ // Related fields
+ EnvironmentID *int
}
func (find *InstanceFind) String() string {
diff --git a/server/environment.go b/server/environment.go
index 97c31acb30..97378a39fc 100644
--- a/server/environment.go
+++ b/server/environment.go
@@ -73,6 +73,21 @@ func (s *Server) registerEnvironmentRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformed patch environment request").SetInternal(err)
}
+ // Ensure the environment has no instance before it's archived.
+ if v := envPatch.RowStatus; v != nil && *v == string(api.Archived) {
+ archivedStatus := api.Archived
+ instances, err := s.store.FindInstance(ctx, &api.InstanceFind{
+ EnvironmentID: &id,
+ RowStatus: &archivedStatus,
+ })
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("Failed to find instances in the environment %d", id)).SetInternal(err)
+ }
+ if len(instances) > 0 {
+ return echo.NewHTTPError(http.StatusBadRequest, "You should archive all instances under the environment before archiving the environment.")
+ }
+ }
+
env, err := s.store.PatchEnvironment(ctx, envPatch)
if err != nil {
if common.ErrorCode(err) == common.NotFound {
diff --git a/server/project.go b/server/project.go
index de1e30c130..90a5c7b01d 100644
--- a/server/project.go
+++ b/server/project.go
@@ -157,6 +157,17 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformed patch project request").SetInternal(err)
}
+ // Ensure the project has no database before it's archived.
+ if v := projectPatch.RowStatus; v != nil && *v == string(api.Archived) {
+ databases, err := s.store.FindDatabase(ctx, &api.DatabaseFind{ProjectID: &id})
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("Failed to find databases in the project %d", id)).SetInternal(err)
+ }
+ if len(databases) > 0 {
+ return echo.NewHTTPError(http.StatusBadRequest, "You should transfer all databases under the project before archiving the project.")
+ }
+ }
+
project, err := s.store.PatchProject(ctx, projectPatch)
if err != nil {
if common.ErrorCode(err) == common.NotFound {
diff --git a/store/instance.go b/store/instance.go
index d74f42c282..b1f351126d 100644
--- a/store/instance.go
+++ b/store/instance.go
@@ -580,6 +580,9 @@ func findInstanceQuery(find *api.InstanceFind) (string, []interface{}) {
if v := find.RowStatus; v != nil {
where, args = append(where, fmt.Sprintf("row_status = $%d", len(args)+1)), append(args, *v)
}
+ if v := find.EnvironmentID; v != nil {
+ where, args = append(where, fmt.Sprintf("environment_id = $%d", len(args)+1)), append(args, *v)
+ }
return strings.Join(where, " AND "), args
}