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_accounts ON devices_accounts.account_id = accounts.id
INNER JOIN devices ON devices.id = devices_accounts.device_id INNER JOIN devices ON devices.id = devices_accounts.device_id
WHERE grace_period_expires_at >= NOW() WHERE grace_period_expires_at >= NOW()
AND accounts.is_deleted IS FALSE
` `
rows, err := pool.Query(ctx, query) rows, err := pool.Query(ctx, query)
if err != nil { 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, last_message_id, next_notification_check_at, next_stuck_notification_check_at,
check_count check_count
FROM accounts FROM accounts
WHERE id = $1` WHERE id = $1 AND is_deleted IS FALSE`
accs, err := p.fetch(ctx, query, id) accs, err := p.fetch(ctx, query, id)
if err != nil { 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, last_message_id, next_notification_check_at, next_stuck_notification_check_at,
check_count check_count
FROM accounts FROM accounts
WHERE reddit_account_id = $1` WHERE reddit_account_id = $1 AND is_deleted IS FALSE`
accs, err := p.fetch(ctx, query, id) accs, err := p.fetch(ctx, query, id)
if err != nil { 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 { func (p *postgresAccountRepository) CreateOrUpdate(ctx context.Context, acc *domain.Account) error {
query := ` query := `
INSERT INTO accounts (username, reddit_account_id, access_token, refresh_token, token_expires_at, 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) last_message_id, next_notification_check_at, next_stuck_notification_check_at, is_deleted)
VALUES ($1, $2, $3, $4, $5, '', NOW(), NOW()) VALUES ($1, $2, $3, $4, $5, '', NOW(), NOW(), false)
ON CONFLICT(username) DO ON CONFLICT(username) DO
UPDATE SET access_token = $3, UPDATE SET access_token = $3,
refresh_token = $4, refresh_token = $4,
token_expires_at = $5 token_expires_at = $5,
is_deleted = false
RETURNING id` RETURNING id`
return p.conn.QueryRow( return p.conn.QueryRow(
@ -108,8 +109,8 @@ func (p *postgresAccountRepository) Create(ctx context.Context, acc *domain.Acco
query := ` query := `
INSERT INTO accounts INSERT INTO accounts
(username, reddit_account_id, access_token, refresh_token, token_expires_at, (username, reddit_account_id, access_token, refresh_token, token_expires_at,
last_message_id, next_notification_check_at, next_stuck_notification_check_at) last_message_id, next_notification_check_at, next_stuck_notification_check_at, is_deleted)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, false)
RETURNING id` RETURNING id`
return p.conn.QueryRow( 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 { 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) _, err := p.conn.Exec(ctx, query, id)
return err return err
} }
@ -188,14 +190,16 @@ func (p *postgresAccountRepository) GetByAPNSToken(ctx context.Context, token st
FROM accounts FROM accounts
INNER JOIN devices_accounts ON accounts.id = devices_accounts.account_id INNER JOIN devices_accounts ON accounts.id = devices_accounts.account_id
INNER JOIN devices ON devices.id = devices_accounts.device_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) return p.fetch(ctx, query, token)
} }
func (p *postgresAccountRepository) PruneStale(ctx context.Context, expiry time.Time) (int64, error) { func (p *postgresAccountRepository) PruneStale(ctx context.Context, expiry time.Time) (int64, error) {
query := ` query := `
DELETE FROM accounts UPDATE accounts
SET is_deleted = TRUE
WHERE token_expires_at < $1` WHERE token_expires_at < $1`
res, err := p.conn.Exec(ctx, query, expiry) 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 LEFT JOIN devices_accounts ON accounts.id = devices_accounts.account_id
GROUP BY accounts.id GROUP BY accounts.id
) )
DELETE FROM accounts WHERE id IN ( UPDATE accounts
SET is_deleted = TRUE
WHERE id IN (
SELECT id SELECT id
FROM accounts_with_device_count FROM accounts_with_device_count
WHERE device_count = 0 WHERE device_count = 0