implement account soft delete

This commit is contained in:
Andre Medeiros 2022-11-01 12:45:11 -04:00
parent 98cafdfaed
commit 646dc0dd34
2 changed files with 18 additions and 11 deletions

View file

@ -458,6 +458,7 @@ func enqueueAccounts(ctx context.Context, logger *zap.Logger, statsd *statsd.Cli
INNER JOIN devices_accounts ON devices_accounts.account_id = accounts.id
INNER JOIN devices ON devices.id = devices_accounts.device_id
WHERE grace_period_expires_at >= NOW()
AND accounts.is_deleted IS FALSE
`
rows, err := pool.Query(ctx, query)
if err != nil {

View file

@ -50,7 +50,7 @@ func (p *postgresAccountRepository) GetByID(ctx context.Context, id int64) (doma
last_message_id, next_notification_check_at, next_stuck_notification_check_at,
check_count
FROM accounts
WHERE id = $1`
WHERE id = $1 AND is_deleted IS FALSE`
accs, err := p.fetch(ctx, query, id)
if err != nil {
@ -69,7 +69,7 @@ func (p *postgresAccountRepository) GetByRedditID(ctx context.Context, id string
last_message_id, next_notification_check_at, next_stuck_notification_check_at,
check_count
FROM accounts
WHERE reddit_account_id = $1`
WHERE reddit_account_id = $1 AND is_deleted IS FALSE`
accs, err := p.fetch(ctx, query, id)
if err != nil {
@ -85,12 +85,13 @@ func (p *postgresAccountRepository) GetByRedditID(ctx context.Context, id string
func (p *postgresAccountRepository) CreateOrUpdate(ctx context.Context, acc *domain.Account) error {
query := `
INSERT INTO accounts (username, reddit_account_id, access_token, refresh_token, token_expires_at,
last_message_id, next_notification_check_at, next_stuck_notification_check_at)
VALUES ($1, $2, $3, $4, $5, '', NOW(), NOW())
last_message_id, next_notification_check_at, next_stuck_notification_check_at, is_deleted)
VALUES ($1, $2, $3, $4, $5, '', NOW(), NOW(), false)
ON CONFLICT(username) DO
UPDATE SET access_token = $3,
refresh_token = $4,
token_expires_at = $5
token_expires_at = $5,
is_deleted = false
RETURNING id`
return p.conn.QueryRow(
@ -108,8 +109,8 @@ func (p *postgresAccountRepository) Create(ctx context.Context, acc *domain.Acco
query := `
INSERT INTO accounts
(username, reddit_account_id, access_token, refresh_token, token_expires_at,
last_message_id, next_notification_check_at, next_stuck_notification_check_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
last_message_id, next_notification_check_at, next_stuck_notification_check_at, is_deleted)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, false)
RETURNING id`
return p.conn.QueryRow(
@ -159,7 +160,8 @@ func (p *postgresAccountRepository) Update(ctx context.Context, acc *domain.Acco
}
func (p *postgresAccountRepository) Delete(ctx context.Context, id int64) error {
query := `DELETE FROM accounts WHERE id = $1`
//query := `DELETE FROM accounts WHERE id = $1`
query := `UPDATE accounts SET is_deleted = TRUE WHERE id = $1`
_, err := p.conn.Exec(ctx, query, id)
return err
}
@ -188,14 +190,16 @@ func (p *postgresAccountRepository) GetByAPNSToken(ctx context.Context, token st
FROM accounts
INNER JOIN devices_accounts ON accounts.id = devices_accounts.account_id
INNER JOIN devices ON devices.id = devices_accounts.device_id
WHERE devices.apns_token = $1`
WHERE devices.apns_token = $1
AND accounts.is_deleted IS FALSE`
return p.fetch(ctx, query, token)
}
func (p *postgresAccountRepository) PruneStale(ctx context.Context, expiry time.Time) (int64, error) {
query := `
DELETE FROM accounts
UPDATE accounts
SET is_deleted = TRUE
WHERE token_expires_at < $1`
res, err := p.conn.Exec(ctx, query, expiry)
@ -211,7 +215,9 @@ func (p *postgresAccountRepository) PruneOrphaned(ctx context.Context) (int64, e
LEFT JOIN devices_accounts ON accounts.id = devices_accounts.account_id
GROUP BY accounts.id
)
DELETE FROM accounts WHERE id IN (
UPDATE accounts
SET is_deleted = TRUE
WHERE id IN (
SELECT id
FROM accounts_with_device_count
WHERE device_count = 0