Skip to content

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

FEATURE (physical backups): Add physical backups healthchecks

Rostislav Dugin7663973ca93f
diff --git a/backend/internal/features/databases/model.go b/backend/internal/features/databases/model.go
index 2886f8f9..7da60423 100644
--- a/backend/internal/features/databases/model.go
+++ b/backend/internal/features/databases/model.go
@@ -109,14 +109,34 @@ func (d *Database) TestConnection(
) error {
switch d.Type {
case DatabaseTypePostgresLogical:
+ if d.PostgresqlLogical == nil {
+ return errors.New("postgresql logical config is not set")
+ }
+
return d.PostgresqlLogical.TestConnection(logger, encryptor)
case DatabaseTypePostgresPhysical:
+ if d.PostgresqlPhysical == nil {
+ return errors.New("postgresql physical config is not set")
+ }
+
return d.PostgresqlPhysical.TestReplicationConnection(logger, encryptor)
case DatabaseTypeMysql:
+ if d.Mysql == nil {
+ return errors.New("mysql config is not set")
+ }
+
return d.Mysql.TestConnection(logger, encryptor)
case DatabaseTypeMariadb:
+ if d.Mariadb == nil {
+ return errors.New("mariadb config is not set")
+ }
+
return d.Mariadb.TestConnection(logger, encryptor)
case DatabaseTypeMongodb:
+ if d.Mongodb == nil {
+ return errors.New("mongodb config is not set")
+ }
+
return d.Mongodb.TestConnection(logger, encryptor)
default:
return errors.New("connection test not supported for database type: " + string(d.Type))
diff --git a/backend/internal/features/databases/model_test.go b/backend/internal/features/databases/model_test.go
new file mode 100644
index 00000000..5058541f
--- /dev/null
+++ b/backend/internal/features/databases/model_test.go
@@ -0,0 +1,39 @@
+package databases
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+
+ "databasus-backend/internal/util/encryption"
+ "databasus-backend/internal/util/logger"
+)
+
+func Test_TestConnection_ForEveryDatabaseType_WithoutEngineConfig_ReturnsErrorInsteadOfPanicking(
+ t *testing.T,
+) {
+ missingConfigCases := []struct {
+ databaseType DatabaseType
+ expectedError string
+ }{
+ {DatabaseTypePostgresLogical, "postgresql logical config is not set"},
+ {DatabaseTypePostgresPhysical, "postgresql physical config is not set"},
+ {DatabaseTypeMysql, "mysql config is not set"},
+ {DatabaseTypeMariadb, "mariadb config is not set"},
+ {DatabaseTypeMongodb, "mongodb config is not set"},
+ {DatabaseType("CASSANDRA"), "connection test not supported for database type: CASSANDRA"},
+ }
+
+ for _, missingConfigCase := range missingConfigCases {
+ t.Run(string(missingConfigCase.databaseType), func(t *testing.T) {
+ databaseWithoutConfig := Database{Type: missingConfigCase.databaseType}
+
+ err := databaseWithoutConfig.TestConnection(
+ logger.GetLogger(),
+ encryption.GetFieldEncryptor(),
+ )
+
+ assert.EqualError(t, err, missingConfigCase.expectedError)
+ })
+ }
+}
diff --git a/backend/internal/features/databases/physical_model_test.go b/backend/internal/features/databases/physical_model_test.go
index ece6cc7a..8f05eed3 100644
--- a/backend/internal/features/databases/physical_model_test.go
+++ b/backend/internal/features/databases/physical_model_test.go
@@ -155,6 +155,29 @@ func Test_TestDatabaseConnectionDirect_OnReplicationReadyCluster_ReturnsSuccess(
}
}
+func Test_TestDatabaseConnectionDirect_WithoutPhysicalConfig_ReturnsBadRequest(t *testing.T) {
+ router := createTestRouter()
+ owner := users_testing.CreateTestUser(users_enums.UserRoleMember)
+ workspace := workspaces_testing.CreateTestWorkspace("Test Workspace", owner, router)
+ defer workspaces_testing.RemoveTestWorkspace(workspace, router)
+
+ databaseWithoutConfig := Database{
+ WorkspaceID: &workspace.ID,
+ Name: "physical db without config",
+ Type: DatabaseTypePostgresPhysical,
+ }
+
+ response := test_utils.MakePostRequest(
+ t, router,
+ "/api/v1/databases/test-connection-direct",
+ "Bearer "+owner.Token,
+ databaseWithoutConfig,
+ http.StatusBadRequest,
+ )
+
+ assert.Contains(t, string(response.Body), "postgresql physical config is not set")
+}
+
func Test_CreatePhysicalDatabase_DetectsVersionFromServer_OverridingPayloadVersion(t *testing.T) {
for _, fx := range physicalFixtures() {
t.Run(fx.name, func(t *testing.T) {
@@ -237,6 +260,51 @@ func Test_CreateReplicationOnlyUser_OnLogicalDatabase_ReturnsBadRequest(t *testi
assert.Contains(t, string(testResp.Body), "POSTGRES_PHYSICAL")
}
+func Test_UpdatePhysicalDatabase_KeepsSameChildRow_WithStableIdAndDatabaseId(t *testing.T) {
+ for _, fx := range physicalFixtures() {
+ t.Run(fx.name, func(t *testing.T) {
+ router := createTestRouter()
+ owner := users_testing.CreateTestUser(users_enums.UserRoleMember)
+ workspace := workspaces_testing.CreateTestWorkspace("Test Workspace", owner, router)
+
+ database := createPhysicalDatabaseInternalAPI(t, router, workspace.ID, owner.Token, fx)
+ defer func() {
+ RemoveTestDatabase(database)
+ workspaces_testing.RemoveTestWorkspace(workspace, router)
+ }()
+
+ createdDatabase, err := databaseRepository.FindByID(database.ID)
+ require.NoError(t, err)
+ require.NotNil(t, createdDatabase.PostgresqlPhysical)
+ require.NotEqual(t, uuid.Nil, createdDatabase.PostgresqlPhysical.ID)
+ require.NotNil(t, createdDatabase.PostgresqlPhysical.DatabaseID)
+ assert.Equal(t, database.ID, *createdDatabase.PostgresqlPhysical.DatabaseID)
+
+ createdDatabase.Name = "Renamed " + createdDatabase.Name
+ test_utils.MakePostRequest(
+ t, router,
+ "/api/v1/databases/update",
+ "Bearer "+owner.Token,
+ createdDatabase,
+ http.StatusOK,
+ )
+
+ updatedDatabase, err := databaseRepository.FindByID(database.ID)
+ require.NoError(t, err)
+ require.NotNil(t, updatedDatabase.PostgresqlPhysical)
+ assert.Equal(t,
+ createdDatabase.PostgresqlPhysical.ID,
+ updatedDatabase.PostgresqlPhysical.ID,
+ "update must reuse the existing physical config row, not insert a second one")
+ require.NotNil(t, updatedDatabase.PostgresqlPhysical.DatabaseID)
+ assert.Equal(t, database.ID, *updatedDatabase.PostgresqlPhysical.DatabaseID)
+ assert.Equal(t,
+ createdDatabase.PostgresqlPhysical.ReplicationSlotName,
+ updatedDatabase.PostgresqlPhysical.ReplicationSlotName)
+ })
+ }
+}
+
func Test_CopyDatabase_OnPhysicalDatabase_DoesNotCopySystemIdentifier(t *testing.T) {
for _, fx := range physicalFixtures() {
t.Run(fx.name, func(t *testing.T) {
diff --git a/backend/internal/features/databases/repository.go b/backend/internal/features/databases/repository.go
index 2b7c9b24..6ae6a82c 100644
--- a/backend/internal/features/databases/repository.go
+++ b/backend/internal/features/databases/repository.go
@@ -1,8 +1,6 @@
package databases
import (
- "errors"
-
"github.com/google/uuid"
"gorm.io/gorm"
@@ -24,39 +22,20 @@ func (r *DatabaseRepository) Save(database *Database) (*Database, error) {
}
err := db.Transaction(func(tx *gorm.DB) error {
- switch database.Type {
- case DatabaseTypePostgresLogical:
- if database.PostgresqlLogical == nil {
- return errors.New("postgresql configuration is required for PostgreSQL database")
- }
- database.PostgresqlLogical.DatabaseID = &database.ID
- case DatabaseTypeMysql:
- if database.Mysql == nil {
- return errors.New("mysql configuration is required for MySQL database")
- }
- database.Mysql.DatabaseID = &database.ID
- case DatabaseTypeMariadb:
- if database.Mariadb == nil {
- return errors.New("mariadb configuration is required for MariaDB database")
- }
- database.Mariadb.DatabaseID = &database.ID
- case DatabaseTypeMongodb:
- if database.Mongodb == nil {
- return errors.New("mongodb configuration is required for MongoDB database")
- }
- database.Mongodb.DatabaseID = &database.ID
+ if err := database.Validate(); err != nil {
+ return err
}
+ withoutAssociations := tx.Omit(
+ "PostgresqlLogical", "PostgresqlPhysical", "Mysql", "Mariadb", "Mongodb", "Notifiers",
+ )
+
if isNew {
- if err := tx.Create(database).
- Omit("Postgresql", "Mysql", "Mariadb", "Mongodb", "Notifiers").
- Error; err != nil {
+ if err := withoutAssociations.Create(database).Error; err != nil {
return err
}
} else {
- if err := tx.Save(database).
- Omit("Postgresql", "Mysql", "Mariadb", "Mongodb", "Notifiers").
- Error; err != nil {
+ if err := withoutAssociations.Save(database).Error; err != nil {
return err
}
}
@@ -74,6 +53,18 @@ func (r *DatabaseRepository) Save(database *Database) (*Database, error) {
return err
}
}
+ case DatabaseTypePostgresPhysical:
+ database.PostgresqlPhysical.DatabaseID = &database.ID
+ if database.PostgresqlPhysical.ID == uuid.Nil {
+ database.PostgresqlPhysical.ID = uuid.New()
+ if err := tx.Create(database.PostgresqlPhysical).Error; err != nil {
+ return err
+ }
+ } else {
+ if err := tx.Save(database.PostgresqlPhysical).Error; err != nil {
+ return err
+ }
+ }
case DatabaseTypeMysql:
database.Mysql.DatabaseID = &database.ID
if database.Mysql.ID == uuid.Nil {
diff --git a/backend/internal/features/databases/service.go b/backend/internal/features/databases/service.go
index 0f524da2..ccbf4900 100644
--- a/backend/internal/features/databases/service.go
+++ b/backend/internal/features/databases/service.go
@@ -922,8 +922,6 @@ func (s *DatabaseService) CreateReplicationOnlyUser(
return username, password, nil
}
-// resolveConnectionTarget merges an unsaved request over the persisted database
-// when an ID is supplied, so a connection test uses the same merge rules as save.
// fillPhysicalLastBackupTimes populates LastBackupTime for physical databases,
// whose backups (FULL / INCREMENTAL / WAL) live outside the databases table and so
// are not denormalized onto the row the way logical backups are. A failure here is
@@ -955,6 +953,8 @@ func (s *DatabaseService) fillPhysicalLastBackupTimes(databases []*Database) {
}
}
+// resolveConnectionTarget merges an unsaved request over the persisted database
+// when an ID is supplied, so a connection test uses the same merge rules as save.
func (s *DatabaseService) resolveConnectionTarget(database *Database) (*Database, error) {
if database.ID == uuid.Nil {
return database, nil
diff --git a/backend/internal/features/databases/testing.go b/backend/internal/features/databases/testing.go
index a508dcd9..684a4eb2 100644
--- a/backend/internal/features/databases/testing.go
+++ b/backend/internal/features/databases/testing.go
@@ -20,6 +20,8 @@ import (
"databasus-backend/internal/util/tools"
)
+var PhysicalPostgresVersionTags = []string{"17", "18"}
+
func GetTestPostgresConfig() *postgresql_logical.PostgresqlLogicalDatabase {
env := config.GetEnv()
port, err := strconv.Atoi(env.TestLogicalPostgres16Port)
@@ -47,7 +49,10 @@ func physicalPostgresVersion(versionTag string) tools.PostgresqlVersion {
case "18":
return tools.PostgresqlVersion18
default:
- panic(fmt.Sprintf("unsupported physical postgres version tag: %s (use \"17\" or \"18\")", versionTag))
+ panic(fmt.Sprintf(
+ "unsupported physical postgres version tag: %s (supported: %v)",
+ versionTag, PhysicalPostgresVersionTags,
+ ))
}
}
@@ -66,7 +71,10 @@ func physicalPrimaryEndpoint(versionTag string) (string, int) {
case "18":
portStr = env.TestPhysicalPostgres18Port
default:
- panic(fmt.Sprintf("unsupported physical postgres version tag: %s (use \"17\" or \"18\")", versionTag))
+ panic(fmt.Sprintf(
+ "unsupported physical postgres version tag: %s (supported: %v)",
+ versionTag, PhysicalPostgresVersionTags,
+ ))
}
port, err := strconv.Atoi(portStr)
diff --git a/backend/internal/features/healthcheck/attempt/background_service.go b/backend/internal/features/healthcheck/attempt/background_service.go
index c809ba5f..644314f1 100644
--- a/backend/internal/features/healthcheck/attempt/background_service.go
+++ b/backend/internal/features/healthcheck/attempt/background_service.go
@@ -49,11 +49,15 @@ func (s *HealthcheckAttemptBackgroundService) checkDatabases() {
}
for _, healthcheckConfig := range healthcheckConfigs {
- go func(healthcheckConfig *healthcheck_config.HealthcheckConfig) {
- err := s.checkDatabaseHealthUseCase.Execute(now, healthcheckConfig)
+ go func() {
+ err := s.checkDatabaseHealthUseCase.Execute(now, &healthcheckConfig)
if err != nil {
- s.logger.Error("failed to check database health", "error", err)
+ s.logger.Error(
+ "failed to check database health",
+ "database_id", healthcheckConfig.DatabaseID,
+ "error", err,
+ )
}
- }(&healthcheckConfig)
+ }()
}
}
diff --git a/backend/internal/features/healthcheck/attempt/check_database_health_uc.go b/backend/internal/features/healthcheck/attempt/check_database_health_uc.go
index 1534d063..7e29e826 100644
--- a/backend/internal/features/healthcheck/attempt/check_database_health_uc.go
+++ b/backend/internal/features/healthcheck/attempt/check_database_health_uc.go
@@ -30,11 +30,6 @@ func (uc *CheckDatabaseHealthUseCase) Execute(
return err
}
- err = uc.validateDatabase(database)
- if err != nil {
- return err
- }
-
isExecuteNewAttempt, err := uc.isReadyForNewAttempt(
now,
database,
@@ -48,13 +43,12 @@ func (uc *CheckDatabaseHealthUseCase) Execute(
return nil
}
- heathcheckAttempt, err := uc.healthcheckDatabase(now, database)
+ healthcheckAttempt, err := uc.healthcheckDatabase(now, database)
if err != nil {
return err
}
- // Save the attempt
- err = uc.healthcheckAttemptRepository.Insert(heathcheckAttempt)
+ err = uc.healthcheckAttemptRepository.Insert(healthcheckAttempt)
if err != nil {
return err
}
@@ -62,7 +56,7 @@ func (uc *CheckDatabaseHealthUseCase) Execute(
err = uc.updateDatabaseHealthStatusIfChanged(
database,
healthcheckConfig,
- heathcheckAttempt,
+ healthcheckAttempt,
)
if err != nil {
return err
@@ -82,18 +76,18 @@ func (uc *CheckDatabaseHealthUseCase) Execute(
func (uc *CheckDatabaseHealthUseCase) updateDatabaseHealthStatusIfChanged(
database *databases.Database,
healthcheckConfig *healthcheck_config.HealthcheckConfig,
- heathcheckAttempt *HealthcheckAttempt,
+ healthcheckAttempt *HealthcheckAttempt,
) error {
- if &heathcheckAttempt.Status == database.HealthStatus {
+ if database.HealthStatus != nil && *database.HealthStatus == healthcheckAttempt.Status {
return nil
}
if (database.HealthStatus == nil ||
*database.HealthStatus == databases.HealthStatusUnavailable) &&
- heathcheckAttempt.Status == databases.HealthStatusAvailable {
+ healthcheckAttempt.Status == databases.HealthStatusAvailable {
err := uc.databaseService.SetHealthStatus(
database.ID,
- &heathcheckAttempt.Status,
+ &healthcheckAttempt.Status,
)
if err != nil {
return err
@@ -102,16 +96,14 @@ func (uc *CheckDatabaseHealthUseCase) updateDatabaseHealthStatusIfChanged(
uc.sendDbStatusNotification(
healthcheckConfig,
database,
- heathcheckAttempt.Status,
+ healthcheckAttempt.Status,
)
}
if (database.HealthStatus == nil ||
*database.HealthStatus == databases.HealthStatusAvailable) &&
- heathcheckAttempt.Status == databases.HealthStatusUnavailable {
- if healthcheckConfig.AttemptsBeforeConcideredAsDown <= 1 {
- // proceed, 1 fail is enough to consider db as down
- } else {
+ healthcheckAttempt.Status == databases.HealthStatusUnavailable {
+ if healthcheckConfig.AttemptsBeforeConcideredAsDown > 1 {
lastHealthcheckAttempts, err := uc.healthcheckAttemptRepository.FindByDatabaseIDWithLimit(
database.ID,
healthcheckConfig.AttemptsBeforeConcideredAsDown,
@@ -133,7 +125,7 @@ func (uc *CheckDatabaseHealthUseCase) updateDatabaseHealthStatusIfChanged(
err := uc.databaseService.SetHealthStatus(
database.ID,
- &heathcheckAttempt.Status,
+ &healthcheckAttempt.Status,
)
if err != nil {
return err
@@ -153,7 +145,6 @@ func (uc *CheckDatabaseHealthUseCase) healthcheckDatabase(
now time.Time,
database *databases.Database,
) (*HealthcheckAttempt, error) {
- // Test the connection
healthStatus := databases.HealthStatusAvailable
err := uc.databaseService.TestDatabaseConnectionDirect(database)
if err != nil {
@@ -166,7 +157,6 @@ func (uc *CheckDatabaseHealthUseCase) healthcheckDatabase(
)
}
- // Create health check attempt
attempt := &HealthcheckAttempt{
ID: uuid.New(),
DatabaseID: database.ID,
@@ -177,33 +167,6 @@ func (uc *CheckDatabaseHealthUseCase) healthcheckDatabase(
return attempt, nil
}
-func (uc *CheckDatabaseHealthUseCase) validateDatabase(
- database *databases.Database,
-) error {
- switch database.Type {
- case databases.DatabaseTypePostgresLogical:
- if database.PostgresqlLogical == nil {
- return fmt.Errorf("database Postgresql config is not set")
- }
- case databases.DatabaseTypeMysql:
- if database.Mysql == nil {
- return fmt.Errorf("database MySQL config is not set")
- }
- case databases.DatabaseTypeMariadb:
- if database.Mariadb == nil {
- return fmt.Errorf("database MariaDB config is not set")
- }
- case databases.DatabaseTypeMongodb:
- if database.Mongodb == nil {
- return fmt.Errorf("database MongoDB config is not set")
- }
- default:
- return fmt.Errorf("unsupported database type: %s", database.Type)
- }
-
- return nil
-}
-
func (uc *CheckDatabaseHealthUseCase) isReadyForNewAttempt(
now time.Time,
database *databases.Database,
diff --git a/backend/internal/features/healthcheck/attempt/check_database_health_uc_test.go b/backend/internal/features/healthcheck/attempt/check_database_health_uc_test.go
index b22287ce..d68e4e47 100644
--- a/backend/internal/features/healthcheck/attempt/check_database_health_uc_test.go
+++ b/backend/internal/features/healthcheck/attempt/check_database_health_uc_test.go
@@ -6,6 +6,7 @@ import (
"testing"
"time"
+ "github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
@@ -35,6 +36,30 @@ func onlineNotification(databaseName string) notifier_models.Notification {
}
}
+const anyPhysicalVersionTag = "17"
+
+func newTestUseCase(
+ healthcheckAttemptSender HealthcheckAttemptSender,
+ databaseService DatabaseService,
+) *CheckDatabaseHealthUseCase {
+ return &CheckDatabaseHealthUseCase{
+ &HealthcheckAttemptRepository{},
+ healthcheckAttemptSender,
+ databaseService,
+ }
+}
+
+func newTestHealthcheckConfig(databaseID uuid.UUID) *healthcheck_config.HealthcheckConfig {
+ return &healthcheck_config.HealthcheckConfig{
+ DatabaseID: databaseID,
+ IsHealthcheckEnabled: true,
+ IsSentNotificationWhenUnavailable: true,
+ IntervalMinutes: 1,
+ AttemptsBeforeConcideredAsDown: 1,
+ StoreAttemptsDays: 7,
+ }
+}
+
func Test_CheckDatabaseHealthUseCase(t *testing.T) {
user := users_testing.CreateTestUser(users_enums.UserRoleAdmin)
@@ -82,11 +107,7 @@ func Test_CheckDatabaseHealthUseCase(t *testing.T) {
}
// Create use case with mock sender
- useCase := &CheckDatabaseHealthUseCase{
- healthcheckAttemptRepository: &HealthcheckAttemptRepository{},
- healthcheckAttemptSender: mockSender,
- databaseService: mockDatabaseService,
- }
+ useCase := newTestUseCase(mockSender, mockDatabaseService)
// Execute healthcheck
err := useCase.Execute(time.Now().UTC(), healthcheckConfig)
@@ -145,11 +166,7 @@ func Test_CheckDatabaseHealthUseCase(t *testing.T) {
}
// Create use case with mock sender
- useCase := &CheckDatabaseHealthUseCase{
- healthcheckAttemptRepository: &HealthcheckAttemptRepository{},
- healthcheckAttemptSender: mockSender,
- databaseService: mockDatabaseService,
- }
+ useCase := newTestUseCase(mockSender, mockDatabaseService)
// Execute first healthcheck
err := useCase.Execute(time.Now().UTC(), healthcheckConfig)
@@ -221,11 +238,7 @@ func Test_CheckDatabaseHealthUseCase(t *testing.T) {
}
// Create use case with mock sender
- useCase := &CheckDatabaseHealthUseCase{
- healthcheckAttemptRepository: &HealthcheckAttemptRepository{},
- healthcheckAttemptSender: mockSender,
- databaseService: mockDatabaseService,
- }
+ useCase := newTestUseCase(mockSender, mockDatabaseService)
// Execute three failed healthchecks
now := time.Now().UTC()
@@ -296,11 +309,7 @@ func Test_CheckDatabaseHealthUseCase(t *testing.T) {
}
// Create use case with mock sender
- useCase := &CheckDatabaseHealthUseCase{
- healthcheckAttemptRepository: &HealthcheckAttemptRepository{},
- healthcheckAttemptSender: mockSender,
- databaseService: mockDatabaseService,
- }
+ useCase := newTestUseCase(mockSender, mockDatabaseService)
// Execute healthcheck (should succeed)
err = useCase.Execute(time.Now().UTC(), healthcheckConfig)
@@ -362,11 +371,7 @@ func Test_CheckDatabaseHealthUseCase(t *testing.T) {
}
// Create use case with mock sender
- useCase := &CheckDatabaseHealthUseCase{
- healthcheckAttemptRepository: &HealthcheckAttemptRepository{},
- healthcheckAttemptSender: mockSender,
- databaseService: mockDatabaseService,
- }
+ useCase := newTestUseCase(mockSender, mockDatabaseService)
// Execute first healthcheck
now := time.Now().UTC()
@@ -436,4 +441,90 @@ func Test_CheckDatabaseHealthUseCase(t *testing.T) {
assert.Equal(t, databases.HealthStatusAvailable, attempts[1].Status)
},
)
+
+ t.Run(
+ "Test_CheckPhysicalDbHealth_WhenClusterIsReplicationReady_AttemptMarkedAvailable",
+ func(t *testing.T) {
+ for _, physicalVersionTag := range databases.PhysicalPostgresVersionTags {
+ t.Run("pg"+physicalVersionTag, func(t *testing.T) {
+ database := databases.CreateTestPhysicalPostgresDatabase(
+ workspace.ID,
+ notifier,
+ physicalVersionTag,
+ )
+ defer databases.RemoveTestDatabase(database)
+
+ mockSender := &MockHealthcheckAttemptSender{}
+ mockSender.On("SendNotification", mock.Anything, mock.Anything).Return()
+
+ useCase := newTestUseCase(mockSender, databases.GetDatabaseService())
+
+ err := useCase.Execute(time.Now().UTC(), newTestHealthcheckConfig(database.ID))
+ assert.NoError(t, err)
+
+ attempts, err := useCase.healthcheckAttemptRepository.FindByDatabaseIDWithLimit(
+ database.ID,
+ 1,
+ )
+ assert.NoError(t, err)
+ assert.Len(t, attempts, 1)
+ assert.Equal(t, databases.HealthStatusAvailable, attempts[0].Status)
+
+ persistedDatabase, err := databases.GetDatabaseService().
+ GetDatabaseByID(database.ID)
+ assert.NoError(t, err)
+ if assert.NotNil(t, persistedDatabase.HealthStatus) {
+ assert.Equal(
+ t,
+ databases.HealthStatusAvailable,
+ *persistedDatabase.HealthStatus,
+ )
+ }
+ })
+ }
+ },
+ )
+
+ t.Run(
+ "Test_CheckPhysicalDbHealth_WhenConnectionFails_DbMarkedAsUnavailable",
+ func(t *testing.T) {
+ database := databases.CreateTestPhysicalPostgresDatabase(
+ workspace.ID,
+ notifier,
+ anyPhysicalVersionTag,
+ )
+ defer databases.RemoveTestDatabase(database)
+
+ mockSender := &MockHealthcheckAttemptSender{}
+ mockSender.On("SendNotification", mock.Anything, mock.Anything).Return()
+
+ mockDatabaseService := &MockDatabaseService{}
+ mockDatabaseService.On("TestDatabaseConnectionDirect", database).
+ Return(errors.New("replication connection refused"))
+ unavailableStatus := databases.HealthStatusUnavailable
+ mockDatabaseService.On("SetHealthStatus", database.ID, &unavailableStatus).Return(nil)
+ mockDatabaseService.On("GetDatabaseByID", database.ID).Return(database, nil)
+
+ useCase := newTestUseCase(mockSender, mockDatabaseService)
+
+ err := useCase.Execute(time.Now().UTC(), newTestHealthcheckConfig(database.ID))
+ assert.NoError(t, err)
+
+ attempts, err := useCase.healthcheckAttemptRepository.FindByDatabaseIDWithLimit(
+ database.ID,
+ 1,
+ )
+ assert.NoError(t, err)
+ assert.Len(t, attempts, 1)
+ assert.Equal(t, databases.HealthStatusUnavailable, attempts[0].Status)
+
+ mockDatabaseService.AssertCalled(t, "SetHealthStatus", database.ID, &unavailableStatus)
+ mockSender.AssertCalled(
+ t,
+ "SendNotification",
+ mock.Anything,
+ unavailableNotification(database.Name),
+ )
+ },
+ )
}
diff --git a/backend/internal/features/restores/controller_test.go b/backend/internal/features/restores/controller_test.go
index aec3b9e1..8257f51c 100644
--- a/backend/internal/features/restores/controller_test.go
+++ b/backend/internal/features/restores/controller_test.go
@@ -534,6 +534,17 @@ func Test_RestoreBackup_WithParallelRestoreInProgress_ReturnsError(t *testing.T)
database, backup := createTestDatabaseWithBackupForRestore(workspace, owner, router)
defer cleanupDatabaseWithBackup(database, backup)
+ // Seeded rather than started for real: a restore dispatched over HTTP runs in a goroutine that
+ // reaches a terminal status within milliseconds, so the IN_PROGRESS state the guard reacts to
+ // would be a race.
+ restoreInProgress := restoring.CreateTestRestore(t, backup, restores_core.RestoreStatusInProgress)
+ defer func() {
+ restoreRepository := &restores_core.RestoreRepository{}
+ if err := restoreRepository.DeleteByID(restoreInProgress.ID); err != nil {
+ t.Logf("Failed to delete seeded restore: %v", err)
+ }
+ }()
+
request := restores_core.RestoreBackupRequest{
PostgresqlLogicalDatabase: &postgresql_logical.PostgresqlLogicalDatabase{
Version: tools.PostgresqlVersion16,
@@ -544,17 +555,7 @@ func Test_RestoreBackup_WithParallelRestoreInProgress_ReturnsError(t *testing.T)
},
}
- testResp := test_utils.MakePostRequest(
- t,
- router,
- fmt.Sprintf("/api/v1/restores/%s/restore", backup.ID.String()),
- "Bearer "+owner.Token,
- request,
- http.StatusOK,
- )
- assert.Contains(t, string(testResp.Body), "restore started successfully")
-
- testResp2 := test_utils.MakePostRequest(
+ response := test_utils.MakePostRequest(
t,
router,
fmt.Sprintf("/api/v1/restores/%s/restore", backup.ID.String()),
@@ -563,7 +564,7 @@ func Test_RestoreBackup_WithParallelRestoreInProgress_ReturnsError(t *testing.T)
http.StatusBadRequest,
)
- assert.Contains(t, string(testResp2.Body), "another restore is already in progress")
+ assert.Contains(t, string(response.Body), "another restore is already in progress")
}
func createTestRouter() *gin.Engine {
diff --git a/backend/internal/util/testing/containers/containers.go b/backend/internal/util/testing/containers/containers.go
index 398e0f6d..a70a739f 100644
--- a/backend/internal/util/testing/containers/containers.go
+++ b/backend/internal/util/testing/containers/containers.go
@@ -17,6 +17,8 @@ import (
// under go test -p=N; 512m dwarfs every test fixture and tmpfs only consumes the bytes written.
const dataDirTmpfsOptions = "rw,size=512m"
+const loopbackIP = "127.0.0.1"
+
// Endpoint is the reachable address of a started container's primary port.
type Endpoint struct {
Host string
@@ -57,8 +59,9 @@ func startContainer(t *testing.T, req testcontainers.ContainerRequest, mappedPor
return ContainerHandle{Endpoint: endpointOf(t, container, mappedPort), Container: container}
}
-// endpointOf honours TESTCONTAINERS_HOST_OVERRIDE, which CI sets to reach containers published on
-// the Docker host bridge.
+// The MySQL client resolves the name "localhost" to a unix socket rather than TCP, and the server
+// lives in a container where no such socket exists. testcontainers reports "localhost" whenever
+// TESTCONTAINERS_HOST_OVERRIDE is unset, i.e. on every local run.
func endpointOf(t *testing.T, container testcontainers.Container, mappedPort string) Endpoint {
t.Helper()
@@ -69,6 +72,10 @@ func endpointOf(t *testing.T, container testcontainers.Container, mappedPort str
t.Fatalf("failed to get container host: %v", err)
}
+ if host == "localhost" {
+ host = loopbackIP
+ }
+
port, err := container.MappedPort(ctx, mappedPort)
if err != nil {
t.Fatalf("failed to get container mapped port: %v", err)
diff --git a/backend/internal/util/testing/containers/mongodb.go b/backend/internal/util/testing/containers/mongodb.go
index fd18c9b7..57f1faae 100644
--- a/backend/internal/util/testing/containers/mongodb.go
+++ b/backend/internal/util/testing/containers/mongodb.go
@@ -23,6 +23,17 @@ const mongodbPort = "27017/tcp"
// as soon as mongod is listening, so the ceiling is free there.
const mongodbStartupTimeout = 240 * time.Second
+// The image entrypoint boots mongod twice: on 127.0.0.1 to create the root user, then on 0.0.0.0.
+// Docker's proxy accepts on the mapped port during the first boot, so a port check alone hands out
+// a connection that is immediately reset.
+func mongodbReady() wait.Strategy {
+ return wait.ForAll(
+ wait.ForLog("Waiting for connections").
+ WithOccurrence(2).WithStartupTimeout(mongodbStartupTimeout),
+ wait.ForListeningPort(mongodbPort),
+ )
+}
+
func mongodbEnv() map[string]string {
return map[string]string{
"MONGO_INITDB_ROOT_USERNAME": MongodbUsername,
@@ -40,7 +51,7 @@ func mongodbRequest(image string) testcontainers.ContainerRequest {
Env: mongodbEnv(),
Cmd: []string{"mongod", "--auth"},
Tmpfs: map[string]string{"/data/db": dataDirTmpfsOptions},
- WaitingFor: wait.ForListeningPort(mongodbPort).WithStartupTimeout(mongodbStartupTimeout),
+ WaitingFor: mongodbReady(),
}
}
@@ -72,7 +83,7 @@ func StartMongodbSSL(t *testing.T, pemPath, crtPath string) Endpoint {
"--tlsCAFile", "/etc/ssl-test/server.crt",
"--tlsAllowConnectionsWithoutCertificates",
},
- WaitingFor: wait.ForListeningPort(mongodbPort).WithStartupTimeout(mongodbStartupTimeout),
+ WaitingFor: mongodbReady(),
}
return start(t, req, mongodbPort)
diff --git a/frontend/src/entity/databases/index.ts b/frontend/src/entity/databases/index.ts
index 54aee3df..2fd52674 100644
--- a/frontend/src/entity/databases/index.ts
+++ b/frontend/src/entity/databases/index.ts
@@ -2,6 +2,7 @@ export { databaseApi } from './api/databaseApi';
export { type Database } from './model/Database';
export { DatabaseType } from './model/DatabaseType';
export { getDatabaseLogoFromType } from './model/getDatabaseLogoFromType';
+export { getDatabaseTypeLabel } from './model/getDatabaseTypeLabel';
export { isPostgresType } from './model/isPostgresType';
export { initializeDatabaseTypeData } from './model/initializeDatabaseTypeData';
export { Period } from './model/Period';
diff --git a/frontend/src/entity/databases/model/getDatabaseTypeLabel.ts b/frontend/src/entity/databases/model/getDatabaseTypeLabel.ts
new file mode 100644
index 00000000..138b9105
--- /dev/null
+++ b/frontend/src/entity/databases/model/getDatabaseTypeLabel.ts
@@ -0,0 +1,17 @@
+import { DatabaseType } from './DatabaseType';
+
+export const getDatabaseTypeLabel = (type: DatabaseType): string => {
+ switch (type) {
+ case DatabaseType.POSTGRES_LOGICAL:
+ case DatabaseType.POSTGRES_PHYSICAL:
+ return 'PostgreSQL';
+ case DatabaseType.MYSQL:
+ return 'MySQL';
+ case DatabaseType.MARIADB:
+ return 'MariaDB';
+ case DatabaseType.MONGODB:
+ return 'MongoDB';
+ default:
+ return 'database';
+ }
+};
diff --git a/frontend/src/features/databases/ui/DatabaseComponent.tsx b/frontend/src/features/databases/ui/DatabaseComponent.tsx
index be7b1ca7..d49141dd 100644
--- a/frontend/src/features/databases/ui/DatabaseComponent.tsx
+++ b/frontend/src/features/databases/ui/DatabaseComponent.tsx
@@ -36,7 +36,7 @@ export const DatabaseComponent = ({
setIsHealthcheckVisible(isVisible);
};
- const isPostgresDatabase = database?.type === DatabaseType.POSTGRES_LOGICAL;
+ const isLogicalDatabase = database?.type === DatabaseType.POSTGRES_LOGICAL;
const isPhysicalDatabase = database?.type === DatabaseType.POSTGRES_PHYSICAL;
const loadSettings = () => {
@@ -74,7 +74,7 @@ export const DatabaseComponent = ({
Backups
</div>
- {isPostgresDatabase && (
+ {isLogicalDatabase && (
<div
className={`mr-2 cursor-pointer rounded-tl-md rounded-tr-md px-6 py-2 ${currentTab === 'verifications' ? 'bg-white dark:bg-gray-800' : 'bg-gray-200 dark:bg-gray-700'}`}
onClick={() => setCurrentTab('verifications')}
@@ -122,7 +122,7 @@ export const DatabaseComponent = ({
</>
)}
- {currentTab === 'verifications' && isPostgresDatabase && (
+ {currentTab === 'verifications' && isLogicalDatabase && (
<VerificationsComponent
database={database}
isCanManageDBs={isCanManageDBs}
diff --git a/frontend/src/features/databases/ui/DatabaseConfigComponent.tsx b/frontend/src/features/databases/ui/DatabaseConfigComponent.tsx
index e9cb581b..3ff21d87 100644
--- a/frontend/src/features/databases/ui/DatabaseConfigComponent.tsx
+++ b/frontend/src/features/databases/ui/DatabaseConfigComponent.tsx
@@ -193,7 +193,7 @@ export const DatabaseConfigComponent = ({
});
};
- const isPostgresDatabase = database.type === DatabaseType.POSTGRES_LOGICAL;
+ const isLogicalDatabase = database.type === DatabaseType.POSTGRES_LOGICAL;
return (
<div className="relative w-full rounded-tr-md rounded-br-md rounded-bl-md bg-white p-3 shadow sm:p-5 dark:bg-gray-800">
@@ -405,7 +405,7 @@ export const DatabaseConfigComponent = ({
</div>
</div>
- {isPostgresDatabase && (
+ {isLogicalDatabase && (
<div className="w-full lg:w-[400px]">
<div className="mt-5 flex items-center font-bold">
<div>Restore verification</div>
diff --git a/frontend/src/features/databases/ui/edit/CreateReadOnlyComponent.tsx b/frontend/src/features/databases/ui/edit/CreateReadOnlyComponent.tsx
index e6f98677..a82ea8d3 100644
--- a/frontend/src/features/databases/ui/edit/CreateReadOnlyComponent.tsx
+++ b/frontend/src/features/databases/ui/edit/CreateReadOnlyComponent.tsx
@@ -1,7 +1,12 @@
import { Button, Modal, Spin } from 'antd';
import { useEffect, useState } from 'react';
-import { type Database, DatabaseType, databaseApi } from '../../../../entity/databases';
+import {
+ type Database,
+ DatabaseType,
+ databaseApi,
+ getDatabaseTypeLabel,
+} from '../../../../entity/databases';
interface Props {
database: Database;
@@ -27,21 +32,12 @@ export const CreateReadOnlyComponent = ({
const [privileges, setPrivileges] = useState<string[]>([]);
const [isPrivilegesExpanded, setIsPrivilegesExpanded] = useState(false);
- const isPostgres = database.type === DatabaseType.POSTGRES_LOGICAL;
+ const isLogicalPostgres = database.type === DatabaseType.POSTGRES_LOGICAL;
const isPhysicalPostgres = database.type === DatabaseType.POSTGRES_PHYSICAL;
const isMysql = database.type === DatabaseType.MYSQL;
const isMariadb = database.type === DatabaseType.MARIADB;
const isMongodb = database.type === DatabaseType.MONGODB;
- const databaseTypeName =
- isPostgres || isPhysicalPostgres
- ? 'PostgreSQL'
- : isMysql
- ? 'MySQL'
- : isMariadb
- ? 'MariaDB'
- : isMongodb
- ? 'MongoDB'
- : 'database';
+ const databaseTypeName = getDatabaseTypeLabel(database.type);
const privilegesLabel = isMongodb ? 'roles' : 'privileges';
const userKindNoun = isPhysicalPostgres ? 'replication-only user' : 'read-only user';
@@ -82,7 +78,7 @@ export const CreateReadOnlyComponent = ({
if (isPhysicalPostgres && database.postgresqlPhysical) {
database.postgresqlPhysical.username = response.username;
database.postgresqlPhysical.password = response.password;
- } else if (isPostgres && database.postgresqlLogical) {
+ } else if (isLogicalPostgres && database.postgresqlLogical) {
database.postgresqlLogical.username = response.username;
database.postgresqlLogical.password = response.password;
} else if (isMysql && database.mysql) {
diff --git a/frontend/src/features/databases/ui/show/ShowDatabaseBaseInfoComponent.tsx b/frontend/src/features/databases/ui/show/ShowDatabaseBaseInfoComponent.tsx
index c444f4de..76165d8b 100644
--- a/frontend/src/features/databases/ui/show/ShowDatabaseBaseInfoComponent.tsx
+++ b/frontend/src/features/databases/ui/show/ShowDatabaseBaseInfoComponent.tsx
@@ -1,4 +1,8 @@
-import { type Database, getDatabaseLogoFromType } from '../../../../entity/databases';
+import {
+ type Database,
+ getDatabaseLogoFromType,
+ getDatabaseTypeLabel,
+} from '../../../../entity/databases';
interface Props {
database: Database;
@@ -20,7 +24,7 @@ export const ShowDatabaseBaseInfoComponent = ({ database, isShowName, isShowType
<div className="mb-1 flex w-full items-center">
<div className="min-w-[150px]">Database type</div>
<div className="flex items-center">
- <span>{database.type === 'POSTGRES_LOGICAL' ? 'PostgreSQL' : 'MySQL'}</span>
+ <span>{getDatabaseTypeLabel(database.type)}</span>
<img
src={getDatabaseLogoFromType(database.type)}
alt="databaseIcon"