Skip to content

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

chore(store): migrate anomaly legacy store to v1 store (#5000)

h3n4l90392111cde1
diff --git a/backend/api/v1/anomaly_service.go b/backend/api/v1/anomaly_service.go
index 239e854db8..5d6eeb7c40 100644
--- a/backend/api/v1/anomaly_service.go
+++ b/backend/api/v1/anomaly_service.go
@@ -57,7 +57,7 @@ func (s *AnomalyService) SearchAnomalies(ctx context.Context, request *v1pb.Sear
}
for _, tp := range types {
if v, ok := typesMap[tp]; ok {
- find.Types = append(find.Types, &v)
+ find.Types = append(find.Types, v)
} else {
return nil, status.Errorf(codes.InvalidArgument, "Invalid type filter %q", tp)
}
diff --git a/backend/legacyapi/anomaly.go b/backend/legacyapi/anomaly.go
index d3aa361ff2..7b904ba396 100644
--- a/backend/legacyapi/anomaly.go
+++ b/backend/legacyapi/anomaly.go
@@ -1,9 +1,5 @@
package api
-import (
- "encoding/json"
-)
-
// AnomalyType is the type of a task.
type AnomalyType string
@@ -111,45 +107,3 @@ type Anomaly struct {
Severity AnomalySeverity `jsonapi:"attr,severity"`
Payload string `jsonapi:"attr,payload"`
}
-
-// AnomalyUpsert is the API message for creating an anomaly.
-type AnomalyUpsert struct {
- // Standard fields
- CreatorID int
-
- // Related fields
- InstanceID int
- DatabaseID *int
-
- // Domain specific fields
- Type AnomalyType `jsonapi:"attr,type"`
- Payload string `jsonapi:"attr,payload"`
-}
-
-// AnomalyFind is the API message for finding anomalies.
-type AnomalyFind struct {
- // Standard fields
- RowStatus *RowStatus
-
- // Related fields
- InstanceID *int
- DatabaseID *int
- Type *AnomalyType
- // Only applicable if InstanceID is specified, if true, then we only return instance anomaly (database_id is NULL)
- InstanceOnly bool
-}
-
-func (find *AnomalyFind) String() string {
- str, err := json.Marshal(*find)
- if err != nil {
- return err.Error()
- }
- return string(str)
-}
-
-// AnomalyArchive is the API message for archiving an anomaly.
-type AnomalyArchive struct {
- InstanceID *int
- DatabaseID *int
- Type AnomalyType
-}
diff --git a/backend/runner/anomaly/scanner.go b/backend/runner/anomaly/scanner.go
index e7ff62f0c3..de41562941 100644
--- a/backend/runner/anomaly/scanner.go
+++ b/backend/runner/anomaly/scanner.go
@@ -164,11 +164,10 @@ func (s *Scanner) checkInstanceAnomaly(ctx context.Context, instance *store.Inst
zap.String("type", string(api.AnomalyInstanceConnection)),
zap.Error(err))
} else {
- if _, err = s.store.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
- CreatorID: api.SystemBotID,
- InstanceID: instance.UID,
- Type: api.AnomalyInstanceConnection,
- Payload: string(payload),
+ if _, err = s.store.UpsertActiveAnomalyV2(ctx, api.SystemBotID, &store.AnomalyMessage{
+ InstanceUID: instance.UID,
+ Type: api.AnomalyInstanceConnection,
+ Payload: string(payload),
}); err != nil {
log.Error("Failed to create anomaly",
zap.String("instance", instance.ResourceID),
@@ -180,9 +179,9 @@ func (s *Scanner) checkInstanceAnomaly(ctx context.Context, instance *store.Inst
}
defer driver.Close(ctx)
- err = s.store.ArchiveAnomaly(ctx, &api.AnomalyArchive{
- InstanceID: &instance.UID,
- Type: api.AnomalyInstanceConnection,
+ err = s.store.ArchiveAnomalyV2(ctx, &store.ArchiveAnomalyMessage{
+ InstanceUID: &instance.UID,
+ Type: api.AnomalyInstanceConnection,
})
if err != nil && common.ErrorCode(err) != common.NotFound {
log.Error("Failed to close anomaly",
@@ -201,10 +200,9 @@ func (s *Scanner) checkInstanceAnomaly(ctx context.Context, instance *store.Inst
zap.Error(err))
} else {
if setup {
- if _, err = s.store.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
- CreatorID: api.SystemBotID,
- InstanceID: instance.UID,
- Type: api.AnomalyInstanceMigrationSchema,
+ if _, err = s.store.UpsertActiveAnomalyV2(ctx, api.SystemBotID, &store.AnomalyMessage{
+ InstanceUID: instance.UID,
+ Type: api.AnomalyInstanceMigrationSchema,
}); err != nil {
log.Error("Failed to create anomaly",
zap.String("instance", instance.ResourceID),
@@ -212,9 +210,9 @@ func (s *Scanner) checkInstanceAnomaly(ctx context.Context, instance *store.Inst
zap.Error(err))
}
} else {
- err := s.store.ArchiveAnomaly(ctx, &api.AnomalyArchive{
- InstanceID: &instance.UID,
- Type: api.AnomalyInstanceMigrationSchema,
+ err := s.store.ArchiveAnomalyV2(ctx, &store.ArchiveAnomalyMessage{
+ InstanceUID: &instance.UID,
+ Type: api.AnomalyInstanceMigrationSchema,
})
if err != nil && common.ErrorCode(err) != common.NotFound {
log.Error("Failed to close anomaly",
@@ -243,12 +241,11 @@ func (s *Scanner) checkDatabaseAnomaly(ctx context.Context, instance *store.Inst
zap.String("type", string(api.AnomalyDatabaseConnection)),
zap.Error(err))
} else {
- if _, err = s.store.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
- CreatorID: api.SystemBotID,
- InstanceID: instance.UID,
- DatabaseID: &database.UID,
- Type: api.AnomalyDatabaseConnection,
- Payload: string(payload),
+ if _, err = s.store.UpsertActiveAnomalyV2(ctx, api.SystemBotID, &store.AnomalyMessage{
+ InstanceUID: instance.UID,
+ DatabaseUID: &database.UID,
+ Type: api.AnomalyDatabaseConnection,
+ Payload: string(payload),
}); err != nil {
log.Error("Failed to create anomaly",
zap.String("instance", instance.ResourceID),
@@ -260,9 +257,9 @@ func (s *Scanner) checkDatabaseAnomaly(ctx context.Context, instance *store.Inst
return
}
defer driver.Close(ctx)
- err = s.store.ArchiveAnomaly(ctx, &api.AnomalyArchive{
- DatabaseID: &database.UID,
- Type: api.AnomalyDatabaseConnection,
+ err = s.store.ArchiveAnomalyV2(ctx, &store.ArchiveAnomalyMessage{
+ DatabaseUID: &database.UID,
+ Type: api.AnomalyDatabaseConnection,
})
if err != nil && common.ErrorCode(err) != common.NotFound {
log.Error("Failed to close anomaly",
@@ -332,12 +329,11 @@ func (s *Scanner) checkDatabaseAnomaly(ctx context.Context, instance *store.Inst
zap.String("type", string(api.AnomalyDatabaseSchemaDrift)),
zap.Error(err))
} else {
- if _, err = s.store.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
- CreatorID: api.SystemBotID,
- InstanceID: instance.UID,
- DatabaseID: &database.UID,
- Type: api.AnomalyDatabaseSchemaDrift,
- Payload: string(payload),
+ if _, err = s.store.UpsertActiveAnomalyV2(ctx, api.SystemBotID, &store.AnomalyMessage{
+ InstanceUID: instance.UID,
+ DatabaseUID: &database.UID,
+ Type: api.AnomalyDatabaseSchemaDrift,
+ Payload: string(payload),
}); err != nil {
log.Error("Failed to create anomaly",
zap.String("instance", instance.ResourceID),
@@ -347,9 +343,9 @@ func (s *Scanner) checkDatabaseAnomaly(ctx context.Context, instance *store.Inst
}
}
} else {
- err := s.store.ArchiveAnomaly(ctx, &api.AnomalyArchive{
- DatabaseID: &database.UID,
- Type: api.AnomalyDatabaseSchemaDrift,
+ err := s.store.ArchiveAnomalyV2(ctx, &store.ArchiveAnomalyMessage{
+ DatabaseUID: &database.UID,
+ Type: api.AnomalyDatabaseSchemaDrift,
})
if err != nil && common.ErrorCode(err) != common.NotFound {
log.Error("Failed to close anomaly",
@@ -417,12 +413,11 @@ func (s *Scanner) checkBackupAnomaly(ctx context.Context, environment *store.Env
zap.String("type", string(api.AnomalyDatabaseBackupPolicyViolation)),
zap.Error(err))
} else {
- if _, err = s.store.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
- CreatorID: api.SystemBotID,
- InstanceID: instance.UID,
- DatabaseID: &database.UID,
- Type: api.AnomalyDatabaseBackupPolicyViolation,
- Payload: string(payload),
+ if _, err = s.store.UpsertActiveAnomalyV2(ctx, api.SystemBotID, &store.AnomalyMessage{
+ InstanceUID: instance.UID,
+ DatabaseUID: &database.UID,
+ Type: api.AnomalyDatabaseBackupPolicyViolation,
+ Payload: string(payload),
}); err != nil {
log.Error("Failed to create anomaly",
zap.String("instance", instance.ResourceID),
@@ -432,9 +427,9 @@ func (s *Scanner) checkBackupAnomaly(ctx context.Context, environment *store.Env
}
}
} else {
- err := s.store.ArchiveAnomaly(ctx, &api.AnomalyArchive{
- DatabaseID: &database.UID,
- Type: api.AnomalyDatabaseBackupPolicyViolation,
+ err := s.store.ArchiveAnomalyV2(ctx, &store.ArchiveAnomalyMessage{
+ DatabaseUID: &database.UID,
+ Type: api.AnomalyDatabaseBackupPolicyViolation,
})
if err != nil && common.ErrorCode(err) != common.NotFound {
log.Error("Failed to close anomaly",
@@ -500,12 +495,11 @@ func (s *Scanner) checkBackupAnomaly(ctx context.Context, environment *store.Env
zap.String("type", string(api.AnomalyDatabaseBackupMissing)),
zap.Error(err))
} else {
- if _, err = s.store.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
- CreatorID: api.SystemBotID,
- InstanceID: instance.UID,
- DatabaseID: &database.UID,
- Type: api.AnomalyDatabaseBackupMissing,
- Payload: string(payload),
+ if _, err = s.store.UpsertActiveAnomalyV2(ctx, api.SystemBotID, &store.AnomalyMessage{
+ InstanceUID: instance.UID,
+ DatabaseUID: &database.UID,
+ Type: api.AnomalyDatabaseBackupMissing,
+ Payload: string(payload),
}); err != nil {
log.Error("Failed to create anomaly",
zap.String("instance", instance.ResourceID),
@@ -515,9 +509,9 @@ func (s *Scanner) checkBackupAnomaly(ctx context.Context, environment *store.Env
}
}
} else {
- err := s.store.ArchiveAnomaly(ctx, &api.AnomalyArchive{
- DatabaseID: &database.UID,
- Type: api.AnomalyDatabaseBackupMissing,
+ err := s.store.ArchiveAnomalyV2(ctx, &store.ArchiveAnomalyMessage{
+ DatabaseUID: &database.UID,
+ Type: api.AnomalyDatabaseBackupMissing,
})
if err != nil && common.ErrorCode(err) != common.NotFound {
log.Error("Failed to close anomaly",
diff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.go
index e4ed3ede68..9692f443e7 100644
--- a/backend/runner/taskrun/executor.go
+++ b/backend/runner/taskrun/executor.go
@@ -428,9 +428,9 @@ func postMigration(ctx context.Context, stores *store.Store, activityManager *ac
}
// Remove schema drift anomalies.
- if err := stores.ArchiveAnomaly(ctx, &api.AnomalyArchive{
- DatabaseID: task.DatabaseID,
- Type: api.AnomalyDatabaseSchemaDrift,
+ if err := stores.ArchiveAnomalyV2(ctx, &store.ArchiveAnomalyMessage{
+ DatabaseUID: task.DatabaseID,
+ Type: api.AnomalyDatabaseSchemaDrift,
}); err != nil && common.ErrorCode(err) != common.NotFound {
log.Error("Failed to archive anomaly",
zap.String("instance", instance.ResourceID),
diff --git a/backend/server/anomaly.go b/backend/server/anomaly.go
index 0ed6f89e88..f0f435815f 100644
--- a/backend/server/anomaly.go
+++ b/backend/server/anomaly.go
@@ -9,44 +9,51 @@ import (
"github.com/labstack/echo/v4"
api "github.com/bytebase/bytebase/backend/legacyapi"
+ "github.com/bytebase/bytebase/backend/store"
)
func (s *Server) registerAnomalyRoutes(g *echo.Group) {
g.GET("/anomaly", func(c echo.Context) error {
ctx := c.Request().Context()
normalRowStatus := api.Normal
- anomalyFind := &api.AnomalyFind{
+ find := &store.ListAnomalyMessage{
RowStatus: &normalRowStatus,
}
+
if instanceIDStr := c.QueryParam("instance"); instanceIDStr != "" {
instanceID, err := strconv.Atoi(instanceIDStr)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Query parameter instance is not a number: %s", instanceIDStr)).SetInternal(err)
}
- anomalyFind.InstanceOnly = true
- anomalyFind.InstanceID = &instanceID
+ find.InstanceUID = &instanceID
+ find.Types = append(find.Types, api.AnomalyInstanceConnection, api.AnomalyInstanceMigrationSchema)
}
if databaseIDStr := c.QueryParam("database"); databaseIDStr != "" {
databaseID, err := strconv.Atoi(databaseIDStr)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Query parameter database is not a number: %s", databaseIDStr)).SetInternal(err)
}
- anomalyFind.DatabaseID = &databaseID
+ find.DatabaseUID = &databaseID
}
if rowStatus := c.QueryParam("rowStatus"); rowStatus != "" {
- anomalyFind.RowStatus = (*api.RowStatus)(&rowStatus)
+ find.RowStatus = (*api.RowStatus)(&rowStatus)
}
if anomalyType := c.QueryParam("type"); anomalyType != "" {
- anomalyFind.Type = (*api.AnomalyType)(&anomalyType)
+ find.Types = append(find.Types, api.AnomalyType(anomalyType))
}
- anomalyList, err := s.store.FindAnomaly(ctx, anomalyFind)
+ anomalyList, err := s.store.ListAnomalyV2(ctx, find)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch anomaly list").SetInternal(err)
}
+ var apiAnomalyList []*api.Anomaly
+ for _, anomaly := range anomalyList {
+ apiAnomalyList = append(apiAnomalyList, anomaly.ToAPIAnomaly())
+ }
+
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
- if err := jsonapi.MarshalPayload(c.Response().Writer, anomalyList); err != nil {
+ if err := jsonapi.MarshalPayload(c.Response().Writer, apiAnomalyList); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal anomaly list response").SetInternal(err)
}
return nil
diff --git a/backend/store/anomaly.go b/backend/store/anomaly.go
index 0f10905a7a..eff795305c 100644
--- a/backend/store/anomaly.go
+++ b/backend/store/anomaly.go
@@ -12,124 +12,62 @@ import (
api "github.com/bytebase/bytebase/backend/legacyapi"
)
-// anomalyRaw is the store model for an Anomaly.
-// Fields have exactly the same meanings as Anomaly.
-type anomalyRaw struct {
+// AnomalyMessage is the message of the anomaly.
+type AnomalyMessage struct {
+ // InstanceUID is the unique identifier of the instance.
+ InstanceUID int
+ // DatabaseUID is the unique identifier of the database, it will be nil if the anomaly is instance level.
+ DatabaseUID *int
+ // Type is the type of the anomaly.
+ Type api.AnomalyType
+ // Payload is the payload of the anomaly.
+ Payload string
+ // Output only fields.
+ //
+ // ID is the unique identifier of the anomaly.
ID int
-
- // Standard fields
- CreatorID int
+ // CreatedTs is the timestamp when the anomaly is created.
CreatedTs int64
- UpdaterID int
+ // UpdatedTs is the timestamp when the anomaly is updated.
UpdatedTs int64
-
- // Related fields
- InstanceID int
- DatabaseID *int
-
- // Domain specific fields
- Type api.AnomalyType
- // Calculated field derived from type
- Severity api.AnomalySeverity
- Payload string
}
-// toAnomaly creates an instance of Anomaly based on the anomalyRaw.
-// This is intended to be called when we need to compose an Anomaly relationship.
-func (raw *anomalyRaw) toAnomaly() *api.Anomaly {
+// ToAPIAnomaly converts the anomaly message to the api anomaly.
+func (a *AnomalyMessage) ToAPIAnomaly() *api.Anomaly {
return &api.Anomaly{
- ID: raw.ID,
-
- // Standard fields
- CreatorID: raw.CreatorID,
- CreatedTs: raw.CreatedTs,
- UpdaterID: raw.UpdaterID,
- UpdatedTs: raw.UpdatedTs,
-
- // Related fields
- InstanceID: raw.InstanceID,
- DatabaseID: raw.DatabaseID,
-
- // Domain specific fields
- Type: raw.Type,
- // Calculated field derived from type
- Severity: raw.Severity,
- Payload: raw.Payload,
+ ID: a.ID,
+ UpdatedTs: a.UpdatedTs,
+ CreatedTs: a.CreatedTs,
+ InstanceID: a.InstanceUID,
+ DatabaseID: a.DatabaseUID,
+ Type: a.Type,
+ Payload: a.Payload,
}
}
-// UpsertActiveAnomaly upserts an instance of anomaly.
-func (s *Store) UpsertActiveAnomaly(ctx context.Context, upsert *api.AnomalyUpsert) (*api.Anomaly, error) {
- anomalyRaw, err := s.upsertActiveAnomalyRaw(ctx, upsert)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to upsert active anomaly with AnomalyUpsert[%+v]", upsert)
- }
- anomaly, err := s.composeAnomaly(ctx, anomalyRaw)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to compose anomaly with AnomalyRaw[%+v]", anomalyRaw)
- }
- return anomaly, nil
+// ListAnomalyMessage is the message to list anomalies.
+type ListAnomalyMessage struct {
+ RowStatus *api.RowStatus
+ InstanceUID *int
+ DatabaseUID *int
+ Types []api.AnomalyType
}
-// FindAnomaly finds a list of Anomaly instances.
-func (s *Store) FindAnomaly(ctx context.Context, find *api.AnomalyFind) ([]*api.Anomaly, error) {
- anomalyRawList, err := s.findAnomalyRaw(ctx, find)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to find anomaly with AnomalyFind[%+v]", find)
- }
- var anomalyList []*api.Anomaly
- for _, anomalyRaw := range anomalyRawList {
- anomaly, err := s.composeAnomaly(ctx, anomalyRaw)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to compose anomaly with AnomalyRaw[%+v]", anomalyRaw)
- }
- anomalyList = append(anomalyList, anomaly)
- }
- return anomalyList, nil
+// ArchiveAnomalyMessage is the message to archive an anomaly.
+type ArchiveAnomalyMessage struct {
+ InstanceUID *int
+ DatabaseUID *int
+ Type api.AnomalyType
}
-//
-// private functions
-//
-
-func (s *Store) composeAnomaly(ctx context.Context, raw *anomalyRaw) (*api.Anomaly, error) {
- anomaly := raw.toAnomaly()
-
- creator, err := s.GetPrincipalByID(ctx, anomaly.CreatorID)
- if err != nil {
- return nil, err
- }
- anomaly.Creator = creator
-
- updater, err := s.GetPrincipalByID(ctx, anomaly.UpdaterID)
- if err != nil {
- return nil, err
- }
- anomaly.Updater = updater
-
- instance, err := s.GetInstanceByID(ctx, anomaly.InstanceID)
- if err != nil {
- return nil, err
- }
- anomaly.Instance = instance
-
- if anomaly.DatabaseID != nil {
- database, err := s.GetDatabase(ctx, &api.DatabaseFind{
- ID: anomaly.DatabaseID,
- })
- if err != nil {
- return nil, err
- }
- anomaly.Database = database
- }
-
- return anomaly, nil
+// updateAnomalyMessage is the message to update an anomaly.
+type updateAnomalyMessage struct {
+ ID int
+ Payload string
}
-// upsertActiveAnomalyRaw would update the existing active anomaly if both database id and type match, otherwise create a new one.
-// Do not use ON CONFLICT (upsert syntax) as it will consume auto-increment id. Functional wise, this is fine, but
-// from the UX perspective, it's not great, since user will see large id gaps.
-func (s *Store) upsertActiveAnomalyRaw(ctx context.Context, upsert *api.AnomalyUpsert) (*anomalyRaw, error) {
+// UpsertActiveAnomalyV2 upserts an instance of anomaly.
+func (s *Store) UpsertActiveAnomalyV2(ctx context.Context, principalUID int, upsert *AnomalyMessage) (*AnomalyMessage, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -137,31 +75,34 @@ func (s *Store) upsertActiveAnomalyRaw(ctx context.Context, upsert *api.AnomalyU
defer tx.Rollback()
status := api.Normal
- find := &api.AnomalyFind{
- RowStatus: &status,
- InstanceID: &upsert.InstanceID,
- DatabaseID: upsert.DatabaseID,
- Type: &upsert.Type,
+ find := &ListAnomalyMessage{
+ RowStatus: &status,
+ InstanceUID: &upsert.InstanceUID,
+ DatabaseUID: upsert.DatabaseUID,
+ Types: []api.AnomalyType{upsert.Type},
}
- list, err := findAnomalyListImpl(ctx, tx, find)
+ list, err := s.listAnomalyImplV2(ctx, tx, find)
if err != nil {
return nil, err
}
- var anomalyRaw *anomalyRaw
+ var anomaly *AnomalyMessage
if len(list) == 0 {
- anomalyRaw, err = createAnomalyImpl(ctx, tx, upsert)
+ anomaly, err = s.createAnomalyImplV2(ctx, tx, principalUID, &AnomalyMessage{
+ InstanceUID: upsert.InstanceUID,
+ DatabaseUID: upsert.DatabaseUID,
+ Type: upsert.Type,
+ Payload: upsert.Payload,
+ })
if err != nil {
return nil, err
}
} else if len(list) == 1 {
- // Even if field value does not change, we still patch to update the updated_ts
- patch := &anomalyPatch{
- ID: list[0].ID,
- UpdaterID: upsert.CreatorID,
- Payload: upsert.Payload,
- }
- anomalyRaw, err = patchAnomalyImpl(ctx, tx, patch)
+ // Even if field value does not change, we still patch to update the updated_ts.
+ anomaly, err = updateAnomalyV2(ctx, tx, principalUID, &updateAnomalyMessage{
+ ID: list[0].ID,
+ Payload: upsert.Payload,
+ })
if err != nil {
return nil, err
}
@@ -173,50 +114,86 @@ func (s *Store) upsertActiveAnomalyRaw(ctx context.Context, upsert *api.AnomalyU
return nil, FormatError(err)
}
- return anomalyRaw, nil
+ return anomaly, nil
}
-// findAnomalyRaw retrieves a list of anomalies based on the find condition.
-func (s *Store) findAnomalyRaw(ctx context.Context, find *api.AnomalyFind) ([]*anomalyRaw, error) {
+// ListAnomalyV2 lists anomalies, only return the normal ones.
+func (s *Store) ListAnomalyV2(ctx context.Context, list *ListAnomalyMessage) ([]*AnomalyMessage, error) {
+ // Build where clause
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
- return nil, FormatError(err)
+ return nil, errors.Wrapf(err, "failed to begin transaction")
}
defer tx.Rollback()
- list, err := findAnomalyListImpl(ctx, tx, find)
+ anomalies, err := s.listAnomalyImplV2(ctx, tx, list)
if err != nil {
return nil, err
}
- return list, nil
+ if err := tx.Commit(); err != nil {
+ return nil, errors.Wrapf(err, "failed to commit transaction")
+ }
+
+ return anomalies, nil
}
-// ArchiveAnomaly archives an existing anomaly by ID.
-// Returns ENOTFOUND if anomaly does not exist.
-func (s *Store) ArchiveAnomaly(ctx context.Context, archive *api.AnomalyArchive) error {
+// ArchiveAnomalyV2 archives an anomaly.
+func (s *Store) ArchiveAnomalyV2(ctx context.Context, archive *ArchiveAnomalyMessage) error {
+ if archive.InstanceUID == nil && archive.DatabaseUID == nil {
+ return &common.Error{Code: common.Internal, Err: errors.Errorf("failed to close anomaly, should specify either instanceID or databaseID")}
+ }
+ if archive.InstanceUID != nil && archive.DatabaseUID != nil {
+ return &common.Error{Code: common.Internal, Err: errors.Errorf("failed to close anomaly, should specify either instanceID or databaseID, but not both")}
+ }
+
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
- return FormatError(err)
+ return errors.Wrapf(err, "failed to begin transaction")
}
defer tx.Rollback()
- if err := archiveAnomalyImpl(ctx, tx, archive); err != nil {
- return FormatError(err)
+ // Remove row from database.
+ if archive.InstanceUID != nil {
+ result, err := tx.ExecContext(ctx,
+ `UPDATE anomaly SET row_status = $1 WHERE instance_id = $2 AND database_id IS NULL AND type = $3`,
+ api.Archived,
+ *archive.InstanceUID,
+ archive.Type,
+ )
+ if err != nil {
+ return FormatError(err)
+ }
+
+ if rows, _ := result.RowsAffected(); rows == 0 {
+ return &common.Error{Code: common.NotFound, Err: errors.Errorf("anomaly not found instance: %d type: %s", *archive.InstanceUID, archive.Type)}
+ }
+ } else if archive.DatabaseUID != nil {
+ result, err := tx.ExecContext(ctx,
+ `UPDATE anomaly SET row_status = $1 WHERE database_id = $2 AND type = $3`,
+ api.Archived,
+ *archive.DatabaseUID,
+ archive.Type,
+ )
+ if err != nil {
+ return FormatError(err)
+ }
+
+ if rows, _ := result.RowsAffected(); rows == 0 {
+ return &common.Error{Code: common.NotFound, Err: errors.Errorf("anomaly not found database: %d type: %s", *archive.DatabaseUID, archive.Type)}
+ }
}
if err := tx.Commit(); err != nil {
- return FormatError(err)
+ return errors.Wrapf(err, "failed to commit transaction")
}
-
return nil
}
-// createAnomalyImpl creates a new anomaly.
-func createAnomalyImpl(ctx context.Context, tx *Tx, upsert *api.AnomalyUpsert) (*anomalyRaw, error) {
+func (*Store) createAnomalyImplV2(ctx context.Context, tx *Tx, principalUID int, create *AnomalyMessage) (*AnomalyMessage, error) {
// Inserts row into database.
- if upsert.Payload == "" {
- upsert.Payload = "{}"
+ if create.Payload == "" {
+ create.Payload = "{}"
}
query := `
INSERT INTO anomaly (
@@ -228,242 +205,42 @@ func createAnomalyImpl(ctx context.Context, tx *Tx, upsert *api.AnomalyUpsert) (
payload
)
VALUES ($1, $2, $3, $4, $5, $6)
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, instance_id, database_id, type, payload
+ RETURNING id, instance_id, database_id, type, payload
`
- var anomalyRaw anomalyRaw
- var databaseID sql.NullInt32
+ var anomaly AnomalyMessage
+ var databaseUID sql.NullInt32
if err := tx.QueryRowContext(ctx, query,
- upsert.CreatorID,
- upsert.CreatorID,
- upsert.InstanceID,
- upsert.DatabaseID,
- upsert.Type,
- upsert.Payload,
+ principalUID,
+ principalUID,
+ create.InstanceUID,
+ create.DatabaseUID,
+ create.Type,
+ create.Payload,
).Scan(
- &anomalyRaw.ID,
- &anomalyRaw.CreatorID,
- &anomalyRaw.CreatedTs,
- &anomalyRaw.UpdaterID,
- &anomalyRaw.UpdatedTs,
- &anomalyRaw.InstanceID,
- &databaseID,
- &anomalyRaw.Type,
- &anomalyRaw.Payload,
+ &anomaly.ID,
+ &anomaly.InstanceUID,
+ databaseUID,
+ &anomaly.Type,
+ &anomaly.Payload,
); err != nil {
if err == sql.ErrNoRows {
return nil, common.FormatDBErrorEmptyRowWithQuery(query)
}
return nil, FormatError(err)
}
- if databaseID.Valid {
- value := int(databaseID.Int32)
- anomalyRaw.DatabaseID = &value
+ if databaseUID.Valid {
+ value := int(databaseUID.Int32)
+ anomaly.DatabaseUID = &value
}
- anomalyRaw.Severity = api.AnomalySeverityFromType(anomalyRaw.Type)
- return &anomalyRaw, nil
+
+ return &anomaly, nil
}
-func findAnomalyListImpl(ctx context.Context, tx *Tx, find *api.AnomalyFind) ([]*anomalyRaw, error) {
- // Build WHERE clause.
+func (*Store) listAnomalyImplV2(ctx context.Context, tx *Tx, list *ListAnomalyMessage) ([]*AnomalyMessage, error) {
where, args := []string{"TRUE"}, []interface{}{}
- if v := find.InstanceID; v != nil {
- where, args = append(where, fmt.Sprintf("instance_id = $%d", len(args)+1)), append(args, *v)
- if find.InstanceOnly {
- where = append(where, "database_id is NULL")
- }
- }
- if find.InstanceID == nil || !find.InstanceOnly {
- if v := find.DatabaseID; v != nil {
- where, args = append(where, fmt.Sprintf("database_id = $%d", len(args)+1)), append(args, *v)
- }
- }
- if v := find.RowStatus; v != nil {
+ if v := list.RowStatus; v != nil {
where, args = append(where, fmt.Sprintf("row_status = $%d", len(args)+1)), append(args, *v)
}
- if v := find.Type; v != nil {
- where, args = append(where, fmt.Sprintf("type = $%d", len(args)+1)), append(args, *v)
- }
-
- rows, err := tx.QueryContext(ctx, `
- SELECT
- id,
- creator_id,
- created_ts,
- updater_id,
- updated_ts,
- instance_id,
- database_id,
- type,
- payload
- FROM anomaly
- WHERE `+strings.Join(where, " AND ")+`
- AND EXISTS (
- SELECT 1
- FROM instance
- WHERE instance.id = anomaly.instance_id AND instance.row_status != 'ARCHIVED'
- )
- `,
- args...,
- )
- if err != nil {
- return nil, FormatError(err)
- }
- defer rows.Close()
-
- // Iterate over result set and deserialize rows into anomalyRawList.
- var anomalyRawList []*anomalyRaw
- for rows.Next() {
- var anomalyRaw anomalyRaw
- databaseID := sql.NullInt32{}
- if err := rows.Scan(
- &anomalyRaw.ID,
- &anomalyRaw.CreatorID,
- &anomalyRaw.CreatedTs,
- &anomalyRaw.UpdaterID,
- &anomalyRaw.UpdatedTs,
- &anomalyRaw.InstanceID,
- &databaseID,
- &anomalyRaw.Type,
- &anomalyRaw.Payload,
- ); err != nil {
- return nil, FormatError(err)
- }
- if databaseID.Valid {
- value := int(databaseID.Int32)
- anomalyRaw.DatabaseID = &value
- }
- anomalyRaw.Severity = api.AnomalySeverityFromType(anomalyRaw.Type)
-
- anomalyRawList = append(anomalyRawList, &anomalyRaw)
- }
- if err := rows.Err(); err != nil {
- return nil, FormatError(err)
- }
-
- return anomalyRawList, nil
-}
-
-type anomalyPatch struct {
- ID int
-
- // Standard fields
- UpdaterID int
-
- // Domain specific fields
- Payload string
-}
-
-// patchAnomalyImpl patches an anomaly.
-func patchAnomalyImpl(ctx context.Context, tx *Tx, patch *anomalyPatch) (*anomalyRaw, error) {
- // Build UPDATE clause.
- if patch.Payload == "" {
- patch.Payload = "{}"
- }
- set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
- set, args = append(set, "payload = $2"), append(args, patch.Payload)
- args = append(args, patch.ID)
-
- // Execute update query with RETURNING.
- query := `
- UPDATE anomaly
- SET ` + strings.Join(set, ", ") + `
- WHERE id = $3
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, instance_id, database_id, type, payload
- `
- var anomalyRaw anomalyRaw
- var databaseID sql.NullInt32
- if err := tx.QueryRowContext(ctx, query, args...).Scan(
-
- &anomalyRaw.ID,
- &anomalyRaw.CreatorID,
- &anomalyRaw.CreatedTs,
- &anomalyRaw.UpdaterID,
- &anomalyRaw.UpdatedTs,
- &anomalyRaw.InstanceID,
- &databaseID,
- &anomalyRaw.Type,
- &anomalyRaw.Payload,
- ); err != nil {
- if err == sql.ErrNoRows {
- return nil, common.FormatDBErrorEmptyRowWithQuery(query)
- }
- return nil, FormatError(err)
- }
- if databaseID.Valid {
- value := int(databaseID.Int32)
- anomalyRaw.DatabaseID = &value
- }
- anomalyRaw.Severity = api.AnomalySeverityFromType(anomalyRaw.Type)
- return &anomalyRaw, nil
-}
-
-// archiveAnomalyImpl archives an anomaly by ID.
-func archiveAnomalyImpl(ctx context.Context, tx *Tx, archive *api.AnomalyArchive) error {
- if archive.InstanceID == nil && archive.DatabaseID == nil {
- return &common.Error{Code: common.Internal, Err: errors.Errorf("failed to close anomaly, should specify either instanceID or databaseID")}
- }
- if archive.InstanceID != nil && archive.DatabaseID != nil {
- return &common.Error{Code: common.Internal, Err: errors.Errorf("failed to close anomaly, should specify either instanceID or databaseID, but not both")}
- }
- // Remove row from database.
- if archive.InstanceID != nil {
- result, err := tx.ExecContext(ctx,
- `UPDATE anomaly SET row_status = $1 WHERE instance_id = $2 AND database_id IS NULL AND type = $3`,
- api.Archived,
- *archive.InstanceID,
- archive.Type,
- )
- if err != nil {
- return FormatError(err)
- }
-
- rows, _ := result.RowsAffected()
- if rows == 0 {
- return &common.Error{Code: common.NotFound, Err: errors.Errorf("anomaly not found instance: %d type: %s", *archive.InstanceID, archive.Type)}
- }
- } else if archive.DatabaseID != nil {
- result, err := tx.ExecContext(ctx,
- `UPDATE anomaly SET row_status = $1 WHERE database_id = $2 AND type = $3`,
- api.Archived,
- *archive.DatabaseID,
- archive.Type,
- )
- if err != nil {
- return FormatError(err)
- }
-
- rows, _ := result.RowsAffected()
- if rows == 0 {
- return &common.Error{Code: common.NotFound, Err: errors.Errorf("anomaly not found database: %d type: %s", *archive.DatabaseID, archive.Type)}
- }
- }
-
- return nil
-}
-
-// AnomalyMessage is the message of the anomaly.
-type AnomalyMessage struct {
- // InstanceUID is the unique identifier of the instance.
- InstanceUID int
- // DatabaseUID is the unique identifier of the database, it will be nil if the anomaly is instance level.
- DatabaseUID *int
- // Type is the type of the anomaly.
- Type api.AnomalyType
- // Payload is the payload of the anomaly.
- Payload string
-}
-
-// ListAnomalyMessage is the message to list anomalies.
-type ListAnomalyMessage struct {
- InstanceUID *int
- DatabaseUID *int
- Types []*api.AnomalyType
-}
-
-// ListAnomalyV2 lists anomalies, only return the normal ones.
-func (s *Store) ListAnomalyV2(ctx context.Context, list *ListAnomalyMessage) ([]*AnomalyMessage, error) {
- // Build where clause
- where, args := []string{"row_status = $1"}, []interface{}{api.Normal}
if v := list.InstanceUID; v != nil {
where, args = append(where, fmt.Sprintf("instance_id = $%d", len(args)+1)), append(args, *v)
}
@@ -473,13 +250,16 @@ func (s *Store) ListAnomalyV2(ctx context.Context, list *ListAnomalyMessage) ([]
if len(list.Types) > 0 {
var sub []string
for _, v := range list.Types {
- sub, args = append(sub, fmt.Sprintf("$%d", len(args)+1)), append(args, *v)
+ sub, args = append(sub, fmt.Sprintf("$%d", len(args)+1)), append(args, v)
}
where = append(where, fmt.Sprintf("type IN (%s)", strings.Join(sub, `,`)))
}
query := fmt.Sprintf(`
SELECT
+ id,
+ created_ts,
+ updated_ts,
instance_id,
database_id,
type,
@@ -492,12 +272,6 @@ func (s *Store) ListAnomalyV2(ctx context.Context, list *ListAnomalyMessage) ([]
))
`, strings.Join(where, " AND "))
- tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
- if err != nil {
- return nil, errors.Wrapf(err, "failed to begin transaction")
- }
- defer tx.Rollback()
-
rows, err := tx.QueryContext(ctx, query, args...)
if err != nil {
return nil, FormatError(err)
@@ -510,6 +284,9 @@ func (s *Store) ListAnomalyV2(ctx context.Context, list *ListAnomalyMessage) ([]
// DatabaseID field can be NULL in the PostgreSQL database, so we use sql.NullInt32 to represent it.
var databaseID sql.NullInt32
if err := rows.Scan(
+ &anomaly.ID,
+ &anomaly.CreatedTs,
+ &anomaly.UpdatedTs,
&anomaly.InstanceUID,
&databaseID,
&anomaly.Type,
@@ -527,9 +304,46 @@ func (s *Store) ListAnomalyV2(ctx context.Context, list *ListAnomalyMessage) ([]
return nil, FormatError(err)
}
- if err := tx.Commit(); err != nil {
- return nil, errors.Wrapf(err, "failed to commit transaction")
+ return anomalies, nil
+}
+
+// updateAnomalyV2 patches an anomaly.
+func updateAnomalyV2(ctx context.Context, tx *Tx, principalUID int, update *updateAnomalyMessage) (*AnomalyMessage, error) {
+ // Build UPDATE clause.
+ if update.Payload == "" {
+ update.Payload = "{}"
}
+ set, args := []string{"updater_id = $1"}, []interface{}{principalUID}
+ set, args = append(set, "payload = $2"), append(args, update.Payload)
+ args = append(args, update.ID)
- return anomalies, nil
+ // Execute update query with RETURNING.
+ query := `
+ UPDATE anomaly
+ SET ` + strings.Join(set, ", ") + `
+ WHERE id = $3
+ RETURNING id, created_ts, updated_ts, instance_id, database_id, type, payload
+ `
+ var anomaly AnomalyMessage
+ var databaseID sql.NullInt32
+ if err := tx.QueryRowContext(ctx, query, args...).Scan(
+ &anomaly.ID,
+ &anomaly.CreatedTs,
+ &anomaly.UpdatedTs,
+ &anomaly.InstanceUID,
+ &databaseID,
+ &anomaly.Type,
+ &anomaly.Payload,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
+ }
+ return nil, FormatError(err)
+ }
+ if databaseID.Valid {
+ value := int(databaseID.Int32)
+ anomaly.DatabaseUID = &value
+ }
+
+ return &anomaly, nil
}
diff --git a/frontend/src/components/AnomalyTable.vue b/frontend/src/components/AnomalyTable.vue
index c7f7a5f3f2..b2509caafc 100644
--- a/frontend/src/components/AnomalyTable.vue
+++ b/frontend/src/components/AnomalyTable.vue
@@ -88,7 +88,11 @@ import {
AnomalyType,
} from "../types";
import { databaseSlug, humanizeTs, instanceSlug } from "../utils";
-import { useEnvironmentStore } from "@/store";
+import {
+ useDatabaseStore,
+ useEnvironmentStore,
+ useInstanceStore,
+} from "@/store";
type Action = {
onClick: () => void;
@@ -165,11 +169,11 @@ export default defineComponent({
case "bb.anomaly.instance.migration-schema":
return "Please create migration schema on the instance first.";
case "bb.anomaly.database.backup.policy-violation": {
- const environment = useEnvironmentStore().getEnvironmentById(
- anomaly.instance.environment.id
- );
const payload =
anomaly.payload as AnomalyDatabaseBackupPolicyViolationPayload;
+ const environment = useEnvironmentStore().getEnvironmentById(
+ payload.environmentId
+ );
return `'${environment.name}' environment requires ${payload.expectedSchedule} auto-backup.`;
}
case "bb.anomaly.database.backup.missing": {
@@ -198,37 +202,48 @@ export default defineComponent({
const action = (anomaly: Anomaly): Action => {
switch (anomaly.type) {
- case "bb.anomaly.instance.connection":
+ case "bb.anomaly.instance.connection": {
+ const instance = useInstanceStore().getInstanceById(
+ anomaly.instanceId!
+ );
return {
onClick: () => {
router.push({
name: "workspace.instance.detail",
params: {
- instanceSlug: instanceSlug(anomaly.instance),
+ instanceSlug: instanceSlug(instance),
},
});
},
title: t("anomaly.action.check-instance"),
};
- case "bb.anomaly.instance.migration-schema":
+ }
+ case "bb.anomaly.instance.migration-schema": {
+ const instance = useInstanceStore().getInstanceById(
+ anomaly.instanceId!
+ );
return {
onClick: () => {
router.push({
name: "workspace.instance.detail",
params: {
- instanceSlug: instanceSlug(anomaly.instance),
+ instanceSlug: instanceSlug(instance),
},
});
},
title: t("anomaly.action.check-instance"),
};
+ }
case "bb.anomaly.database.backup.policy-violation": {
+ const database = useDatabaseStore().getDatabaseById(
+ anomaly.databaseId!
+ );
return {
onClick: () => {
router.push({
name: "workspace.database.detail",
params: {
- databaseSlug: databaseSlug(anomaly.database!),
+ databaseSlug: databaseSlug(database),
},
hash: "#backup-and-restore",
});
@@ -236,31 +251,39 @@ export default defineComponent({
title: t("anomaly.action.configure-backup"),
};
}
- case "bb.anomaly.database.backup.missing":
+ case "bb.anomaly.database.backup.missing": {
+ const database = useDatabaseStore().getDatabaseById(
+ anomaly.databaseId!
+ );
return {
onClick: () => {
router.push({
name: "workspace.database.detail",
params: {
- databaseSlug: databaseSlug(anomaly.database!),
+ databaseSlug: databaseSlug(database),
},
hash: "#backup-and-restore",
});
},
title: t("anomaly.action.view-backup"),
};
- case "bb.anomaly.database.connection":
+ }
+ case "bb.anomaly.database.connection": {
+ const instance = useInstanceStore().getInstanceById(
+ anomaly.instanceId!
+ );
return {
onClick: () => {
router.push({
name: "workspace.instance.detail",
params: {
- instanceSlug: instanceSlug(anomaly.instance),
+ instanceSlug: instanceSlug(instance),
},
});
},
title: t("anomaly.action.check-instance"),
};
+ }
case "bb.anomaly.database.schema.drift":
return {
onClick: () => {