From db6d8413a74d1fe624e431243bb0808bee8ab01f Mon Sep 17 00:00:00 2001 From: David Dworken Date: Mon, 20 Jan 2025 17:21:49 -0800 Subject: [PATCH] Add explicit DROP TABLE directives for temp_inactive_devices and drop the TEMP qualifier for non-postgres DBs, fixes #288 --- backend/server/internal/database/db.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/backend/server/internal/database/db.go b/backend/server/internal/database/db.go index 096b49d..208f1a8 100644 --- a/backend/server/internal/database/db.go +++ b/backend/server/internal/database/db.go @@ -430,9 +430,16 @@ func (db *DB) GenerateAndStoreActiveUserStats(ctx context.Context) error { func (db *DB) SelfHostedDeepClean(ctx context.Context) error { return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { + supportsTempTable := db.Name() == "postgres" + tmpDirective := "" + if supportsTempTable { + tmpDirective = "TEMP" + } runDeletes := os.Getenv("HISHTORY_SELF_HOSTED_DEEP_CLEAN") != "" + _ = tx.Exec(`DROP TABLE IF EXISTS temp_inactive_devices`) + defer tx.Exec(`DROP TABLE IF EXISTS temp_inactive_devices`) r := tx.Exec(` - CREATE TEMP TABLE temp_inactive_devices AS ( + CREATE ` + tmpDirective + ` TABLE temp_inactive_devices AS ( SELECT device_id FROM usage_data WHERE last_used <= (now() - INTERVAL '90 days')