mirror of
https://github.com/eduardolat/pgbackweb.git
synced 2026-05-11 22:19:16 -05:00
Add soft delete executions functionality
This commit is contained in:
@@ -1,13 +0,0 @@
|
||||
package executions
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (s *Service) DeleteExecution(
|
||||
ctx context.Context, id uuid.UUID,
|
||||
) error {
|
||||
return s.dbgen.ExecutionsServiceDeleteExecution(ctx, id)
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
-- name: ExecutionsServiceDeleteExecution :exec
|
||||
DELETE FROM executions
|
||||
WHERE id = @id;
|
||||
@@ -0,0 +1,40 @@
|
||||
package executions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (s *Service) SoftDeleteExecution(
|
||||
ctx context.Context, executionID uuid.UUID,
|
||||
) error {
|
||||
execution, err := s.dbgen.ExecutionsServiceGetExecutionForSoftDelete(
|
||||
ctx, dbgen.ExecutionsServiceGetExecutionForSoftDeleteParams{
|
||||
ExecutionID: executionID,
|
||||
EncryptionKey: *s.env.PBW_ENCRYPTION_KEY,
|
||||
},
|
||||
)
|
||||
if err != nil && errors.Is(err, sql.ErrNoRows) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if execution.ExecutionPath.Valid {
|
||||
err := s.ints.S3Client.Delete(
|
||||
execution.DestinationAccessKey, execution.DestinationSecretKey,
|
||||
execution.DestinationRegion, execution.DestinationEndpoint,
|
||||
execution.DestinationBucketName, execution.ExecutionPath.String,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return s.dbgen.ExecutionsServiceSoftDeleteExecution(ctx, executionID)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
-- name: ExecutionsServiceGetExecutionForSoftDelete :one
|
||||
SELECT
|
||||
executions.id as execution_id,
|
||||
executions.path as execution_path,
|
||||
|
||||
backups.id as backup_id,
|
||||
|
||||
destinations.bucket_name as destination_bucket_name,
|
||||
destinations.region as destination_region,
|
||||
destinations.endpoint as destination_endpoint,
|
||||
pgp_sym_decrypt(
|
||||
destinations.access_key, @encryption_key
|
||||
) AS destination_access_key,
|
||||
pgp_sym_decrypt(
|
||||
destinations.secret_key, @encryption_key
|
||||
) AS destination_secret_key
|
||||
FROM executions
|
||||
JOIN backups ON backups.id = executions.backup_id
|
||||
JOIN destinations ON destinations.id = backups.destination_id
|
||||
WHERE executions.id = @execution_id;
|
||||
|
||||
-- name: ExecutionsServiceSoftDeleteExecution :exec
|
||||
UPDATE executions
|
||||
SET
|
||||
status = 'deleted',
|
||||
deleted_at = NOW()
|
||||
WHERE id = @id;
|
||||
@@ -0,0 +1,18 @@
|
||||
package executions
|
||||
|
||||
import "context"
|
||||
|
||||
func (s *Service) SoftDeleteExpiredExecutions(ctx context.Context) error {
|
||||
expiredExecutions, err := s.dbgen.ExecutionsServiceGetExpiredExecutions(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, execution := range expiredExecutions {
|
||||
if err := s.SoftDeleteExecution(ctx, execution.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
-- name: ExecutionsServiceGetExpiredExecutions :many
|
||||
SELECT executions.*
|
||||
FROM executions
|
||||
JOIN backups ON executions.backup_id = backups.id
|
||||
WHERE
|
||||
backups.retention_days > 0
|
||||
AND executions.status != 'deleted'
|
||||
AND executions.finished_at IS NOT NULL
|
||||
AND (
|
||||
executions.finished_at + (backups.retention_days || ' days')::INTERVAL
|
||||
) < NOW();
|
||||
Reference in New Issue
Block a user