68 lines
2.0 KiB
Go
68 lines
2.0 KiB
Go
package notificationstore
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
pgtable "galaxy/notification/internal/adapters/postgres/jet/notification/table"
|
|
|
|
pg "github.com/go-jet/jet/v2/postgres"
|
|
)
|
|
|
|
// DeleteRecordsOlderThan removes records rows whose `accepted_at` predates
|
|
// cutoff. The records FK CASCADE clears the dependent routes and
|
|
// dead_letters rows in the same statement.
|
|
func (store *Store) DeleteRecordsOlderThan(ctx context.Context, cutoff time.Time) (int64, error) {
|
|
if store == nil {
|
|
return 0, errors.New("delete notification records: nil store")
|
|
}
|
|
operationCtx, cancel, err := store.operationContext(ctx, "delete notification records")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer cancel()
|
|
|
|
stmt := pgtable.Records.DELETE().
|
|
WHERE(pgtable.Records.AcceptedAt.LT(pg.TimestampzT(cutoff.UTC())))
|
|
|
|
query, args := stmt.Sql()
|
|
result, err := store.db.ExecContext(operationCtx, query, args...)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("delete notification records: %w", err)
|
|
}
|
|
rows, err := result.RowsAffected()
|
|
if err != nil {
|
|
return 0, fmt.Errorf("delete notification records: rows affected: %w", err)
|
|
}
|
|
return rows, nil
|
|
}
|
|
|
|
// DeleteMalformedIntentsOlderThan removes malformed-intent rows whose
|
|
// `recorded_at` predates cutoff.
|
|
func (store *Store) DeleteMalformedIntentsOlderThan(ctx context.Context, cutoff time.Time) (int64, error) {
|
|
if store == nil {
|
|
return 0, errors.New("delete malformed intents: nil store")
|
|
}
|
|
operationCtx, cancel, err := store.operationContext(ctx, "delete malformed intents")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer cancel()
|
|
|
|
stmt := pgtable.MalformedIntents.DELETE().
|
|
WHERE(pgtable.MalformedIntents.RecordedAt.LT(pg.TimestampzT(cutoff.UTC())))
|
|
|
|
query, args := stmt.Sql()
|
|
result, err := store.db.ExecContext(operationCtx, query, args...)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("delete malformed intents: %w", err)
|
|
}
|
|
rows, err := result.RowsAffected()
|
|
if err != nil {
|
|
return 0, fmt.Errorf("delete malformed intents: rows affected: %w", err)
|
|
}
|
|
return rows, nil
|
|
}
|