refactor: extract data fixes into dedicated package
This commit is contained in:
@@ -5,27 +5,16 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"sort"
|
|
||||||
"time"
|
"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 {
|
func RunDataFixes(sqlDB *sql.DB) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
fixes := append([]dataFix(nil), registeredDataFixes...)
|
fixes := dbfixes.All()
|
||||||
sort.Slice(fixes, func(i, j int) bool { return fixes[i].id < fixes[j].id })
|
|
||||||
|
|
||||||
if len(fixes) == 0 {
|
if len(fixes) == 0 {
|
||||||
return nil
|
return nil
|
||||||
@@ -41,15 +30,15 @@ func RunDataFixes(sqlDB *sql.DB) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, fix := range fixes {
|
for _, fix := range fixes {
|
||||||
if applied[fix.id] {
|
if applied[fix.ID] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Running data fix id=%s", fix.id)
|
log.Printf("Running data fix id=%s", fix.ID)
|
||||||
if err := fix.apply(ctx, sqlDB); err != nil {
|
if err := fix.Apply(ctx, sqlDB); err != nil {
|
||||||
return fmt.Errorf("data fix %s failed: %w", fix.id, err)
|
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
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package database
|
package fixes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -7,9 +7,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
registerDataFix(dataFix{
|
Register(Fix{
|
||||||
id: "20260526_episode_availability_backfill_next_refresh_at",
|
ID: "20260526_episode_availability_backfill_next_refresh_at",
|
||||||
apply: func(ctx context.Context, sqlDB *sql.DB) error {
|
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),
|
// 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.
|
// which can result in "never refresh again" behavior on the server.
|
||||||
_, err := sqlDB.ExecContext(ctx, `
|
_, 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
|
||||||
|
}
|
||||||
|
|
||||||
@@ -32,8 +32,8 @@ async function main(): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const id = `${formatYYYYMMDD(new Date())}_${slug}`;
|
const id = `${formatYYYYMMDD(new Date())}_${slug}`;
|
||||||
const dir = path.join(process.cwd(), 'internal', 'database');
|
const dir = path.join(process.cwd(), 'internal', 'database', 'fixes');
|
||||||
const filePath = path.join(dir, `fix_${id}.go`);
|
const filePath = path.join(dir, `${id}.go`);
|
||||||
|
|
||||||
await mkdir(dir, { recursive: true });
|
await mkdir(dir, { recursive: true });
|
||||||
|
|
||||||
@@ -41,7 +41,7 @@ async function main(): Promise<void> {
|
|||||||
throw new Error(`data fix already exists: ${filePath}`);
|
throw new Error(`data fix already exists: ${filePath}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const contents = `package database
|
const contents = `package fixes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -50,9 +50,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
registerDataFix(dataFix{
|
Register(Fix{
|
||||||
id: "${id}",
|
ID: "${id}",
|
||||||
apply: func(ctx context.Context, sqlDB *sql.DB) error {
|
Apply: func(ctx context.Context, sqlDB *sql.DB) error {
|
||||||
// TODO: implement fix
|
// TODO: implement fix
|
||||||
// _, err := sqlDB.ExecContext(ctx, \`UPDATE ...\`)
|
// _, err := sqlDB.ExecContext(ctx, \`UPDATE ...\`)
|
||||||
// if err != nil { return fmt.Errorf("...: %w", err) }
|
// if err != nil { return fmt.Errorf("...: %w", err) }
|
||||||
|
|||||||
Reference in New Issue
Block a user