From 646dc0dd34694b70166e7ffbcc27c6cac1941384 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Tue, 1 Nov 2022 12:45:11 -0400 Subject: [PATCH] implement account soft delete --- internal/cmd/scheduler.go | 1 + internal/repository/postgres_account.go | 28 +++++++++++++++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/internal/cmd/scheduler.go b/internal/cmd/scheduler.go index c4f8d8b..7129a0f 100644 --- a/internal/cmd/scheduler.go +++ b/internal/cmd/scheduler.go @@ -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 { diff --git a/internal/repository/postgres_account.go b/internal/repository/postgres_account.go index 6e938ba..4665684 100644 --- a/internal/repository/postgres_account.go +++ b/internal/repository/postgres_account.go @@ -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