Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Backup plan: implement runtime enforcement in database create actions.
spinningbotb687c27e0d6a
diff --git a/api/backup.go b/api/backup.goindex 371dc90a6e..13ce6770a3 100644--- a/api/backup.go+++ b/api/backup.go@@ -2,6 +2,7 @@ package apiimport ("context"+ "database/sql""encoding/json")@@ -203,5 +204,6 @@ type BackupService interface {PatchBackup(ctx context.Context, patch *BackupPatch) (*Backup, error)FindBackupSetting(ctx context.Context, find *BackupSettingFind) (*BackupSetting, error)UpsertBackupSetting(ctx context.Context, upsert *BackupSettingUpsert) (*BackupSetting, error)+ UpsertBackupSettingTx(ctx context.Context, tx *sql.Tx, upsert *BackupSettingUpsert) (*BackupSetting, error)FindBackupSettingsMatch(ctx context.Context, match *BackupSettingsMatch) ([]*BackupSetting, error)}diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.goindex 8d37954b8f..b4e74a0d5a 100644--- a/bin/server/cmd/root.go+++ b/bin/server/cmd/root.go@@ -287,13 +287,13 @@ 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.DatabaseService = store.NewDatabaseService(m.l, db, s.CacheService, s.PolicyService)+ s.BackupService = store.NewBackupService(m.l, db)+ 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)s.TableService = store.NewTableService(m.l, db)s.ColumnService = store.NewColumnService(m.l, db)s.IndexService = store.NewIndexService(m.l, db)- s.BackupService = store.NewBackupService(m.l, db)s.IssueService = store.NewIssueService(m.l, db, s.CacheService)s.IssueSubscriberService = store.NewIssueSubscriberService(m.l, db)s.PipelineService = store.NewPipelineService(m.l, db, s.CacheService)diff --git a/store/backup.go b/store/backup.goindex d89136c854..08ed45a214 100644--- a/store/backup.go+++ b/store/backup.go@@ -2,6 +2,7 @@ package storeimport ("context"+ "database/sql""fmt""strings"@@ -364,7 +365,7 @@ func (s *BackupService) UpsertBackupSetting(ctx context.Context, upsert *api.Bac}defer tx.Rollback()- backup, err := s.upsertBackupSetting(ctx, tx, upsert)+ backup, err := s.UpsertBackupSettingTx(ctx, tx.Tx, upsert)if err != nil {return nil, err}@@ -376,8 +377,8 @@ func (s *BackupService) UpsertBackupSetting(ctx context.Context, upsert *api.Bacreturn backup, nil}-// upsertBackupSetting updates an existing backup setting.-func (s *BackupService) upsertBackupSetting(ctx context.Context, tx *Tx, upsert *api.BackupSettingUpsert) (*api.BackupSetting, error) {+// UpsertBackupSettingTx updates an existing backup setting.+func (s *BackupService) UpsertBackupSettingTx(ctx context.Context, tx *sql.Tx, upsert *api.BackupSettingUpsert) (*api.BackupSetting, error) {// Upsert row into backup_setting.row, err := tx.QueryContext(ctx, `INSERT INTO backup_setting (diff --git a/store/database.go b/store/database.goindex 75e19f860f..d614a65448 100644--- a/store/database.go+++ b/store/database.go@@ -4,6 +4,7 @@ import ("context""database/sql""fmt"+ "math/rand""strings""github.com/bytebase/bytebase/api"@@ -22,15 +23,17 @@ type DatabaseService struct {cache api.CacheServicepolicyService api.PolicyService+ backupService api.BackupService}// NewDatabaseService returns a new instance of DatabaseService.-func NewDatabaseService(logger *zap.Logger, db *DB, cache api.CacheService, policyService api.PolicyService) *DatabaseService {+func NewDatabaseService(logger *zap.Logger, db *DB, cache api.CacheService, policyService api.PolicyService, backupService api.BackupService) *DatabaseService {return &DatabaseService{l: logger,db: db,cache: cache,policyService: policyService,+ backupService: backupService,}}@@ -55,10 +58,6 @@ 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) {- database, err := s.createDatabase(ctx, tx, create)- if err != nil {- return nil, err- }pType := api.PolicyTypeBackupPlanpolicy, err := s.policyService.FindPolicy(ctx, &api.PolicyFind{EnvironmentId: &create.EnvironmentId,@@ -68,8 +67,29 @@ func (s *DatabaseService) CreateDatabaseTx(ctx context.Context, tx *sql.Tx, creareturn nil, err}backupPlanPolicy := api.BackupPlanPolicyValue(policy.Payload)++ database, err := s.createDatabase(ctx, tx, create)+ if err != nil {+ return nil, err+ }++ // Enable automatic backup setting based on backup plan policy.if backupPlanPolicy != api.BackupPlanPolicyValueNever {- // TODO(spinningbot): enforce automatic backup.+ backupSettingUpsert := &api.BackupSettingUpsert{+ UpdaterId: api.SYSTEM_BOT_ID,+ DatabaseId: database.ID,+ Enabled: true,+ Hour: rand.Intn(24),+ }+ switch backupPlanPolicy {+ case api.BackupPlanPolicyValueDaily:+ backupSettingUpsert.DayOfWeek = -1+ case api.BackupPlanPolicyValueWeekly:+ backupSettingUpsert.DayOfWeek = rand.Intn(7)+ }+ if _, err := s.backupService.UpsertBackupSettingTx(ctx, tx, backupSettingUpsert); err != nil {+ return nil, err+ }}if err := s.cache.UpsertCache(api.DatabaseCache, database.ID, database); err != nil {