Skip to content

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

Backup plan policy: validate backup setting mutation.

spinningbot313c500c8bf1
diff --git a/api/backup.go b/api/backup.go
index 13ce6770a3..1ce0814138 100644
--- a/api/backup.go
+++ b/api/backup.go
@@ -182,7 +182,8 @@ type BackupSettingUpsert struct {
UpdaterId int
// Related fields
- DatabaseId int `jsonapi:"attr,databaseId"`
+ DatabaseId int `jsonapi:"attr,databaseId"`
+ EnvironmentId int
// Domain specific fields
Enabled bool `jsonapi:"attr,enabled"`
diff --git a/api/policy.go b/api/policy.go
index 6f62abff00..7e0020e46e 100644
--- a/api/policy.go
+++ b/api/policy.go
@@ -26,8 +26,8 @@ const (
// ApprovalPolicyValueManualAlways is MANUAL_APPROVAL_ALWAYS approval policy value.
ApprovalPolicyValueManualAlways ApprovalPolicyValue = "MANUAL_APPROVAL_ALWAYS"
- // BackupPlanPolicyScheduleNever is NEVER backup plan policy value.
- BackupPlanPolicyScheduleNever BackupPlanPolicySchedule = "NEVER"
+ // BackupPlanPolicyScheduleUnset is NEVER backup plan policy value.
+ BackupPlanPolicyScheduleUnset BackupPlanPolicySchedule = "UNSET"
// BackupPlanPolicyScheduleDaily is DAILY backup plan policy value.
BackupPlanPolicyScheduleDaily BackupPlanPolicySchedule = "DAILY"
// BackupPlanPolicyScheduleWeekly is WEEKLY backup plan policy value.
@@ -96,6 +96,7 @@ type PolicyUpsert struct {
type PolicyService interface {
FindPolicy(ctx context.Context, find *PolicyFind) (*Policy, error)
UpsertPolicy(ctx context.Context, upsert *PolicyUpsert) (*Policy, error)
+ GetBackupPlanPolicy(ctx context.Context, environmentID int) (*BackupPlanPolicy, error)
}
// BackupPlanPolicy is the policy configuration for backup plan.
@@ -140,7 +141,7 @@ func ValidatePolicy(pType PolicyType, payload string) error {
if err != nil {
return err
}
- if bp.Schedule != BackupPlanPolicyScheduleNever && bp.Schedule != BackupPlanPolicyScheduleDaily && bp.Schedule != BackupPlanPolicyScheduleWeekly {
+ if bp.Schedule != BackupPlanPolicyScheduleUnset && bp.Schedule != BackupPlanPolicyScheduleDaily && bp.Schedule != BackupPlanPolicyScheduleWeekly {
return fmt.Errorf("invalid backup plan policy schedule: %q", bp.Schedule)
}
}
@@ -155,7 +156,7 @@ func GetDefaultPolicy(pType PolicyType) (string, error) {
return "", nil
case PolicyTypeBackupPlan:
return BackupPlanPolicy{
- Schedule: BackupPlanPolicyScheduleNever,
+ Schedule: BackupPlanPolicyScheduleUnset,
}.String()
}
return "", nil
diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.go
index b4e74a0d5a..8e6c38fd30 100644
--- a/bin/server/cmd/root.go
+++ b/bin/server/cmd/root.go
@@ -287,7 +287,7 @@ func (m *main) Run() error {
s.ProjectWebhookService = store.NewProjectWebhookService(m.l, db)
s.EnvironmentService = store.NewEnvironmentService(m.l, db, s.CacheService)
s.DataSourceService = store.NewDataSourceService(m.l, db)
- s.BackupService = store.NewBackupService(m.l, db)
+ s.BackupService = store.NewBackupService(m.l, db, s.PolicyService)
s.DatabaseService = store.NewDatabaseService(m.l, db, s.CacheService, s.PolicyService, s.BackupService)
s.InstanceService = store.NewInstanceService(m.l, db, s.CacheService, s.DatabaseService, s.DataSourceService)
s.InstanceUserService = store.NewInstanceUserService(m.l, db)
diff --git a/server/database.go b/server/database.go
index 3a0a6a248b..b84214e6cf 100644
--- a/server/database.go
+++ b/server/database.go
@@ -482,13 +482,14 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
databaseFind := &api.DatabaseFind{
ID: &id,
}
- _, err = s.ComposeDatabaseByFind(context.Background(), databaseFind)
+ db, err := s.ComposeDatabaseByFind(context.Background(), databaseFind)
if err != nil {
if common.ErrorCode(err) == common.ENOTFOUND {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", id))
}
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch database ID: %v", id)).SetInternal(err)
}
+ backupSettingUpsert.EnvironmentId = db.Instance.Environment.ID
backupSetting, err := s.BackupService.UpsertBackupSetting(context.Background(), backupSettingUpsert)
if err != nil {
diff --git a/store/backup.go b/store/backup.go
index 08ed45a214..e082885b1a 100644
--- a/store/backup.go
+++ b/store/backup.go
@@ -17,13 +17,14 @@ var (
// BackupService represents a service for managing backup.
type BackupService struct {
- l *zap.Logger
- db *DB
+ l *zap.Logger
+ db *DB
+ policyService api.PolicyService
}
// NewBackupService returns a new instance of BackupService.
-func NewBackupService(logger *zap.Logger, db *DB) *BackupService {
- return &BackupService{l: logger, db: db}
+func NewBackupService(logger *zap.Logger, db *DB, policyService api.PolicyService) *BackupService {
+ return &BackupService{l: logger, db: db, policyService: policyService}
}
// CreateBackup creates a new backup.
@@ -359,6 +360,27 @@ func (s *BackupService) findBackupSetting(ctx context.Context, tx *Tx, find *api
// UpsertBackupSetting sets the backup settings for a database.
func (s *BackupService) UpsertBackupSetting(ctx context.Context, upsert *api.BackupSettingUpsert) (*api.BackupSetting, error) {
+ backupPlanPolicy, err := s.policyService.GetBackupPlanPolicy(ctx, upsert.EnvironmentId)
+ if err != nil {
+ return nil, err
+ }
+ // Backup plan policy check for backup setting mutation.
+ if backupPlanPolicy.Schedule != api.BackupPlanPolicyScheduleUnset {
+ if !upsert.Enabled {
+ return nil, &common.Error{Code: common.EINVALID, Message: fmt.Sprintf("backup setting should not be disabled for backup plan policy schedule %q", backupPlanPolicy.Schedule)}
+ }
+ switch backupPlanPolicy.Schedule {
+ case api.BackupPlanPolicyScheduleDaily:
+ if upsert.DayOfWeek != -1 {
+ return nil, &common.Error{Code: common.EINVALID, Message: fmt.Sprintf("backup setting DayOfWeek should be unset for backup plan policy schedule %q", backupPlanPolicy.Schedule)}
+ }
+ case api.BackupPlanPolicyScheduleWeekly:
+ if upsert.DayOfWeek == -1 {
+ return nil, &common.Error{Code: common.EINVALID, Message: fmt.Sprintf("backup setting DayOfWeek should be set for backup plan policy schedule %q", backupPlanPolicy.Schedule)}
+ }
+ }
+ }
+
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
diff --git a/store/database.go b/store/database.go
index c6549de129..d267a84cfb 100644
--- a/store/database.go
+++ b/store/database.go
@@ -58,15 +58,7 @@ func (s *DatabaseService) CreateDatabase(ctx context.Context, create *api.Databa
}
func (s *DatabaseService) CreateDatabaseTx(ctx context.Context, tx *sql.Tx, create *api.DatabaseCreate) (*api.Database, error) {
- pType := api.PolicyTypeBackupPlan
- policy, err := s.policyService.FindPolicy(ctx, &api.PolicyFind{
- EnvironmentId: &create.EnvironmentId,
- Type: &pType,
- })
- if err != nil {
- return nil, err
- }
- backupPlanPolicy, err := api.UnmarshalBackupPlanPolicy(policy.Payload)
+ backupPlanPolicy, err := s.policyService.GetBackupPlanPolicy(ctx, create.EnvironmentId)
if err != nil {
return nil, err
}
@@ -77,7 +69,7 @@ func (s *DatabaseService) CreateDatabaseTx(ctx context.Context, tx *sql.Tx, crea
}
// Enable automatic backup setting based on backup plan policy.
- if backupPlanPolicy.Schedule != api.BackupPlanPolicyScheduleNever {
+ if backupPlanPolicy.Schedule != api.BackupPlanPolicyScheduleUnset {
backupSettingUpsert := &api.BackupSettingUpsert{
UpdaterId: api.SYSTEM_BOT_ID,
DatabaseId: database.ID,
diff --git a/store/policy.go b/store/policy.go
index dd534be841..10ef0b0763 100644
--- a/store/policy.go
+++ b/store/policy.go
@@ -199,3 +199,16 @@ func (s *PolicyService) upsertPolicy(ctx context.Context, tx *Tx, upsert *api.Po
return &policy, nil
}
+
+// GetBackupPlanPolicy will get the backup plan policy for an environment.
+func (s *PolicyService) GetBackupPlanPolicy(ctx context.Context, environmentID int) (*api.BackupPlanPolicy, error) {
+ pType := api.PolicyTypeBackupPlan
+ policy, err := s.FindPolicy(ctx, &api.PolicyFind{
+ EnvironmentId: &environmentID,
+ Type: &pType,
+ })
+ if err != nil {
+ return nil, err
+ }
+ return api.UnmarshalBackupPlanPolicy(policy.Payload)
+}