delete devices heuristic fix

This commit is contained in:
Andre Medeiros 2021-09-25 14:56:14 -04:00
parent 65202112f9
commit a26b1f6a32
2 changed files with 3 additions and 10 deletions

View file

@ -146,10 +146,10 @@ func pruneAccounts(ctx context.Context, logger *logrus.Logger, pool *pgxpool.Poo
} }
func pruneDevices(ctx context.Context, logger *logrus.Logger, pool *pgxpool.Pool) { func pruneDevices(ctx context.Context, logger *logrus.Logger, pool *pgxpool.Pool) {
before := time.Now().Unix() - staleDeviceThreshold threshold := time.Now().Unix()
dr := repository.NewPostgresDevice(pool) dr := repository.NewPostgresDevice(pool)
count, err := dr.PruneStale(ctx, before) count, err := dr.PruneStale(ctx, threshold)
if err != nil { if err != nil {
logger.WithFields(logrus.Fields{ logger.WithFields(logrus.Fields{
"err": err, "err": err,

View file

@ -143,14 +143,7 @@ func (p *postgresDeviceRepository) Delete(ctx context.Context, token string) err
} }
func (p *postgresDeviceRepository) PruneStale(ctx context.Context, before int64) (int64, error) { func (p *postgresDeviceRepository) PruneStale(ctx context.Context, before int64) (int64, error) {
query := ` query := `DELETE FROM devices WHERE active_until < $1`
WITH deleted_devices AS (
DELETE FROM devices
WHERE active_until < $1
RETURNING id
)
DELETE FROM devices_accounts
WHERE device_id IN (SELECT id FROM deleted_devices)`
res, err := p.pool.Exec(ctx, query, before) res, err := p.pool.Exec(ctx, query, before)