Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Commit

Permalink
added db cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
hexahigh committed Feb 27, 2024
1 parent 7103c65 commit e096334
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
dbHost = flag.String("db:host", "localhost:3306", "Database host (Unused for sqlite)")
dbDb = flag.String("db:db", "yapc", "Database name (Unused for sqlite)")
dbFile = flag.String("db:file", "./data/yapc.db", "SQLite database file")
cleanDb = flag.Bool("cleandb", false, "Clean the database")
)

var downloadSpeeds []float64
Expand Down Expand Up @@ -71,6 +72,11 @@ func main() {
log.Fatalf("Invalid database type: %s", *dbType)
os.Exit(1)
}

if *cleanDb {
dbClean()
}

onStart()
initDB()

Expand Down Expand Up @@ -656,3 +662,45 @@ func handlePing(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("pong"))
}

func dbClean() {
log.Println("Cleaning database")

log.Println("Looking for missing files...")
// Query the database to get all IDs
rows, err := db.Query("SELECT id FROM data")
if err != nil {
log.Fatalf("Failed to query database: %v", err)
}
defer rows.Close()

// Iterate over the rows
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
log.Fatalf("Failed to scan row: %v", err)
}

// Construct the file path
filePath := filepath.Join(*dataDir, id)
if *compress {
filePath += ".zst"
}

// Check if the file exists
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
// If the file does not exist, delete the entry from the database
_, err := db.Exec("DELETE FROM data WHERE id = ?", id)
if err != nil {
log.Printf("Failed to delete entry with ID %s: %v", id, err)
} else {
log.Printf("Deleted entry with ID %s because the file does not exist", id)
}
}
}

if err := rows.Err(); err != nil {
log.Fatalf("Error iterating over rows: %v", err)
}
}

0 comments on commit e096334

Please sign in to comment.