This commit is contained in:
2026-06-30 09:31:33 +07:00
parent c736326162
commit bbf8336664
77 changed files with 12601 additions and 556 deletions

View File

@@ -3,6 +3,9 @@ package db
import (
"fmt"
"log"
"strings"
"time"
"server/internal/models"
"gorm.io/driver/mysql"
@@ -35,10 +38,110 @@ func ConnectDB(host, port, user, password, dbName string) (*gorm.DB, error) {
func AutoMigrate(db *gorm.DB) error {
log.Println("Running AutoMigrate...")
return db.AutoMigrate(
if err := migrateLegacyWifiTables(db); err != nil {
return err
}
if err := db.AutoMigrate(
&models.Class{},
&models.ClassCourseLink{},
&models.Student{},
&models.ClassStudent{},
)
&models.ClassSchedule{},
&models.ClassCourse{},
&models.ClassAllowedApp{},
&models.AppPoolEntry{},
&models.WifiPoolEntry{},
&models.AcceptedWifi{},
&models.StudentSession{},
&models.AttendanceResult{},
); err != nil {
return err
}
if err := purgeLegacyWifiWithoutBSSID(db); err != nil {
return err
}
return migrateLegacyDiscoveredApps(db)
}
// Bảng WiFi cũ dùng ssid_key làm PK; schema mới cần id + bssid_key — GORM không tự đổi PK.
func migrateLegacyWifiTables(db *gorm.DB) error {
if db.Migrator().HasTable("accepted_wifis") {
needsReset := !db.Migrator().HasColumn(&models.AcceptedWifi{}, "id") ||
!db.Migrator().HasColumn(&models.AcceptedWifi{}, "bssid_key")
if needsReset {
log.Println("Dropping legacy accepted_wifis (SSID-only schema) — will recreate with BSSID...")
if err := db.Migrator().DropTable("accepted_wifis"); err != nil {
return err
}
}
}
if db.Migrator().HasTable("wifi_pool") {
needsReset := !db.Migrator().HasColumn(&models.WifiPoolEntry{}, "bssid_key")
if needsReset {
log.Println("Dropping legacy wifi_pool (SSID-only schema) — will recreate with BSSID...")
if err := db.Migrator().DropTable("wifi_pool"); err != nil {
return err
}
}
}
return nil
}
func purgeLegacyWifiWithoutBSSID(db *gorm.DB) error {
_ = db.Where("bssid = '' OR bssid_key = '' OR LENGTH(bssid_key) <> 12").Delete(&models.AcceptedWifi{}).Error
_ = db.Where("bssid = '' OR bssid_key = '' OR LENGTH(bssid_key) <> 12").Delete(&models.WifiPoolEntry{}).Error
return nil
}
// Gộp dữ liệu cũ (theo lớp) vào kho global nếu bảng discovered_apps còn tồn tại.
func migrateLegacyDiscoveredApps(db *gorm.DB) error {
if !db.Migrator().HasTable("discovered_apps") {
return nil
}
type legacyRow struct {
ProcessName string
WindowTitle string
Keyword string
HitCount int
LastSeenAt time.Time
StudentRkID int64
ClassRkID int64
}
var rows []legacyRow
if err := db.Table("discovered_apps").Find(&rows).Error; err != nil {
return nil
}
for _, r := range rows {
pKey := strings.ToLower(strings.TrimSpace(r.ProcessName))
if pKey == "" {
continue
}
kw := strings.TrimSuffix(pKey, ".exe")
if r.Keyword != "" {
kw = r.Keyword
}
var existing models.AppPoolEntry
if err := db.Where("process_key = ?", pKey).First(&existing).Error; err == nil {
existing.HitCount += r.HitCount
if r.LastSeenAt.After(existing.LastSeenAt) {
existing.LastSeenAt = r.LastSeenAt
existing.WindowTitle = r.WindowTitle
existing.ProcessName = r.ProcessName
existing.LastStudentRkID = r.StudentRkID
existing.LastClassRkID = r.ClassRkID
}
_ = db.Save(&existing).Error
} else {
_ = db.Create(&models.AppPoolEntry{
ProcessName: r.ProcessName,
ProcessKey: pKey,
WindowTitle: r.WindowTitle,
Keyword: kw,
HitCount: r.HitCount,
LastSeenAt: r.LastSeenAt,
LastStudentRkID: r.StudentRkID,
LastClassRkID: r.ClassRkID,
}).Error
}
}
return db.Migrator().DropTable("discovered_apps")
}