Files
wehub-resource-sync 75f3dd141c
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
CI and Release / lint-frontend (push) Has been cancelled
CI and Release / dockerfile-scan (push) Has been cancelled
CI and Release / test-frontend (push) Has been cancelled
CI and Release / lint-verification-agent (push) Has been cancelled
CI and Release / test-verification-agent (push) Has been cancelled
CI and Release / e2e-verification-agent (push) Has been cancelled
CI and Release / test-backend (push) Has been cancelled
CI and Release / build-dev-image (push) Has been cancelled
CI and Release / push-dev-image (push) Has been cancelled
CI and Release / build-image (push) Has been cancelled
CI and Release / build-verification-image (push) Has been cancelled
CI and Release / determine-version (push) Has been cancelled
CI and Release / push-image (push) Has been cancelled
CI and Release / push-verification-image (12) (push) Has been cancelled
CI and Release / lint-backend (push) Has been cancelled
CI and Release / push-verification-image (17) (push) Has been cancelled
CI and Release / push-verification-image (18) (push) Has been cancelled
CI and Release / release (push) Has been cancelled
CI and Release / publish-helm-chart (push) Has been cancelled
CI and Release / push-verification-image (13) (push) Has been cancelled
CI and Release / push-verification-image (14) (push) Has been cancelled
CI and Release / push-verification-image (15) (push) Has been cancelled
CI and Release / push-verification-image (16) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:11:07 +08:00

97 lines
3.1 KiB
Go

package verification_config
import (
"fmt"
"slices"
"strings"
"time"
"github.com/google/uuid"
"gorm.io/gorm"
"databasus-backend/internal/features/intervals"
)
type BackupVerificationConfig struct {
DatabaseID uuid.UUID `json:"databaseId" gorm:"column:database_id;type:uuid;primaryKey;not null"`
IsScheduledVerificationEnabled bool `json:"isScheduledVerificationEnabled" gorm:"column:is_scheduled_verification_enabled;type:boolean;not null;default:false"`
ScheduleType VerificationScheduleType `json:"scheduleType" gorm:"column:schedule_type;type:text;not null;default:'AFTER_BACKUP'"`
VerificationInterval intervals.Interval `json:"verificationInterval" gorm:"embedded"`
SendNotificationsOn []VerificationNotificationType `json:"sendNotificationsOn" gorm:"-"`
SendNotificationsOnString string `json:"-" gorm:"column:send_notifications_on;type:text;not null;default:''"`
CreatedAt time.Time `json:"createdAt" gorm:"column:created_at"`
UpdatedAt time.Time `json:"updatedAt" gorm:"column:updated_at"`
}
func (BackupVerificationConfig) TableName() string {
return "backup_verification_configs"
}
func (c *BackupVerificationConfig) BeforeSave(tx *gorm.DB) error {
if len(c.SendNotificationsOn) > 0 {
notificationTypes := make([]string, len(c.SendNotificationsOn))
for i, notificationType := range c.SendNotificationsOn {
notificationTypes[i] = string(notificationType)
}
c.SendNotificationsOnString = strings.Join(notificationTypes, ",")
} else {
c.SendNotificationsOnString = ""
}
return nil
}
func (c *BackupVerificationConfig) AfterFind(tx *gorm.DB) error {
if c.SendNotificationsOnString != "" {
notificationTypes := strings.Split(c.SendNotificationsOnString, ",")
c.SendNotificationsOn = make([]VerificationNotificationType, len(notificationTypes))
for i, notificationType := range notificationTypes {
c.SendNotificationsOn[i] = VerificationNotificationType(notificationType)
}
} else {
c.SendNotificationsOn = []VerificationNotificationType{}
}
return nil
}
func (c *BackupVerificationConfig) Validate() error {
if c.ScheduleType != VerificationScheduleInterval && c.ScheduleType != VerificationScheduleAfterBackup {
return fmt.Errorf("invalid verification schedule type: %q", c.ScheduleType)
}
if c.IsScheduledVerificationEnabled && c.ScheduleType == VerificationScheduleInterval {
if err := c.VerificationInterval.Validate(); err != nil {
return fmt.Errorf("verification interval: %w", err)
}
}
for _, n := range c.SendNotificationsOn {
switch n {
case NotificationVerificationSuccess,
NotificationVerificationFailed:
default:
return fmt.Errorf("invalid verification notification type: %q", n)
}
}
return nil
}
func (c *BackupVerificationConfig) Copy(newDatabaseID uuid.UUID) *BackupVerificationConfig {
return &BackupVerificationConfig{
DatabaseID: newDatabaseID,
IsScheduledVerificationEnabled: c.IsScheduledVerificationEnabled,
ScheduleType: c.ScheduleType,
VerificationInterval: c.VerificationInterval.Copy(),
SendNotificationsOn: slices.Clone(c.SendNotificationsOn),
}
}