44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
func Cleanup(db *sql.DB, starID int64, full bool) error {
|
|
if starID != LoadtestStarID {
|
|
return errors.New("safety: cleanup only accepts loadtest star_id 999900")
|
|
}
|
|
|
|
queries := []string{
|
|
"DELETE FROM asset_likes WHERE star_id = $1",
|
|
"DELETE FROM exhibitions USING fan_profiles fp WHERE exhibitions.host_profile_id = fp.id AND fp.star_id = $1",
|
|
"DELETE FROM booth_slots WHERE star_id = $1",
|
|
"DELETE FROM mint_orders WHERE star_id = $1",
|
|
"DELETE FROM crystal_transaction_records WHERE star_id = $1",
|
|
"DELETE FROM friendships WHERE star_id = $1",
|
|
"DELETE FROM assets WHERE star_id = $1",
|
|
"DELETE FROM fan_profiles WHERE star_id = $1",
|
|
}
|
|
if full {
|
|
queries = append(queries,
|
|
"DELETE FROM users WHERE id BETWEEN $1 AND $2",
|
|
"DELETE FROM stars WHERE star_id = $1",
|
|
)
|
|
}
|
|
for _, q := range queries {
|
|
var err error
|
|
switch q {
|
|
case "DELETE FROM users WHERE id BETWEEN $1 AND $2":
|
|
_, err = db.Exec(q, LoadtestUserMin, LoadtestUserMax)
|
|
default:
|
|
_, err = db.Exec(q, starID)
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("cleanup %q: %w", q[:30], err)
|
|
}
|
|
}
|
|
return ResetSequences(db)
|
|
}
|