Skip to content

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

feat(backup): change schema for retention policy (#1876)

Yilong Li9980ea98d83a
diff --git a/api/backup.go b/api/backup.go
index 8715768939..b75e424ec2 100644
--- a/api/backup.go
+++ b/api/backup.go
@@ -181,9 +181,13 @@ type BackupSetting struct {
Database *Database
// Domain specific fields
- Enabled bool `jsonapi:"attr,enabled"`
- Hour int `jsonapi:"attr,hour"`
- DayOfWeek int `jsonapi:"attr,dayOfWeek"`
+ Enabled bool `jsonapi:"attr,enabled"`
+ // Schedule related fields
+ Hour int `jsonapi:"attr,hour"`
+ DayOfWeek int `jsonapi:"attr,dayOfWeek"`
+ // RetentionPeriodTs is the period that backup data is kept for the database.
+ // 0 means unset and we do not delete data.
+ RetentionPeriodTs int `jsonapi:"attr,retentionPeriodTs"`
// HookURL is the callback url to be requested (using HTTP GET) after a successful backup.
HookURL string `jsonapi:"attr,hookUrl"`
}
@@ -211,10 +215,11 @@ type BackupSettingUpsert struct {
EnvironmentID int
// Domain specific fields
- Enabled bool `jsonapi:"attr,enabled"`
- Hour int `jsonapi:"attr,hour"`
- DayOfWeek int `jsonapi:"attr,dayOfWeek"`
- HookURL string `jsonapi:"attr,hookUrl"`
+ Enabled bool `jsonapi:"attr,enabled"`
+ Hour int `jsonapi:"attr,hour"`
+ DayOfWeek int `jsonapi:"attr,dayOfWeek"`
+ RetentionPeriodTs int `jsonapi:"attr,retentionPeriodTs"`
+ HookURL string `jsonapi:"attr,hookUrl"`
}
// BackupSettingsMatch is the message to find backup settings matching the conditions.
diff --git a/store/backup.go b/store/backup.go
index 39c12fcb4f..8fad55acf4 100644
--- a/store/backup.go
+++ b/store/backup.go
@@ -80,9 +80,10 @@ type backupSettingRaw struct {
DatabaseID int
// Domain specific fields
- Enabled bool
- Hour int
- DayOfWeek int
+ Enabled bool
+ Hour int
+ DayOfWeek int
+ RetentionPeriodTs int
// HookURL is the callback url to be requested (using HTTP GET) after a successful backup.
HookURL string
}
@@ -647,6 +648,58 @@ func (s *Store) findBackupSettingImpl(ctx context.Context, tx *sql.Tx, find *api
// upsertBackupSettingImpl updates an existing backup setting.
func (s *Store) upsertBackupSettingImpl(ctx context.Context, tx *sql.Tx, upsert *api.BackupSettingUpsert) (*backupSettingRaw, error) {
// Upsert row into backup_setting.
+ if s.db.mode == common.ReleaseModeDev {
+ query := `
+ INSERT INTO backup_setting (
+ creator_id,
+ updater_id,
+ database_id,
+ enabled,
+ hour,
+ day_of_week,
+ retention_period_ts,
+ hook_url
+ )
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
+ ON CONFLICT(database_id) DO UPDATE SET
+ enabled = EXCLUDED.enabled,
+ hour = EXCLUDED.hour,
+ day_of_week = EXCLUDED.day_of_week,
+ retention_period_ts = EXCLUDED.retention_period_ts,
+ hook_url = EXCLUDED.hook_url
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, database_id, enabled, hour, day_of_week, retention_period_ts, hook_url
+ `
+ var backupSettingRaw backupSettingRaw
+ if err := tx.QueryRowContext(ctx, query,
+ upsert.UpdaterID,
+ upsert.UpdaterID,
+ upsert.DatabaseID,
+ upsert.Enabled,
+ upsert.Hour,
+ upsert.DayOfWeek,
+ upsert.RetentionPeriodTs,
+ upsert.HookURL,
+ ).Scan(
+ &backupSettingRaw.ID,
+ &backupSettingRaw.CreatorID,
+ &backupSettingRaw.CreatedTs,
+ &backupSettingRaw.UpdaterID,
+ &backupSettingRaw.UpdatedTs,
+ &backupSettingRaw.DatabaseID,
+ &backupSettingRaw.Enabled,
+ &backupSettingRaw.Hour,
+ &backupSettingRaw.DayOfWeek,
+ &backupSettingRaw.RetentionPeriodTs,
+ &backupSettingRaw.HookURL,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
+ }
+ return nil, FormatError(err)
+ }
+ return &backupSettingRaw, nil
+ }
+
query := `
INSERT INTO backup_setting (
creator_id,
diff --git a/store/database.go b/store/database.go
index 32293df466..cac9f3a26e 100644
--- a/store/database.go
+++ b/store/database.go
@@ -325,6 +325,16 @@ func (s *Store) createDatabaseRawTx(ctx context.Context, tx *sql.Tx, create *api
Hour: rand.Intn(24),
HookURL: "",
}
+ if s.db.mode == common.ReleaseModeDev {
+ backupSettingUpsert = &api.BackupSettingUpsert{
+ UpdaterID: api.SystemBotID,
+ DatabaseID: databaseRaw.ID,
+ Enabled: true,
+ Hour: rand.Intn(24),
+ RetentionPeriodTs: 7 * 24 * 3600,
+ HookURL: "",
+ }
+ }
switch backupPlanPolicy.Schedule {
case api.BackupPlanPolicyScheduleDaily:
backupSettingUpsert.DayOfWeek = -1
diff --git a/store/migration/dev/20220714000000__backup_retention.sql b/store/migration/dev/20220714000000__backup_retention.sql
new file mode 100644
index 0000000000..dcbf8db63b
--- /dev/null
+++ b/store/migration/dev/20220714000000__backup_retention.sql
@@ -0,0 +1 @@
+ALTER TABLE backup_setting ADD retention_period_ts INTEGER NOT NULL DEFAULT 0 CHECK (retention_period_ts >= 0);
diff --git a/store/migration/dev/LATEST.sql b/store/migration/dev/LATEST.sql
index 9ae98a140c..58b235db47 100644
--- a/store/migration/dev/LATEST.sql
+++ b/store/migration/dev/LATEST.sql
@@ -569,10 +569,13 @@ CREATE TABLE backup_setting (
updater_id INTEGER NOT NULL REFERENCES principal (id),
updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
database_id INTEGER NOT NULL REFERENCES db (id),
+ -- enable automatic backup schedule.
enabled BOOLEAN NOT NULL,
hour INTEGER NOT NULL CHECK (hour >= 0 AND hour <= 23),
-- day_of_week can be -1 which is wildcard (daily automatic backup).
day_of_week INTEGER NOT NULL CHECK (day_of_week >= -1 AND day_of_week <= 6),
+ -- retention_period_ts == 0 means unset retention period and we do not delete any data.
+ retention_period_ts INTEGER NOT NULL DEFAULT 0 CHECK (retention_period_ts >= 0),
-- hook_url is the callback url to be requested after a successful backup.
hook_url TEXT NOT NULL
);