Skip to content

Commit

Permalink
Add cleaning for integration test devices to remove DB entries
Browse files Browse the repository at this point in the history
  • Loading branch information
ddworken committed Feb 19, 2024
1 parent 3baba33 commit 339da47
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
48 changes: 47 additions & 1 deletion backend/server/internal/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
})
}
1 change: 0 additions & 1 deletion backend/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 339da47

Please sign in to comment.