feat: make database migration testable and backfill assets
This commit is contained in:
@@ -15,18 +15,22 @@ func main() {
|
||||
_ = os.MkdirAll(filepath.Dir(dbPath), 0755)
|
||||
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||
must(err)
|
||||
must(db.AutoMigrate(&Task{}, &MediaAsset{}, &Setting{}, &User{}, &UserPermission{}, &CreditAccount{}, &CreditTransaction{}, &CreditReservation{}, &RedeemBatch{}, &RedeemCode{}, &RedeemRecord{}, &WebDAVConfig{}, &Session{}, &AuditLog{}))
|
||||
// Replace legacy global task/reservation uniqueness constraints with scoped indexes.
|
||||
must(db.Exec("DROP INDEX IF EXISTS idx_tasks_source_rid_quality").Error)
|
||||
must(db.Exec("CREATE UNIQUE INDEX IF NOT EXISTS idx_tasks_user_source_rid_quality ON tasks(user_id, source, r_id, quality)").Error)
|
||||
must(db.Exec("DROP INDEX IF EXISTS idx_credit_reservations_task_id").Error)
|
||||
must(db.Exec("CREATE INDEX IF NOT EXISTS idx_credit_reservations_task_id ON credit_reservations(task_id)").Error)
|
||||
must(migrateDatabase(db))
|
||||
|
||||
client := &http.Client{Timeout: 2 * 60 * 1e9}
|
||||
cfg := loadConfig(db)
|
||||
a := &App{db: db, cfg: cfg, queue: make(chan uint, 100), httpClient: client}
|
||||
a.loadSettings()
|
||||
a.bootstrapAdmin()
|
||||
must(backfillMediaAssets(db, a.cfg.DownloadDir))
|
||||
if os.Getenv("MIGRATION_CHECK") == "1" {
|
||||
var users, tasks, assets int64
|
||||
db.Model(&User{}).Count(&users)
|
||||
db.Model(&Task{}).Count(&tasks)
|
||||
db.Model(&MediaAsset{}).Count(&assets)
|
||||
println("migration check ok", "users", users, "tasks", tasks, "assets", assets)
|
||||
return
|
||||
}
|
||||
normalizeConfig(&a.cfg)
|
||||
a.am = &AMAPIClient{BaseURL: a.cfg.AMAPI, HTTP: client}
|
||||
if a.cfg.WebDAVURL != "" {
|
||||
|
||||
Reference in New Issue
Block a user