mirror of
https://github.com/skidoodle/ncore-stats.git
synced 2026-04-27 23:37:36 +02:00
yay
This commit is contained in:
+94
-23
@@ -4,6 +4,7 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -18,7 +19,12 @@ func initDB(cfg *Configuration) *sql.DB {
|
||||
db.SetMaxOpenConns(1)
|
||||
|
||||
schemas := []string{
|
||||
`CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, display_name TEXT UNIQUE, profile_id TEXT);`,
|
||||
`CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
display_name TEXT UNIQUE,
|
||||
profile_id TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);`,
|
||||
`CREATE TABLE IF NOT EXISTS profile_history (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER,
|
||||
@@ -46,26 +52,19 @@ func initDB(cfg *Configuration) *sql.DB {
|
||||
}
|
||||
|
||||
func migrate(db *sql.DB) {
|
||||
var columnExists bool
|
||||
_ = db.QueryRow("SELECT COUNT(*) FROM pragma_table_info('profile_history') WHERE name='upload_bytes'").Scan(&columnExists)
|
||||
if !columnExists {
|
||||
var ubExists bool
|
||||
_ = db.QueryRow("SELECT COUNT(*) FROM pragma_table_info('profile_history') WHERE name='upload_bytes'").Scan(&ubExists)
|
||||
if !ubExists {
|
||||
logrus.Info("Migrating: Adding upload_bytes column...")
|
||||
_, err := db.Exec("ALTER TABLE profile_history ADD COLUMN upload_bytes INTEGER")
|
||||
if err != nil {
|
||||
logrus.Errorf("Migration failed (add column): %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
logrus.Info("Migrating: Backfilling upload_bytes from existing strings...")
|
||||
_, _ = db.Exec("ALTER TABLE profile_history ADD COLUMN upload_bytes INTEGER")
|
||||
|
||||
type updateRow struct {
|
||||
id int64
|
||||
bytes int64
|
||||
}
|
||||
var updates []updateRow
|
||||
|
||||
rows, err := db.Query("SELECT id, upload FROM profile_history WHERE upload_bytes IS NULL")
|
||||
if err == nil {
|
||||
rows, _ := db.Query("SELECT id, upload FROM profile_history WHERE upload_bytes IS NULL")
|
||||
if rows != nil {
|
||||
for rows.Next() {
|
||||
var id int64
|
||||
var upload string
|
||||
@@ -75,24 +74,96 @@ func migrate(db *sql.DB) {
|
||||
}
|
||||
rows.Close()
|
||||
}
|
||||
|
||||
if len(updates) > 0 {
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
logrus.Errorf("Transaction start failed: %v", err)
|
||||
return
|
||||
}
|
||||
tx, _ := db.Begin()
|
||||
stmt, _ := tx.Prepare("UPDATE profile_history SET upload_bytes = ? WHERE id = ?")
|
||||
for _, up := range updates {
|
||||
_, _ = stmt.Exec(up.bytes, up.id)
|
||||
}
|
||||
stmt.Close()
|
||||
if err := tx.Commit(); err != nil {
|
||||
logrus.Errorf("Transaction commit failed: %v", err)
|
||||
_ = tx.Commit()
|
||||
}
|
||||
}
|
||||
var caExists bool
|
||||
_ = db.QueryRow("SELECT COUNT(*) FROM pragma_table_info('users') WHERE name='created_at'").Scan(&caExists)
|
||||
if !caExists {
|
||||
logrus.Info("Migrating: Adding created_at column to users...")
|
||||
_, err := db.Exec("ALTER TABLE users ADD COLUMN created_at DATETIME")
|
||||
if err != nil {
|
||||
logrus.Errorf("User migration failed (add column): %v", err)
|
||||
} else {
|
||||
_, _ = db.Exec("UPDATE users SET created_at = CURRENT_TIMESTAMP WHERE created_at IS NULL")
|
||||
logrus.Info("User migration complete.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *State) syncUsers() {
|
||||
if _, err := os.Stat(s.config.UsersPath); os.IsNotExist(err) {
|
||||
logrus.Warnf("Users config file not found at %s, skipping sync", s.config.UsersPath)
|
||||
return
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(s.config.UsersPath)
|
||||
if err != nil {
|
||||
logrus.Errorf("Failed to read users file: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
type userEntry struct {
|
||||
Name string
|
||||
ID string
|
||||
}
|
||||
var users []userEntry
|
||||
|
||||
lines := strings.Split(string(content), "\n")
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
parts := strings.SplitN(line, ":", 2)
|
||||
if len(parts) == 2 {
|
||||
users = append(users, userEntry{
|
||||
Name: strings.TrimSpace(parts[0]),
|
||||
ID: strings.TrimSpace(parts[1]),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
for _, u := range users {
|
||||
_, err := s.db.Exec(`
|
||||
INSERT INTO users (display_name, profile_id)
|
||||
VALUES (?, ?)
|
||||
ON CONFLICT(display_name) DO UPDATE SET profile_id = excluded.profile_id`,
|
||||
u.Name, u.ID)
|
||||
if err != nil {
|
||||
logrus.Errorf("Sync failed for %s: %v", u.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
rows, _ := s.db.Query("SELECT display_name FROM users")
|
||||
if rows != nil {
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var name string
|
||||
rows.Scan(&name)
|
||||
|
||||
found := false
|
||||
for _, u := range users {
|
||||
if u.Name == name {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
logrus.Infof("Sync: Removing %s (not in config)", name)
|
||||
_, _ = s.db.Exec("DELETE FROM users WHERE display_name = ?", name)
|
||||
}
|
||||
}
|
||||
logrus.Info("Migration complete.")
|
||||
}
|
||||
logrus.Infof("User synchronization complete (%d users)", len(users))
|
||||
}
|
||||
|
||||
func (s *State) getLatest() ([]ProfileData, error) {
|
||||
|
||||
Reference in New Issue
Block a user