refactor: extract data fixes into dedicated package
This commit is contained in:
@@ -5,27 +5,16 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
dbfixes "mal/internal/database/fixes"
|
||||
)
|
||||
|
||||
type dataFix struct {
|
||||
id string
|
||||
apply func(ctx context.Context, sqlDB *sql.DB) error
|
||||
}
|
||||
|
||||
var registeredDataFixes []dataFix
|
||||
|
||||
func registerDataFix(fix dataFix) {
|
||||
registeredDataFixes = append(registeredDataFixes, fix)
|
||||
}
|
||||
|
||||
func RunDataFixes(sqlDB *sql.DB) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
fixes := append([]dataFix(nil), registeredDataFixes...)
|
||||
sort.Slice(fixes, func(i, j int) bool { return fixes[i].id < fixes[j].id })
|
||||
fixes := dbfixes.All()
|
||||
|
||||
if len(fixes) == 0 {
|
||||
return nil
|
||||
@@ -41,15 +30,15 @@ func RunDataFixes(sqlDB *sql.DB) error {
|
||||
}
|
||||
|
||||
for _, fix := range fixes {
|
||||
if applied[fix.id] {
|
||||
if applied[fix.ID] {
|
||||
continue
|
||||
}
|
||||
|
||||
log.Printf("Running data fix id=%s", fix.id)
|
||||
if err := fix.apply(ctx, sqlDB); err != nil {
|
||||
return fmt.Errorf("data fix %s failed: %w", fix.id, err)
|
||||
log.Printf("Running data fix id=%s", fix.ID)
|
||||
if err := fix.Apply(ctx, sqlDB); err != nil {
|
||||
return fmt.Errorf("data fix %s failed: %w", fix.ID, err)
|
||||
}
|
||||
if err := markFixApplied(ctx, sqlDB, fix.id); err != nil {
|
||||
if err := markFixApplied(ctx, sqlDB, fix.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package database
|
||||
package fixes
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -7,9 +7,9 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
registerDataFix(dataFix{
|
||||
id: "20260526_episode_availability_backfill_next_refresh_at",
|
||||
apply: func(ctx context.Context, sqlDB *sql.DB) error {
|
||||
Register(Fix{
|
||||
ID: "20260526_episode_availability_backfill_next_refresh_at",
|
||||
Apply: func(ctx context.Context, sqlDB *sql.DB) error {
|
||||
// Old caches could have next_refresh_at NULL (especially for airing shows with missing broadcast metadata),
|
||||
// which can result in "never refresh again" behavior on the server.
|
||||
_, err := sqlDB.ExecContext(ctx, `
|
||||
@@ -25,3 +25,4 @@ WHERE next_refresh_at IS NULL;
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
25
internal/database/fixes/registry.go
Normal file
25
internal/database/fixes/registry.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package fixes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type Fix struct {
|
||||
ID string
|
||||
Apply func(ctx context.Context, sqlDB *sql.DB) error
|
||||
}
|
||||
|
||||
var registered []Fix
|
||||
|
||||
func Register(fix Fix) {
|
||||
registered = append(registered, fix)
|
||||
}
|
||||
|
||||
func All() []Fix {
|
||||
out := append([]Fix(nil), registered...)
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
|
||||
return out
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user