Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

FIX (backups): Keep the physical backup config valid when the database backup type changes

Rostislav Dugin52bfb3149f94
diff --git a/backend/internal/features/backups/backups/backuping/physical/cancellation_listener.go b/backend/internal/features/backups/backups/backuping/physical/cancellation_listener.go
new file mode 100644
index 00000000..c88ba9de
--- /dev/null
+++ b/backend/internal/features/backups/backups/backuping/physical/cancellation_listener.go
@@ -0,0 +1,51 @@
+package backuping_physical
+
+import (
+ "context"
+ "log/slog"
+
+ "github.com/google/uuid"
+
+ physical_repositories "databasus-backend/internal/features/backups/backups/core/physical/repositories"
+ tasks_cancellation "databasus-backend/internal/features/tasks/cancellation"
+)
+
+type PhysicalBackupCancellationListener struct {
+ canceller *PhysicalBackupCanceller
+ walStreamerRepo *physical_repositories.PhysicalWalStreamerRepository
+ taskCancelManager *tasks_cancellation.TaskCancelManager
+ logger *slog.Logger
+}
+
+func (l *PhysicalBackupCancellationListener) OnBackupsDisabled(ctx context.Context, databaseID uuid.UUID) {
+ l.canceller.CancelInFlightForDatabase(databaseID)
+ l.cancelStreamingAndDeleteStreamerRow(ctx, databaseID)
+}
+
+func (l *PhysicalBackupCancellationListener) OnWalStreamingDisabled(ctx context.Context, databaseID uuid.UUID) {
+ l.cancelStreamingAndDeleteStreamerRow(ctx, databaseID)
+}
+
+func (l *PhysicalBackupCancellationListener) OnBeforeDatabaseRemove(ctx context.Context, databaseID uuid.UUID) error {
+ l.canceller.CancelInFlightForDatabase(databaseID)
+ l.cancelStreamingAndDeleteStreamerRow(ctx, databaseID)
+
+ return nil
+}
+
+// Deleting the row first would leave an orphaned pg_receivewal on the slot. The
+// slot itself stays: re-enabling backups or WAL streaming reuses it.
+func (l *PhysicalBackupCancellationListener) cancelStreamingAndDeleteStreamerRow(
+ ctx context.Context,
+ databaseID uuid.UUID,
+) {
+ logger := l.logger.With("database_id", databaseID)
+
+ if err := l.taskCancelManager.CancelTask(databaseID); err != nil {
+ logger.ErrorContext(ctx, "failed to cancel wal streamer task", "error", err)
+ }
+
+ if err := l.walStreamerRepo.DeleteByDatabaseID(databaseID); err != nil {
+ logger.ErrorContext(ctx, "failed to delete wal streamer row", "error", err)
+ }
+}
diff --git a/backend/internal/features/backups/backups/backuping/physical/config_change_listener_test.go b/backend/internal/features/backups/backups/backuping/physical/cancellation_listener_test.go
similarity index 68%
rename from backend/internal/features/backups/backups/backuping/physical/config_change_listener_test.go
rename to backend/internal/features/backups/backups/backuping/physical/cancellation_listener_test.go
index 6d66aeab..faa7350c 100644
--- a/backend/internal/features/backups/backups/backuping/physical/config_change_listener_test.go
+++ b/backend/internal/features/backups/backups/backuping/physical/cancellation_listener_test.go
@@ -9,8 +9,6 @@ import (
physical_enums "databasus-backend/internal/features/backups/backups/core/physical/enums"
physical_repositories "databasus-backend/internal/features/backups/backups/core/physical/repositories"
- backups_config_physical "databasus-backend/internal/features/backups/config/physical"
- postgresql_physical "databasus-backend/internal/features/databases/databases/postgresql/physical"
tasks_cancellation "databasus-backend/internal/features/tasks/cancellation"
"databasus-backend/internal/storage"
"databasus-backend/internal/util/logger"
@@ -44,27 +42,12 @@ func seedClaimAndStreamer(t *testing.T, databaseID uuid.UUID) {
require.NoError(t, physical_repositories.GetWalStreamerRepository().Claim(databaseID))
}
-func configWithBackupType(
- databaseID uuid.UUID,
- enabled bool,
- backupType postgresql_physical.BackupType,
-) *backups_config_physical.PhysicalBackupConfig {
- return &backups_config_physical.PhysicalBackupConfig{
- DatabaseID: databaseID,
- IsBackupsEnabled: enabled,
- PostgresqlPhysical: &postgresql_physical.PostgresqlPhysicalDatabase{BackupType: backupType},
- }
-}
-
-func Test_OnBackupConfigChanged_WhenBackupsDisabled_CancelsInFlightAndDeletesStreamer(t *testing.T) {
+func Test_OnBackupsDisabled_CancelsInFlightAndDeletesStreamer(t *testing.T) {
prereqs := seedBackupPrereqs(t)
seedClaimAndStreamer(t, prereqs.DB.ID)
listener := newTestCancellationListener()
- oldConfig := configWithBackupType(prereqs.DB.ID, true, postgresql_physical.BackupTypeFullIncrementalAndWalStream)
- newConfig := configWithBackupType(prereqs.DB.ID, false, postgresql_physical.BackupTypeFullIncrementalAndWalStream)
-
- listener.OnBackupConfigChanged(oldConfig, newConfig)
+ listener.OnBackupsDisabled(t.Context(), prereqs.DB.ID)
claim, _ := physical_repositories.GetInFlightBackupRepository().FindByDatabaseID(prereqs.DB.ID)
assert.Nil(t, claim, "disabling backups must cancel + release the in-flight claim")
@@ -73,15 +56,12 @@ func Test_OnBackupConfigChanged_WhenBackupsDisabled_CancelsInFlightAndDeletesStr
assert.Nil(t, streamer, "disabling backups must delete the streamer row")
}
-func Test_OnBackupConfigChanged_WhenDemotedFromWalStream_DeletesStreamerKeepsInFlight(t *testing.T) {
+func Test_OnWalStreamingDisabled_DeletesStreamerKeepsInFlight(t *testing.T) {
prereqs := seedBackupPrereqs(t)
seedClaimAndStreamer(t, prereqs.DB.ID)
listener := newTestCancellationListener()
- oldConfig := configWithBackupType(prereqs.DB.ID, true, postgresql_physical.BackupTypeFullIncrementalAndWalStream)
- newConfig := configWithBackupType(prereqs.DB.ID, true, postgresql_physical.BackupTypeFullAndIncremental)
-
- listener.OnBackupConfigChanged(oldConfig, newConfig)
+ listener.OnWalStreamingDisabled(t.Context(), prereqs.DB.ID)
claim, _ := physical_repositories.GetInFlightBackupRepository().FindByDatabaseID(prereqs.DB.ID)
assert.NotNil(t, claim, "demoting BackupType must leave in-flight FULL/INCR running")
diff --git a/backend/internal/features/backups/backups/backuping/physical/config_change_listener.go b/backend/internal/features/backups/backups/backuping/physical/config_change_listener.go
deleted file mode 100644
index ac91241f..00000000
--- a/backend/internal/features/backups/backups/backuping/physical/config_change_listener.go
+++ /dev/null
@@ -1,67 +0,0 @@
-package backuping_physical
-
-import (
- "context"
- "log/slog"
-
- "github.com/google/uuid"
-
- physical_repositories "databasus-backend/internal/features/backups/backups/core/physical/repositories"
- backups_config_physical "databasus-backend/internal/features/backups/config/physical"
- tasks_cancellation "databasus-backend/internal/features/tasks/cancellation"
-)
-
-// PhysicalBackupCancellationListener stands physical backup work down on the two
-// events that require it: a config change (backups disabled or BackupType
-// demoted from WAL_STREAM) and a database removal. It implements both the
-// config-change and db-remove listener seams.
-type PhysicalBackupCancellationListener struct {
- canceller *PhysicalBackupCanceller
- walStreamerRepo *physical_repositories.PhysicalWalStreamerRepository
- taskCancelManager *tasks_cancellation.TaskCancelManager
- logger *slog.Logger
-}
-
-// OnBackupConfigChanged reacts to a config transition. Disabling backups cancels
-// any in-flight FULL/INCR; both disabling and demoting BackupType away from
-// WAL_STREAM delete the streamer row so the scheduler cannot silently re-spawn a
-// streamer the user turned off. The source-PG replication slot is intentionally
-// preserved (re-enabling would reuse it).
-func (l *PhysicalBackupCancellationListener) OnBackupConfigChanged(
- oldConfig, newConfig *backups_config_physical.PhysicalBackupConfig,
-) {
- databaseID := newConfig.DatabaseID
- logger := l.logger.With("database_id", databaseID)
-
- if oldConfig.IsBackupsEnabled && !newConfig.IsBackupsEnabled {
- l.canceller.CancelInFlightForDatabase(databaseID)
- }
-
- l.deleteStreamerRow(logger, databaseID)
-}
-
-// OnBeforeDatabaseRemove cancels any in-flight backup and removes the streamer
-// row before the database (and its cascade-deleted catalog rows) goes away.
-func (l *PhysicalBackupCancellationListener) OnBeforeDatabaseRemove(ctx context.Context, databaseID uuid.UUID) error {
- logger := l.logger.With("database_id", databaseID)
-
- l.canceller.CancelInFlightForDatabase(databaseID)
- l.deleteStreamerRow(logger, databaseID)
-
- return nil
-}
-
-// deleteStreamerRow tears the WAL streamer down: it first cancels the
-// long-running streamer task (registered in TaskCancelManager keyed by
-// database_id) so the local pg_receivewal goroutine stops, then deletes the
-// heartbeat row so the supervisor cannot re-spawn it. Order matters — dropping
-// the row without cancelling would leave an orphaned receiver holding the slot.
-func (l *PhysicalBackupCancellationListener) deleteStreamerRow(logger *slog.Logger, databaseID uuid.UUID) {
- if err := l.taskCancelManager.CancelTask(databaseID); err != nil {
- logger.Error("failed to cancel wal streamer task", "error", err)
- }
-
- if err := l.walStreamerRepo.DeleteByDatabaseID(databaseID); err != nil {
- logger.Error("failed to delete wal streamer row", "error", err)
- }
-}
diff --git a/backend/internal/features/backups/backups/backuping/physical/di.go b/backend/internal/features/backups/backups/backuping/physical/di.go
index 901797ad..f57ee4da 100644
--- a/backend/internal/features/backups/backups/backuping/physical/di.go
+++ b/backend/internal/features/backups/backups/backuping/physical/di.go
@@ -123,5 +123,5 @@ var SetupDependencies = sync.OnceFunc(func() {
// leaving it to pin WAL forever.
databases.GetDatabaseService().AddDbRemoveListener(physicalBackupCancellationListener)
databases.GetDatabaseService().AddDbRemoveListener(physicalSlotCleanupListener)
- backups_config_physical.GetBackupConfigService().SetBackupConfigChangeListener(physicalBackupCancellationListener)
+ backups_config_physical.GetBackupConfigService().SetBackupCancellationListener(physicalBackupCancellationListener)
})
diff --git a/backend/internal/features/backups/backups/backuping/physical/wal_supervisor.go b/backend/internal/features/backups/backups/backuping/physical/wal_supervisor.go
index 3c7c5ba6..e8a2a1a2 100644
--- a/backend/internal/features/backups/backups/backuping/physical/wal_supervisor.go
+++ b/backend/internal/features/backups/backups/backuping/physical/wal_supervisor.go
@@ -172,7 +172,7 @@ func (s *PhysicalWalStreamSupervisor) reconcile(ctx context.Context) {
func isWalStreamCandidate(backupConfig *backups_config_physical.PhysicalBackupConfig) bool {
if backupConfig.PostgresqlPhysical == nil ||
- backupConfig.PostgresqlPhysical.BackupType != postgresql_physical.BackupTypeFullIncrementalAndWalStream {
+ !backupConfig.PostgresqlPhysical.BackupType.IsWalStreaming() {
return false
}
diff --git a/backend/internal/features/backups/backups/core/physical/enums/enums.go b/backend/internal/features/backups/backups/core/physical/enums/enums.go
index 80ccdcbe..33b57ef8 100644
--- a/backend/internal/features/backups/backups/core/physical/enums/enums.go
+++ b/backend/internal/features/backups/backups/core/physical/enums/enums.go
@@ -42,8 +42,7 @@ const (
PhysicalBackupErrorStartLsnOutsideTimeline PhysicalBackupErrorReason = "START_LSN_OUTSIDE_TIMELINE_RANGE"
// Covers both an explicit user cancel on an in-flight backup AND
- // in-flight backups cancelled by OnBackupConfigChanged when the user
- // disables backups or demotes BackupType.
+ // in-flight backups cancelled when the user disables backups.
PhysicalBackupErrorCanceledByUser PhysicalBackupErrorReason = "CANCELED_BY_USER"
// In-flight backup killed because the parent DB was removed via
diff --git a/backend/internal/features/backups/config/physical/config_change_test.go b/backend/internal/features/backups/config/physical/config_change_test.go
deleted file mode 100644
index 541f91c1..00000000
--- a/backend/internal/features/backups/config/physical/config_change_test.go
+++ /dev/null
@@ -1,56 +0,0 @@
-package backups_config_physical
-
-import (
- "testing"
-
- "github.com/google/uuid"
- "github.com/stretchr/testify/assert"
-
- postgresql_physical "databasus-backend/internal/features/databases/databases/postgresql/physical"
-)
-
-type recordingConfigChangeListener struct {
- callCount int
-}
-
-func (r *recordingConfigChangeListener) OnBackupConfigChanged(_, _ *PhysicalBackupConfig) {
- r.callCount++
-}
-
-func configWithType(
- databaseID uuid.UUID,
- enabled bool,
- backupType postgresql_physical.BackupType,
-) *PhysicalBackupConfig {
- return &PhysicalBackupConfig{
- DatabaseID: databaseID,
- IsBackupsEnabled: enabled,
- PostgresqlPhysical: &postgresql_physical.PostgresqlPhysicalDatabase{BackupType: backupType},
- }
-}
-
-func Test_NotifyConfigChangeIfNeeded_FiresOnDisableAndDemoteOnly(t *testing.T) {
- recorder := &recordingConfigChangeListener{}
- service := &BackupConfigService{configChangeListener: recorder}
- databaseID := uuid.New()
-
- walStreamEnabled := configWithType(databaseID, true, postgresql_physical.BackupTypeFullIncrementalAndWalStream)
- walStreamDisabled := configWithType(databaseID, false, postgresql_physical.BackupTypeFullIncrementalAndWalStream)
- fullIncrEnabled := configWithType(databaseID, true, postgresql_physical.BackupTypeFullAndIncremental)
-
- // No-op save (no transition) must not fire.
- service.notifyConfigChangeIfNeeded(walStreamEnabled, walStreamEnabled)
- assert.Equal(t, 0, recorder.callCount)
-
- // Disable fires.
- service.notifyConfigChangeIfNeeded(walStreamEnabled, walStreamDisabled)
- assert.Equal(t, 1, recorder.callCount)
-
- // Demote from WAL_STREAM (still enabled) fires.
- service.notifyConfigChangeIfNeeded(walStreamEnabled, fullIncrEnabled)
- assert.Equal(t, 2, recorder.callCount)
-
- // Enabling-stays-enabled with same type does not fire.
- service.notifyConfigChangeIfNeeded(fullIncrEnabled, fullIncrEnabled)
- assert.Equal(t, 2, recorder.callCount)
-}
diff --git a/backend/internal/features/backups/config/physical/constants.go b/backend/internal/features/backups/config/physical/constants.go
new file mode 100644
index 00000000..722fdfb2
--- /dev/null
+++ b/backend/internal/features/backups/config/physical/constants.go
@@ -0,0 +1,8 @@
+package backups_config_physical
+
+const (
+ defaultChainsRetentionCount = 7
+ defaultFullBackupsRetentionCount = 7
+ defaultWalLagThresholdBytes = 256 * 1024 * 1024
+ defaultBackupTimeOfDay = "04:00"
+)
diff --git a/backend/internal/features/backups/config/physical/controller.go b/backend/internal/features/backups/config/physical/controller.go
index e0039acb..2fce7d26 100644
--- a/backend/internal/features/backups/config/physical/controller.go
+++ b/backend/internal/features/backups/config/physical/controller.go
@@ -80,7 +80,7 @@ func (c *BackupConfigController) GetBackupConfigByDbID(ctx *gin.Context) {
return
}
- backupConfig, err := c.backupConfigService.GetBackupConfigByDbIdWithAuth(ctx.Request.Context(), user, id)
+ backupConfig, err := c.backupConfigService.GetAndRepairBackupConfigByDbIdWithAuth(ctx.Request.Context(), user, id)
if err != nil {
ctx.JSON(http.StatusNotFound, gin.H{"error": "backup configuration not found"})
return
diff --git a/backend/internal/features/backups/config/physical/controller_test.go b/backend/internal/features/backups/config/physical/controller_test.go
index d8f0f054..881aea23 100644
--- a/backend/internal/features/backups/config/physical/controller_test.go
+++ b/backend/internal/features/backups/config/physical/controller_test.go
@@ -1,6 +1,7 @@
package backups_config_physical
import (
+ "context"
"encoding/json"
"fmt"
"net/http"
@@ -22,6 +23,7 @@ import (
users_testing "databasus-backend/internal/features/users/testing"
workspaces_controllers "databasus-backend/internal/features/workspaces/controllers"
workspaces_testing "databasus-backend/internal/features/workspaces/testing"
+ "databasus-backend/internal/storage"
test_utils "databasus-backend/internal/util/testing"
"databasus-backend/internal/util/tools"
)
@@ -104,14 +106,12 @@ func createPhysicalDatabaseViaAPI(
}
func validPhysicalConfigForFullOnly(databaseID uuid.UUID) PhysicalBackupConfig {
- timeOfDay := "04:00"
-
return PhysicalBackupConfig{
DatabaseID: databaseID,
IsBackupsEnabled: true,
FullBackupInterval: intervals.Interval{
Type: intervals.IntervalDaily,
- TimeOfDay: &timeOfDay,
+ TimeOfDay: new("04:00"),
},
Retention: RetentionFullBackups,
FullBackupsRetention: FullBackupsRetention{
@@ -337,3 +337,347 @@ func Test_GetBackupConfig_PhysicalWhenNoneExists_InitializesDefaults(t *testing.
assert.Equal(t, 7, response.FullBackupsRetention.Count)
assert.Contains(t, response.SendNotificationsOn, NotificationChainBroken)
}
+
+type recordingBackupCancellationListener struct {
+ backupsDisabledCount int
+ walStreamingDisabledCount int
+}
+
+func (r *recordingBackupCancellationListener) OnBackupsDisabled(_ context.Context, _ uuid.UUID) {
+ r.backupsDisabledCount++
+}
+
+func (r *recordingBackupCancellationListener) OnWalStreamingDisabled(_ context.Context, _ uuid.UUID) {
+ r.walStreamingDisabledCount++
+}
+
+func recordBackupCancellations(t *testing.T) *recordingBackupCancellationListener {
+ t.Helper()
+
+ previousListener := GetBackupConfigService().backupCancellationListener
+
+ recorder := &recordingBackupCancellationListener{}
+ GetBackupConfigService().SetBackupCancellationListener(recorder)
+ t.Cleanup(func() { GetBackupConfigService().SetBackupCancellationListener(previousListener) })
+
+ return recorder
+}
+
+func validPhysicalConfigForWalStream(databaseID uuid.UUID) PhysicalBackupConfig {
+ return PhysicalBackupConfig{
+ DatabaseID: databaseID,
+ IsBackupsEnabled: true,
+ FullBackupInterval: intervals.Interval{
+ Type: intervals.IntervalWeekly,
+ Weekday: new(1),
+ TimeOfDay: new("04:00"),
+ },
+ IncrementalBackupInterval: intervals.Interval{
+ Type: intervals.IntervalDaily,
+ TimeOfDay: new("05:00"),
+ },
+ Retention: RetentionChains,
+ ChainsRetention: ChainsRetention{Count: 3},
+ WalLagThresholdBytes: 64 * 1024 * 1024,
+ SendNotificationsOn: []BackupNotificationType{
+ NotificationBackupFailed,
+ NotificationWalGap,
+ },
+ }
+}
+
+type backupTypeUpdateSpec struct {
+ router *gin.Engine
+ token string
+ database *databases.Database
+ backupType postgresql_physical.BackupType
+}
+
+func updateBackupTypeViaAPI(t *testing.T, spec backupTypeUpdateSpec) {
+ t.Helper()
+
+ physicalSettings := *spec.database.PostgresqlPhysical
+ physicalSettings.BackupType = spec.backupType
+
+ updateRequest := *spec.database
+ updateRequest.PostgresqlPhysical = &physicalSettings
+
+ test_utils.MakePostRequest(
+ t,
+ spec.router,
+ "/api/v1/databases/update",
+ "Bearer "+spec.token,
+ updateRequest,
+ http.StatusOK,
+ )
+}
+
+// https://github.com/databasus/databasus/issues/746
+func Test_SaveBackupConfig_AfterBackupTypeDemotedFromWalStream_ConfigSavesCleanly(t *testing.T) {
+ router := createPhysicalTestRouter()
+ owner := users_testing.CreateTestUser(t.Context(), users_enums.UserRoleMember)
+ workspace := workspaces_testing.CreateTestWorkspace(t.Context(), "Test Workspace", owner, router)
+
+ database := createPhysicalDatabaseViaAPI(
+ t,
+ "Physical DB "+uuid.New().String(),
+ workspace.ID,
+ owner.Token,
+ router,
+ postgresql_physical.BackupTypeFullIncrementalAndWalStream,
+ "17",
+ )
+ defer func() {
+ databases.RemoveTestDatabase(t.Context(), database)
+ workspaces_testing.RemoveTestWorkspace(t.Context(), workspace, router)
+ }()
+
+ recorder := recordBackupCancellations(t)
+
+ test_utils.MakePostRequest(
+ t,
+ router,
+ "/api/v1/backup-configs/physical/save",
+ "Bearer "+owner.Token,
+ validPhysicalConfigForWalStream(database.ID),
+ http.StatusOK,
+ )
+
+ updateBackupTypeViaAPI(t, backupTypeUpdateSpec{
+ router: router,
+ token: owner.Token,
+ database: database,
+ backupType: postgresql_physical.BackupTypeFullAndIncremental,
+ })
+
+ var demotedConfig PhysicalBackupConfig
+ test_utils.MakeGetRequestAndUnmarshal(
+ t,
+ router,
+ fmt.Sprintf("/api/v1/backup-configs/physical/database/%s", database.ID),
+ "Bearer "+owner.Token,
+ http.StatusOK,
+ &demotedConfig,
+ )
+
+ assert.Zero(t, demotedConfig.WalLagThresholdBytes)
+ assert.Equal(t, RetentionChains, demotedConfig.Retention)
+ assert.Equal(t, 1, recorder.walStreamingDisabledCount, "demotion must stand the wal streamer down")
+
+ test_utils.MakePostRequest(
+ t,
+ router,
+ "/api/v1/backup-configs/physical/save",
+ "Bearer "+owner.Token,
+ demotedConfig,
+ http.StatusOK,
+ )
+}
+
+func Test_SaveBackupConfig_AfterBackupTypePromotedFromFullOnly_ConfigSavesCleanly(t *testing.T) {
+ router := createPhysicalTestRouter()
+ owner := users_testing.CreateTestUser(t.Context(), users_enums.UserRoleMember)
+ workspace := workspaces_testing.CreateTestWorkspace(t.Context(), "Test Workspace", owner, router)
+
+ database := createPhysicalDatabaseViaAPI(
+ t,
+ "Physical DB "+uuid.New().String(),
+ workspace.ID,
+ owner.Token,
+ router,
+ postgresql_physical.BackupTypeFullOnly,
+ "17",
+ )
+ defer func() {
+ databases.RemoveTestDatabase(t.Context(), database)
+ workspaces_testing.RemoveTestWorkspace(t.Context(), workspace, router)
+ }()
+
+ test_utils.MakePostRequest(
+ t,
+ router,
+ "/api/v1/backup-configs/physical/save",
+ "Bearer "+owner.Token,
+ validPhysicalConfigForFullOnly(database.ID),
+ http.StatusOK,
+ )
+
+ updateBackupTypeViaAPI(t, backupTypeUpdateSpec{
+ router: router,
+ token: owner.Token,
+ database: database,
+ backupType: postgresql_physical.BackupTypeFullIncrementalAndWalStream,
+ })
+
+ var promotedConfig PhysicalBackupConfig
+ test_utils.MakeGetRequestAndUnmarshal(
+ t,
+ router,
+ fmt.Sprintf("/api/v1/backup-configs/physical/database/%s", database.ID),
+ "Bearer "+owner.Token,
+ http.StatusOK,
+ &promotedConfig,
+ )
+
+ assert.Equal(t, RetentionChainsAndFullBackups, promotedConfig.Retention)
+ assert.Equal(t, defaultChainsRetentionCount, promotedConfig.ChainsRetention.Count)
+ assert.Equal(t, int64(defaultWalLagThresholdBytes), promotedConfig.WalLagThresholdBytes)
+ assert.Equal(t, intervals.IntervalHourly, promotedConfig.IncrementalBackupInterval.Type)
+
+ test_utils.MakePostRequest(
+ t,
+ router,
+ "/api/v1/backup-configs/physical/save",
+ "Bearer "+owner.Token,
+ promotedConfig,
+ http.StatusOK,
+ )
+}
+
+func Test_GetBackupConfig_PhysicalIncrementalWhenNoneExists_InitializesDefaultsThatSave(t *testing.T) {
+ router := createPhysicalTestRouter()
+ owner := users_testing.CreateTestUser(t.Context(), users_enums.UserRoleMember)
+ workspace := workspaces_testing.CreateTestWorkspace(t.Context(), "Test Workspace", owner, router)
+
+ database := createPhysicalDatabaseViaAPI(
+ t,
+ "Physical DB "+uuid.New().String(),
+ workspace.ID,
+ owner.Token,
+ router,
+ postgresql_physical.BackupTypeFullAndIncremental,
+ "17",
+ )
+ defer func() {
+ databases.RemoveTestDatabase(t.Context(), database)
+ workspaces_testing.RemoveTestWorkspace(t.Context(), workspace, router)
+ }()
+
+ var initializedConfig PhysicalBackupConfig
+ test_utils.MakeGetRequestAndUnmarshal(
+ t,
+ router,
+ fmt.Sprintf("/api/v1/backup-configs/physical/database/%s", database.ID),
+ "Bearer "+owner.Token,
+ http.StatusOK,
+ &initializedConfig,
+ )
+
+ assert.Equal(t, RetentionChainsAndFullBackups, initializedConfig.Retention)
+ assert.Equal(t, defaultChainsRetentionCount, initializedConfig.ChainsRetention.Count)
+ assert.Zero(t, initializedConfig.WalLagThresholdBytes)
+
+ test_utils.MakePostRequest(
+ t,
+ router,
+ "/api/v1/backup-configs/physical/save",
+ "Bearer "+owner.Token,
+ initializedConfig,
+ http.StatusOK,
+ )
+}
+
+func Test_SaveBackupConfig_WhenBackupsGetDisabled_StandsBackupWorkDown(t *testing.T) {
+ router := createPhysicalTestRouter()
+ owner := users_testing.CreateTestUser(t.Context(), users_enums.UserRoleMember)
+ workspace := workspaces_testing.CreateTestWorkspace(t.Context(), "Test Workspace", owner, router)
+
+ database := createPhysicalDatabaseViaAPI(
+ t,
+ "Physical DB "+uuid.New().String(),
+ workspace.ID,
+ owner.Token,
+ router,
+ postgresql_physical.BackupTypeFullOnly,
+ "17",
+ )
+ defer func() {
+ databases.RemoveTestDatabase(t.Context(), database)
+ workspaces_testing.RemoveTestWorkspace(t.Context(), workspace, router)
+ }()
+
+ recorder := recordBackupCancellations(t)
+
+ enabledConfig := validPhysicalConfigForFullOnly(database.ID)
+ test_utils.MakePostRequest(
+ t,
+ router,
+ "/api/v1/backup-configs/physical/save",
+ "Bearer "+owner.Token,
+ enabledConfig,
+ http.StatusOK,
+ )
+ assert.Equal(t, 0, recorder.backupsDisabledCount)
+
+ disabledConfig := enabledConfig
+ disabledConfig.IsBackupsEnabled = false
+
+ test_utils.MakePostRequest(
+ t,
+ router,
+ "/api/v1/backup-configs/physical/save",
+ "Bearer "+owner.Token,
+ disabledConfig,
+ http.StatusOK,
+ )
+
+ assert.Equal(t, 1, recorder.backupsDisabledCount)
+ assert.Equal(t, 0, recorder.walStreamingDisabledCount)
+}
+
+// https://github.com/databasus/databasus/issues/746, as left on installations
+// that switched backup type before the config followed along.
+func Test_GetBackupConfig_WhenStoredConfigPredatesTheBackupType_RepairsItOnRead(t *testing.T) {
+ router := createPhysicalTestRouter()
+ owner := users_testing.CreateTestUser(t.Context(), users_enums.UserRoleMember)
+ workspace := workspaces_testing.CreateTestWorkspace(t.Context(), "Test Workspace", owner, router)
+
+ database := createPhysicalDatabaseViaAPI(
+ t,
+ "Physical DB "+uuid.New().String(),
+ workspace.ID,
+ owner.Token,
+ router,
+ postgresql_physical.BackupTypeFullIncrementalAndWalStream,
+ "17",
+ )
+ defer func() {
+ databases.RemoveTestDatabase(t.Context(), database)
+ workspaces_testing.RemoveTestWorkspace(t.Context(), workspace, router)
+ }()
+
+ test_utils.MakePostRequest(
+ t,
+ router,
+ "/api/v1/backup-configs/physical/save",
+ "Bearer "+owner.Token,
+ validPhysicalConfigForWalStream(database.ID),
+ http.StatusOK,
+ )
+
+ require.NoError(t, storage.GetDb().
+ Model(&postgresql_physical.PostgresqlPhysicalDatabase{}).
+ Where("database_id = ?", database.ID).
+ Update("backup_type", postgresql_physical.BackupTypeFullAndIncremental).Error)
+
+ var repairedConfig PhysicalBackupConfig
+ test_utils.MakeGetRequestAndUnmarshal(
+ t,
+ router,
+ fmt.Sprintf("/api/v1/backup-configs/physical/database/%s", database.ID),
+ "Bearer "+owner.Token,
+ http.StatusOK,
+ &repairedConfig,
+ )
+
+ assert.Zero(t, repairedConfig.WalLagThresholdBytes)
+
+ test_utils.MakePostRequest(
+ t,
+ router,
+ "/api/v1/backup-configs/physical/save",
+ "Bearer "+owner.Token,
+ repairedConfig,
+ http.StatusOK,
+ )
+}
diff --git a/backend/internal/features/backups/config/physical/di.go b/backend/internal/features/backups/config/physical/di.go
index 09df2ef3..287ef6d2 100644
--- a/backend/internal/features/backups/config/physical/di.go
+++ b/backend/internal/features/backups/config/physical/di.go
@@ -7,6 +7,7 @@ import (
"databasus-backend/internal/features/notifiers"
"databasus-backend/internal/features/storages"
workspaces_services "databasus-backend/internal/features/workspaces/services"
+ "databasus-backend/internal/util/logger"
)
var (
@@ -17,6 +18,7 @@ var (
storages.GetStorageService(),
notifiers.GetNotifierService(),
workspaces_services.GetWorkspaceService(),
+ logger.GetLogger(),
nil,
nil,
}
@@ -36,4 +38,5 @@ func GetBackupConfigService() *BackupConfigService {
var SetupDependencies = sync.OnceFunc(func() {
storages.GetStorageService().AddStorageDatabaseCounter(backupConfigService)
+ databases.GetDatabaseService().AddDbBackupTypeChangeListener(backupConfigService)
})
diff --git a/backend/internal/features/backups/config/physical/interfaces.go b/backend/internal/features/backups/config/physical/interfaces.go
index 95da8118..1be6033a 100644
--- a/backend/internal/features/backups/config/physical/interfaces.go
+++ b/backend/internal/features/backups/config/physical/interfaces.go
@@ -10,10 +10,7 @@ type BackupConfigStorageChangeListener interface {
OnBeforeBackupsStorageChange(ctx context.Context, dbID uuid.UUID) error
}
-// BackupConfigChangeListener is notified when a config save crosses a
-// transition that must stand work down: backups disabled, or BackupType demoted
-// away from WAL_STREAM. The scheduler package implements it to cancel in-flight
-// backups and delete the streamer row.
-type BackupConfigChangeListener interface {
- OnBackupConfigChanged(oldConfig, newConfig *PhysicalBackupConfig)
+type BackupCancellationListener interface {
+ OnBackupsDisabled(ctx context.Context, databaseID uuid.UUID)
+ OnWalStreamingDisabled(ctx context.Context, databaseID uuid.UUID)
}
diff --git a/backend/internal/features/backups/config/physical/model.go b/backend/internal/features/backups/config/physical/model.go
index 203c1e69..78600fb7 100644
--- a/backend/internal/features/backups/config/physical/model.go
+++ b/backend/internal/features/backups/config/physical/model.go
@@ -134,6 +134,85 @@ func (b *PhysicalBackupConfig) Copy(newDatabaseID uuid.UUID) *PhysicalBackupConf
}
}
+// The database owns BackupType, so a switch there leaves the config in a shape
+// Validate rejects, and an unsaveable config cannot be edited back into shape.
+func (b *PhysicalBackupConfig) CoerceFieldsForBackupType(backupType postgresql_physical.BackupType) {
+ switch backupType {
+ case postgresql_physical.BackupTypeFullOnly:
+ b.coerceFieldsForFullOnly()
+
+ case postgresql_physical.BackupTypeFullAndIncremental:
+ b.coerceFieldsForIncremental()
+ b.WalLagThresholdBytes = 0
+
+ case postgresql_physical.BackupTypeFullIncrementalAndWalStream:
+ b.coerceFieldsForIncremental()
+
+ if b.WalLagThresholdBytes <= 0 {
+ b.WalLagThresholdBytes = defaultWalLagThresholdBytes
+ }
+ }
+}
+
+func (b *PhysicalBackupConfig) coerceFieldsForFullOnly() {
+ b.IncrementalBackupInterval = intervals.Interval{}
+ b.WalLagThresholdBytes = 0
+ b.Retention = RetentionFullBackups
+ b.ChainsRetention = ChainsRetention{}
+
+ if b.FullBackupsRetention.Validate() != nil {
+ b.FullBackupsRetention = FullBackupsRetention{
+ Policy: FullBackupsRetentionPolicyLastN,
+ Count: defaultFullBackupsRetentionCount,
+ }
+ }
+}
+
+func (b *PhysicalBackupConfig) coerceFieldsForIncremental() {
+ if b.Retention != RetentionChains && b.Retention != RetentionChainsAndFullBackups {
+ if b.FullBackupsRetention.Validate() == nil {
+ b.Retention = RetentionChainsAndFullBackups
+ } else {
+ b.Retention = RetentionChains
+ }
+ }
+
+ if b.Retention == RetentionChains {
+ b.FullBackupsRetention = FullBackupsRetention{}
+ }
+
+ if b.ChainsRetention.Count <= 0 {
+ b.ChainsRetention.Count = defaultChainsRetentionCount
+ }
+
+ if b.IncrementalBackupInterval.Validate() == nil &&
+ isIncrementalStrictlyMoreFrequent(b.IncrementalBackupInterval, b.FullBackupInterval) {
+ return
+ }
+
+ b.IncrementalBackupInterval = buildIntervalFasterThan(b.FullBackupInterval)
+}
+
+func buildIntervalFasterThan(fullBackupInterval intervals.Interval) intervals.Interval {
+ switch fullBackupInterval.Type {
+ case intervals.IntervalDaily:
+ return intervals.Interval{Type: intervals.IntervalHourly}
+
+ case intervals.IntervalWeekly, intervals.IntervalMonthly, intervals.IntervalCron:
+ timeOfDay := defaultBackupTimeOfDay
+ if fullBackupInterval.TimeOfDay != nil {
+ timeOfDay = *fullBackupInterval.TimeOfDay
+ }
+
+ return intervals.Interval{Type: intervals.IntervalDaily, TimeOfDay: &timeOfDay}
+
+ default:
+ // No fixed interval is more frequent than HOURLY; the owner has to slow
+ // the full cadence down first.
+ return intervals.Interval{}
+ }
+}
+
func (b *PhysicalBackupConfig) validateRetentionFields() error {
switch b.Retention {
case RetentionChains:
diff --git a/backend/internal/features/backups/config/physical/model_test.go b/backend/internal/features/backups/config/physical/model_test.go
index fb931b62..03b3309f 100644
--- a/backend/internal/features/backups/config/physical/model_test.go
+++ b/backend/internal/features/backups/config/physical/model_test.go
@@ -348,3 +348,111 @@ func Test_Validate_RejectsMissingPostgresqlPhysicalPreload(t *testing.T) {
assert.Error(t, err)
assert.Contains(t, err.Error(), "preloaded")
}
+
+func Test_CoerceFieldsForBackupType_AcrossEveryTransition_ProducesConfigValidForTheNewType(t *testing.T) {
+ transitions := []struct {
+ name string
+ buildConfig func() *PhysicalBackupConfig
+ newBackupType postgresql_physical.BackupType
+ assertCoercedConfig func(t *testing.T, coercedConfig *PhysicalBackupConfig)
+ }{
+ {
+ name: "WAL_STREAM -> FULL_INCREMENTAL",
+ buildConfig: validFullIncrementalWalStreamConfig,
+ newBackupType: postgresql_physical.BackupTypeFullAndIncremental,
+ assertCoercedConfig: func(t *testing.T, coercedConfig *PhysicalBackupConfig) {
+ assert.Zero(t, coercedConfig.WalLagThresholdBytes)
+ assert.Equal(t, RetentionChains, coercedConfig.Retention)
+ assert.Equal(t, dailyAt("02:00"), coercedConfig.IncrementalBackupInterval)
+ },
+ },
+ {
+ name: "WAL_STREAM -> FULL",
+ buildConfig: validFullIncrementalWalStreamConfig,
+ newBackupType: postgresql_physical.BackupTypeFullOnly,
+ assertCoercedConfig: func(t *testing.T, coercedConfig *PhysicalBackupConfig) {
+ assert.Zero(t, coercedConfig.WalLagThresholdBytes)
+ assert.Equal(t, intervals.Interval{}, coercedConfig.IncrementalBackupInterval)
+ assert.Equal(t, RetentionFullBackups, coercedConfig.Retention)
+ assert.Zero(t, coercedConfig.ChainsRetention.Count)
+ assert.Equal(t, FullBackupsRetentionPolicyLastN, coercedConfig.FullBackupsRetention.Policy)
+ assert.Equal(t, defaultFullBackupsRetentionCount, coercedConfig.FullBackupsRetention.Count)
+ },
+ },
+ {
+ name: "FULL_INCREMENTAL -> WAL_STREAM",
+ buildConfig: validFullIncrementalConfig,
+ newBackupType: postgresql_physical.BackupTypeFullIncrementalAndWalStream,
+ assertCoercedConfig: func(t *testing.T, coercedConfig *PhysicalBackupConfig) {
+ assert.Equal(t, int64(defaultWalLagThresholdBytes), coercedConfig.WalLagThresholdBytes)
+ assert.Equal(t, RetentionChains, coercedConfig.Retention)
+ },
+ },
+ {
+ name: "FULL_INCREMENTAL -> FULL",
+ buildConfig: validFullIncrementalConfig,
+ newBackupType: postgresql_physical.BackupTypeFullOnly,
+ assertCoercedConfig: func(t *testing.T, coercedConfig *PhysicalBackupConfig) {
+ assert.Equal(t, intervals.Interval{}, coercedConfig.IncrementalBackupInterval)
+ assert.Equal(t, RetentionFullBackups, coercedConfig.Retention)
+ assert.Zero(t, coercedConfig.ChainsRetention.Count)
+ },
+ },
+ {
+ name: "FULL -> FULL_INCREMENTAL",
+ buildConfig: validFullOnlyConfig,
+ newBackupType: postgresql_physical.BackupTypeFullAndIncremental,
+ assertCoercedConfig: func(t *testing.T, coercedConfig *PhysicalBackupConfig) {
+ assert.Zero(t, coercedConfig.WalLagThresholdBytes)
+ assert.Equal(t, RetentionChainsAndFullBackups, coercedConfig.Retention)
+ assert.Equal(t, defaultChainsRetentionCount, coercedConfig.ChainsRetention.Count)
+ assert.Equal(t, hourly(), coercedConfig.IncrementalBackupInterval)
+ },
+ },
+ {
+ name: "FULL -> WAL_STREAM",
+ buildConfig: validFullOnlyConfig,
+ newBackupType: postgresql_physical.BackupTypeFullIncrementalAndWalStream,
+ assertCoercedConfig: func(t *testing.T, coercedConfig *PhysicalBackupConfig) {
+ assert.Equal(t, int64(defaultWalLagThresholdBytes), coercedConfig.WalLagThresholdBytes)
+ assert.Equal(t, RetentionChainsAndFullBackups, coercedConfig.Retention)
+ assert.Equal(t, hourly(), coercedConfig.IncrementalBackupInterval)
+ },
+ },
+ }
+
+ for _, transition := range transitions {
+ t.Run(transition.name, func(t *testing.T) {
+ coercedConfig := transition.buildConfig()
+
+ coercedConfig.CoerceFieldsForBackupType(transition.newBackupType)
+ coercedConfig.PostgresqlPhysical.BackupType = transition.newBackupType
+
+ assert.NoError(t, coercedConfig.Validate())
+ transition.assertCoercedConfig(t, coercedConfig)
+ })
+ }
+}
+
+func Test_CoerceFieldsForBackupType_WhenFullCadenceIsHourly_LeavesIncrementalCadenceForTheUser(t *testing.T) {
+ promotedConfig := validFullOnlyConfig()
+ promotedConfig.FullBackupInterval = hourly()
+
+ promotedConfig.CoerceFieldsForBackupType(postgresql_physical.BackupTypeFullAndIncremental)
+ promotedConfig.PostgresqlPhysical.BackupType = postgresql_physical.BackupTypeFullAndIncremental
+
+ assert.Equal(t, intervals.Interval{}, promotedConfig.IncrementalBackupInterval)
+ assert.Error(t, promotedConfig.Validate())
+}
+
+func Test_CoerceFieldsForBackupType_WhenIncrementalCadenceIsSlowerThanFull_DerivesAFasterOne(t *testing.T) {
+ promotedConfig := validFullOnlyConfig()
+ promotedConfig.FullBackupInterval = weeklyMondayAt("02:00")
+ promotedConfig.IncrementalBackupInterval = weeklyMondayAt("03:00")
+
+ promotedConfig.CoerceFieldsForBackupType(postgresql_physical.BackupTypeFullAndIncremental)
+ promotedConfig.PostgresqlPhysical.BackupType = postgresql_physical.BackupTypeFullAndIncremental
+
+ assert.Equal(t, dailyAt("02:00"), promotedConfig.IncrementalBackupInterval)
+ assert.NoError(t, promotedConfig.Validate())
+}
diff --git a/backend/internal/features/backups/config/physical/service.go b/backend/internal/features/backups/config/physical/service.go
index 442f5526..0d5d1fe3 100644
--- a/backend/internal/features/backups/config/physical/service.go
+++ b/backend/internal/features/backups/config/physical/service.go
@@ -3,12 +3,13 @@ package backups_config_physical
import (
"context"
"errors"
+ "fmt"
+ "log/slog"
"time"
"github.com/google/uuid"
"databasus-backend/internal/features/databases"
- postgresql_physical "databasus-backend/internal/features/databases/databases/postgresql/physical"
"databasus-backend/internal/features/intervals"
"databasus-backend/internal/features/notifiers"
"databasus-backend/internal/features/storages"
@@ -22,9 +23,10 @@ type BackupConfigService struct {
storageService *storages.StorageService
notifierService *notifiers.NotifierService
workspaceService *workspaces_services.WorkspaceService
+ logger *slog.Logger
- dbStorageChangeListener BackupConfigStorageChangeListener
- configChangeListener BackupConfigChangeListener
+ dbStorageChangeListener BackupConfigStorageChangeListener
+ backupCancellationListener BackupCancellationListener
}
func (s *BackupConfigService) SetDatabaseStorageChangeListener(
@@ -33,10 +35,10 @@ func (s *BackupConfigService) SetDatabaseStorageChangeListener(
s.dbStorageChangeListener = dbStorageChangeListener
}
-func (s *BackupConfigService) SetBackupConfigChangeListener(
- configChangeListener BackupConfigChangeListener,
+func (s *BackupConfigService) SetBackupCancellationListener(
+ backupCancellationListener BackupCancellationListener,
) {
- s.configChangeListener = configChangeListener
+ s.backupCancellationListener = backupCancellationListener
}
func (s *BackupConfigService) GetStorageAttachedDatabasesIDs(
@@ -142,14 +144,15 @@ func (s *BackupConfigService) SaveBackupConfig(
return nil, err
}
- if existingConfig != nil && s.configChangeListener != nil {
- s.notifyConfigChangeIfNeeded(existingConfig, backupConfig)
+ if existingConfig != nil && s.backupCancellationListener != nil &&
+ existingConfig.IsBackupsEnabled && !backupConfig.IsBackupsEnabled {
+ s.backupCancellationListener.OnBackupsDisabled(ctx, backupConfig.DatabaseID)
}
return savedConfig, nil
}
-func (s *BackupConfigService) GetBackupConfigByDbIdWithAuth(
+func (s *BackupConfigService) GetAndRepairBackupConfigByDbIdWithAuth(
ctx context.Context,
user *users_models.User,
databaseID uuid.UUID,
@@ -159,7 +162,12 @@ func (s *BackupConfigService) GetBackupConfigByDbIdWithAuth(
return nil, err
}
- return s.GetBackupConfigByDbId(databaseID)
+ backupConfig, err := s.GetBackupConfigByDbId(databaseID)
+ if err != nil {
+ return nil, err
+ }
+
+ return s.repairAndSaveConfigForBackupType(ctx, backupConfig)
}
func (s *BackupConfigService) GetBackupConfigByDbId(
@@ -241,6 +249,48 @@ func (s *BackupConfigService) OnDatabaseCopied(ctx context.Context, originalData
_, _ = s.SaveBackupConfig(ctx, newConfig)
}
+func (s *BackupConfigService) OnBackupTypeChanged(
+ ctx context.Context,
+ change databases.BackupTypeChange,
+) {
+ logger := s.logger.With("database_id", change.DatabaseID)
+
+ // Before the config write and regardless of its outcome: a streamer left
+ // running keeps pinning WAL on the source cluster.
+ if s.backupCancellationListener != nil &&
+ change.OldBackupType.IsWalStreaming() && !change.NewBackupType.IsWalStreaming() {
+ s.backupCancellationListener.OnWalStreamingDisabled(ctx, change.DatabaseID)
+ }
+
+ backupConfig, err := s.backupConfigRepository.FindByDatabaseID(change.DatabaseID)
+ if err != nil {
+ logger.ErrorContext(ctx, "failed to load backup config after backup type change", "error", err)
+
+ return
+ }
+
+ if backupConfig == nil {
+ return
+ }
+
+ backupConfig.CoerceFieldsForBackupType(change.NewBackupType)
+
+ if err := backupConfig.Validate(); err != nil {
+ logger.ErrorContext(ctx, "backup config is not valid for the new backup type", "error", err)
+
+ return
+ }
+
+ if _, err := s.backupConfigRepository.Save(backupConfig); err != nil {
+ logger.ErrorContext(ctx, "failed to save backup config after backup type change", "error", err)
+
+ return
+ }
+
+ logger.InfoContext(ctx, fmt.Sprintf("backup config updated for backup type change: %s -> %s",
+ change.OldBackupType, change.NewBackupType))
+}
+
func (s *BackupConfigService) CreateDisabledBackupConfig(databaseID uuid.UUID) error {
return s.initializeDefaultConfig(databaseID)
}
@@ -353,20 +403,54 @@ func (s *BackupConfigService) TransferDatabaseToWorkspace(
return nil
}
+// Heals rows left unsaveable by a backup type switch that predates this repair.
+// Owner-facing reads only: a background caller repairing a row it merely polled
+// would race the owner's own save.
+func (s *BackupConfigService) repairAndSaveConfigForBackupType(
+ ctx context.Context,
+ backupConfig *PhysicalBackupConfig,
+) (*PhysicalBackupConfig, error) {
+ if backupConfig.PostgresqlPhysical == nil || backupConfig.Validate() == nil {
+ return backupConfig, nil
+ }
+
+ logger := s.logger.With("database_id", backupConfig.DatabaseID)
+
+ backupConfig.CoerceFieldsForBackupType(backupConfig.PostgresqlPhysical.BackupType)
+
+ if err := backupConfig.Validate(); err != nil {
+ logger.WarnContext(ctx, "backup config cannot be repaired for its backup type", "error", err)
+
+ return backupConfig, nil
+ }
+
+ repairedConfig, err := s.backupConfigRepository.Save(backupConfig)
+ if err != nil {
+ return nil, err
+ }
+
+ logger.InfoContext(ctx, "repaired backup config for its backup type")
+
+ return repairedConfig, nil
+}
+
func (s *BackupConfigService) initializeDefaultConfig(databaseID uuid.UUID) error {
- timeOfDay := "04:00"
+ database, err := s.databaseService.GetDatabaseByID(databaseID)
+ if err != nil {
+ return err
+ }
- _, err := s.backupConfigRepository.Save(&PhysicalBackupConfig{
+ defaultConfig := &PhysicalBackupConfig{
DatabaseID: databaseID,
IsBackupsEnabled: false,
FullBackupInterval: intervals.Interval{
Type: intervals.IntervalDaily,
- TimeOfDay: &timeOfDay,
+ TimeOfDay: new(defaultBackupTimeOfDay),
},
Retention: RetentionFullBackups,
FullBackupsRetention: FullBackupsRetention{
Policy: FullBackupsRetentionPolicyLastN,
- Count: 7,
+ Count: defaultFullBackupsRetentionCount,
},
SendNotificationsOn: []BackupNotificationType{
NotificationBackupFailed,
@@ -375,7 +459,13 @@ func (s *BackupConfigService) initializeDefaultConfig(databaseID uuid.UUID) erro
NotificationWalGap,
},
Encryption: "NONE",
- })
+ }
+
+ if database.PostgresqlPhysical != nil {
+ defaultConfig.CoerceFieldsForBackupType(database.PostgresqlPhysical.BackupType)
+ }
+
+ _, err = s.backupConfigRepository.Save(defaultConfig)
return err
}
@@ -431,23 +521,6 @@ func (s *BackupConfigService) assignTargetNotifiers(
return s.databaseService.UpdateDatabaseNotifiers(databaseID, targetNotifiers)
}
-// notifyConfigChangeIfNeeded fires the config-change listener only on the two
-// transitions that must stand work down: backups disabled or BackupType
-// demoted away from WAL_STREAM.
-func (s *BackupConfigService) notifyConfigChangeIfNeeded(oldConfig, newConfig *PhysicalBackupConfig) {
- disabled := oldConfig.IsBackupsEnabled && !newConfig.IsBackupsEnabled
- demotedFromWalStream := isWalStream(oldConfig) && !isWalStream(newConfig)
-
- if disabled || demotedFromWalStream {
- s.configChangeListener.OnBackupConfigChanged(oldConfig, newConfig)
- }
-}
-
-func isWalStream(backupConfig *PhysicalBackupConfig) bool {
- return backupConfig.PostgresqlPhysical != nil &&
- backupConfig.PostgresqlPhysical.BackupType == postgresql_physical.BackupTypeFullIncrementalAndWalStream
-}
-
func storageIDsEqual(id1, id2 *uuid.UUID) bool {
if id1 == nil && id2 == nil {
return true
diff --git a/backend/internal/features/databases/databases/postgresql/physical/enums.go b/backend/internal/features/databases/databases/postgresql/physical/enums.go
index 8ea4c6df..73eaad84 100644
--- a/backend/internal/features/databases/databases/postgresql/physical/enums.go
+++ b/backend/internal/features/databases/databases/postgresql/physical/enums.go
@@ -12,6 +12,10 @@ func (t BackupType) IsRequireWalSummary() bool {
return t == BackupTypeFullAndIncremental || t == BackupTypeFullIncrementalAndWalStream
}
+func (t BackupType) IsWalStreaming() bool {
+ return t == BackupTypeFullIncrementalAndWalStream
+}
+
// platform is the detected runtime classification of the source cluster.
// Used to choose the right replication-grant SQL and to format actionable
// fix messages — managed PG can't ALTER SYSTEM, so we tell the user where
diff --git a/backend/internal/features/databases/di.go b/backend/internal/features/databases/di.go
index 3e9be8bb..27d3f95b 100644
--- a/backend/internal/features/databases/di.go
+++ b/backend/internal/features/databases/di.go
@@ -21,6 +21,7 @@ var databaseService = &DatabaseService{
[]DatabaseCreationListener{},
[]DatabaseRemoveListener{},
[]DatabaseCopyListener{},
+ []DatabaseBackupTypeChangeListener{},
workspaces_services.GetWorkspaceService(),
audit_logs.GetAuditLogService(),
encryption.GetFieldEncryptor(),
diff --git a/backend/internal/features/databases/interfaces.go b/backend/internal/features/databases/interfaces.go
index c0e824e4..ae8d4289 100644
--- a/backend/internal/features/databases/interfaces.go
+++ b/backend/internal/features/databases/interfaces.go
@@ -4,6 +4,8 @@ import (
"context"
"github.com/google/uuid"
+
+ postgresql_physical "databasus-backend/internal/features/databases/databases/postgresql/physical"
)
type DatabaseCreationListener interface {
@@ -17,3 +19,13 @@ type DatabaseRemoveListener interface {
type DatabaseCopyListener interface {
OnDatabaseCopied(ctx context.Context, originalDatabaseID, newDatabaseID uuid.UUID)
}
+
+type BackupTypeChange struct {
+ DatabaseID uuid.UUID
+ OldBackupType postgresql_physical.BackupType
+ NewBackupType postgresql_physical.BackupType
+}
+
+type DatabaseBackupTypeChangeListener interface {
+ OnBackupTypeChanged(ctx context.Context, change BackupTypeChange)
+}
diff --git a/backend/internal/features/databases/service.go b/backend/internal/features/databases/service.go
index 9c15a4da..e55f7796 100644
--- a/backend/internal/features/databases/service.go
+++ b/backend/internal/features/databases/service.go
@@ -14,6 +14,7 @@ import (
audit_logs "databasus-backend/internal/features/audit_logs"
audit_logs_models "databasus-backend/internal/features/audit_logs/models"
physical_core_service "databasus-backend/internal/features/backups/backups/core/physical/service"
+ postgresql_physical "databasus-backend/internal/features/databases/databases/postgresql/physical"
"databasus-backend/internal/features/notifiers"
users_models "databasus-backend/internal/features/users/models"
workspaces_services "databasus-backend/internal/features/workspaces/services"
@@ -25,9 +26,10 @@ type DatabaseService struct {
notifierService *notifiers.NotifierService
logger *slog.Logger
- dbCreationListener []DatabaseCreationListener
- dbRemoveListener []DatabaseRemoveListener
- dbCopyListener []DatabaseCopyListener
+ dbCreationListener []DatabaseCreationListener
+ dbRemoveListener []DatabaseRemoveListener
+ dbCopyListener []DatabaseCopyListener
+ dbBackupTypeChangeListener []DatabaseBackupTypeChangeListener
workspaceService *workspaces_services.WorkspaceService
auditLogService *audit_logs.AuditLogService
@@ -53,6 +55,12 @@ func (s *DatabaseService) AddDbCopyListener(
s.dbCopyListener = append(s.dbCopyListener, dbCopyListener)
}
+func (s *DatabaseService) AddDbBackupTypeChangeListener(
+ dbBackupTypeChangeListener DatabaseBackupTypeChangeListener,
+) {
+ s.dbBackupTypeChangeListener = append(s.dbBackupTypeChangeListener, dbBackupTypeChangeListener)
+}
+
func (s *DatabaseService) GetNotifierAttachedDatabasesIDs(
notifierID uuid.UUID,
) ([]uuid.UUID, error) {
@@ -170,6 +178,8 @@ func (s *DatabaseService) UpdateDatabase(
}
}
+ oldBackupType := getPhysicalBackupType(existingDatabase)
+
existingDatabase.Update(database)
if err := existingDatabase.Validate(); err != nil {
@@ -213,6 +223,16 @@ func (s *DatabaseService) UpdateDatabase(
return err
}
+ if newBackupType := getPhysicalBackupType(existingDatabase); newBackupType != oldBackupType {
+ for _, listener := range s.dbBackupTypeChangeListener {
+ listener.OnBackupTypeChanged(ctx, BackupTypeChange{
+ DatabaseID: existingDatabase.ID,
+ OldBackupType: oldBackupType,
+ NewBackupType: newBackupType,
+ })
+ }
+ }
+
if oldName != existingDatabase.Name {
s.auditLogService.WriteAuditLog(ctx, audit_logs_models.AuditEntry{
Message: fmt.Sprintf("Database updated and renamed from '%s' to '%s'", oldName, existingDatabase.Name),
@@ -1004,3 +1024,11 @@ func (s *DatabaseService) resolveConnectionTarget(database *Database) (*Database
return existingDatabase, nil
}
+
+func getPhysicalBackupType(database *Database) postgresql_physical.BackupType {
+ if database.PostgresqlPhysical == nil {
+ return ""
+ }
+
+ return database.PostgresqlPhysical.BackupType
+}