From 339da476360cb76358562ee1c7fc2b4de6d48b85 Mon Sep 17 00:00:00 2001 From: David Dworken Date: Mon, 19 Feb 2024 11:50:12 -0800 Subject: [PATCH] Add cleaning for integration test devices to remove DB entries --- backend/server/internal/database/db.go | 48 +++++++++++++++++++++++++- backend/server/server.go | 1 - 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/backend/server/internal/database/db.go b/backend/server/internal/database/db.go index 0735fdc..dcf87ff 100644 --- a/backend/server/internal/database/db.go +++ b/backend/server/internal/database/db.go @@ -403,6 +403,7 @@ func (db *DB) GenerateAndStoreActiveUserStats(ctx context.Context) error { func (db *DB) DeepClean(ctx context.Context) error { err := db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { + // Delete entries for users that have one device and are inactive r := tx.Exec(` CREATE TEMP TABLE temp_users_with_one_device AS ( SELECT user_id @@ -439,7 +440,8 @@ func (db *DB) DeepClean(ctx context.Context) error { if err != nil { return err } - return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { + err = db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { + // Delete entries for users that have tons and tons of entries (e.g. due to a mis-use or misconfiguration) r := tx.Exec(` CREATE TEMP TABLE users_with_too_many_entries AS ( SELECT user_id @@ -462,4 +464,48 @@ func (db *DB) DeepClean(ctx context.Context) error { fmt.Printf("Ran deep clean for overly active users and deleted %d rows\n", r.RowsAffected) return nil }) + if err != nil { + return err + } + return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { + // Delete entries for integration test users + r := tx.Exec(` + CREATE TEMP TABLE temp_inactive_devices AS ( + SELECT device_id + FROM usage_data + WHERE last_used <= (now() - INTERVAL '90 days') + ) + `) + if r.Error != nil { + return fmt.Errorf("failed to create list of inactive devices: %w", r.Error) + } + r = tx.Exec(` + CREATE TEMP TABLE temp_integration_devices AS ( + SELECT device_id + FROM devices + WHERE is_integration_test_device + ) + `) + if r.Error != nil { + return fmt.Errorf("failed to create list of integration test devices: %w", r.Error) + } + r = tx.Raw(` + DELETE FROM enc_history_entries WHERE + device_id IN (SELECT * FROM temp_inactive_devices) + AND device_id IN (SELECT * FROM temp_integration_devices) + `) + if r.Error != nil { + return fmt.Errorf("failed to delete entries for integration test devices: %w", r.Error) + } + r = tx.Raw(` + DELETE FROM devices WHERE + device_id IN (SELECT * FROM temp_inactive_devices) + AND device_id IN (SELECT * FROM temp_integration_devices) + `) + if r.Error != nil { + return fmt.Errorf("failed to delete integration test devices: %w", r.Error) + } + fmt.Printf("Ran deep clean for integration test devices and deleted %d rows\n", r.RowsAffected) + return nil + }) } diff --git a/backend/server/server.go b/backend/server/server.go index 1e88302..4bcf1ce 100644 --- a/backend/server/server.go +++ b/backend/server/server.go @@ -107,7 +107,6 @@ func OpenDB() (*database.DB, error) { var LAST_USER_STATS_RUN = time.Unix(0, 0) var LAST_DEEP_CLEAN = time.Unix(0, 0) -// TODO: Update this to delete entries from integration test devices, as long as they are inactive func cron(ctx context.Context, db *database.DB, stats *statsd.Client) error { // Determine the latest released version of hishtory to serve via the /api/v1/download // endpoint for hishtory updates.