Skip to content

Commit 7472aa1

Browse files
FIX (backups): Do not double close backup file
1 parent d05efc3 commit 7472aa1

File tree

3 files changed

+10
-15
lines changed

3 files changed

+10
-15
lines changed

backend/internal/features/restores/usecases/postgresql/restore_backup_uc.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,8 @@ func (uc *RestorePostgresqlBackupUsecase) downloadBackupToTempFile(
222222
return "", nil, fmt.Errorf("failed to write backup to temporary file: %w", err)
223223
}
224224

225-
// Close the temp file to ensure all data is written
226-
if err := tempFile.Close(); err != nil {
227-
cleanupFunc()
228-
return "", nil, fmt.Errorf("failed to close temporary backup file: %w", err)
229-
}
225+
// Close the temp file to ensure all data is written - this is handled by defer
226+
// Removing explicit close to avoid double-close error
230227

231228
uc.logger.Info("Backup file written to temporary location", "tempFile", tempBackupFile)
232229
return tempBackupFile, cleanupFunc, nil

backend/internal/features/storages/models/local/model.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@ func (l *LocalStorage) SaveFile(logger *slog.Logger, fileID uuid.UUID, file io.R
6262
return fmt.Errorf("failed to sync temp file: %w", err)
6363
}
6464

65-
err = tempFile.Close()
66-
if err != nil {
67-
logger.Error("Failed to close temp file", "fileId", fileID.String(), "error", err)
68-
return fmt.Errorf("failed to close temp file: %w", err)
69-
}
70-
7165
finalPath := filepath.Join(config.GetEnv().DataFolder, fileID.String())
7266
logger.Debug(
7367
"Moving file from temp to final location",

backend/internal/features/tests/postgresql_backup_restore_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,14 @@ func verifyDataIntegrity(t *testing.T, originalDB *sqlx.DB, restoredDB *sqlx.DB)
222222
assert.NoError(t, err)
223223

224224
assert.Equal(t, len(originalData), len(restoredData), "Should have same number of rows")
225-
for i := range originalData {
226-
assert.Equal(t, originalData[i].ID, restoredData[i].ID, "ID should match")
227-
assert.Equal(t, originalData[i].Name, restoredData[i].Name, "Name should match")
228-
assert.Equal(t, originalData[i].Value, restoredData[i].Value, "Value should match")
225+
226+
// Only compare data if both slices have elements (to avoid panic)
227+
if len(originalData) > 0 && len(restoredData) > 0 {
228+
for i := range originalData {
229+
assert.Equal(t, originalData[i].ID, restoredData[i].ID, "ID should match")
230+
assert.Equal(t, originalData[i].Name, restoredData[i].Name, "Name should match")
231+
assert.Equal(t, originalData[i].Value, restoredData[i].Value, "Value should match")
232+
}
229233
}
230234
}
231235

0 commit comments

Comments
 (0)