Skip to content

Commit

Permalink
feat: consider retetion time o delete
Browse files Browse the repository at this point in the history
  • Loading branch information
paulobernardoaf committed May 7, 2021
1 parent c9446bb commit 7307ef1
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,20 @@ func GetCurrentArchives(ctx context.Context, db *sqlx.DB, org Org, archiveType A

const lookupArchivesNeedingDeletion = `
SELECT id, org_id, start_date::timestamp with time zone as start_date, period, archive_type, hash, size, record_count, url, rollup_id, needs_deletion
FROM archives_archive WHERE org_id = $1 AND archive_type = $2 AND needs_deletion = TRUE
FROM archives_archive WHERE org_id = $1 AND archive_type = $2 AND needs_deletion = TRUE AND start_date BETWEEN $3 AND $4
ORDER BY start_date asc, period desc
`

// GetArchivesNeedingDeletion returns all the archives which need to be deleted
func GetArchivesNeedingDeletion(ctx context.Context, db *sqlx.DB, org Org, archiveType ArchiveType) ([]*Archive, error) {
func GetArchivesNeedingDeletion(ctx context.Context, db *sqlx.DB, org Org, archiveType ArchiveType, now time.Time) ([]*Archive, error) {
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()

archives := make([]*Archive, 0, 1)
err := db.SelectContext(ctx, &archives, lookupArchivesNeedingDeletion, org.ID, archiveType)
endDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC).AddDate(0, 0, -org.RetentionPeriod)
orgUTC := org.CreatedOn.In(time.UTC)
startDate := time.Date(orgUTC.Year(), orgUTC.Month(), orgUTC.Day(), 0, 0, 0, 0, time.UTC)
err := db.SelectContext(ctx, &archives, lookupArchivesNeedingDeletion, org.ID, archiveType, startDate, endDate)
if err != nil && err != sql.ErrNoRows {
return nil, errors.Wrapf(err, "error selecting archives needing deletion for org: %d and type: %s", org.ID, archiveType)
}
Expand Down Expand Up @@ -1550,7 +1553,7 @@ func DeleteArchivedRuns(ctx context.Context, config *Config, db *sqlx.DB, s3Clie
// DeleteArchivedOrgRecords deletes all the records for the passeg in org based on archives already created
func DeleteArchivedOrgRecords(ctx context.Context, now time.Time, config *Config, db *sqlx.DB, s3Client s3iface.S3API, org Org, archiveType ArchiveType) ([]*Archive, error) {
// get all the archives that haven't yet been deleted
archives, err := GetArchivesNeedingDeletion(ctx, db, org, archiveType)
archives, err := GetArchivesNeedingDeletion(ctx, db, org, archiveType, now)
if err != nil {
return nil, fmt.Errorf("error finding archives needing deletion '%s'", archiveType)
}
Expand Down

0 comments on commit 7307ef1

Please sign in to comment.