diff --git a/appview/database/migrations.go b/appview/database/migrations.go index 9c8599a..a0a9065 100644 --- a/appview/database/migrations.go +++ b/appview/database/migrations.go @@ -2,11 +2,46 @@ package database import ( "fmt" + "log/slog" "gorm.io/gorm" ) +// fixColumnNames renames misnamed columns created by GORM's default snake_case +// conversion of acronym field names (e.g. DID → d_id instead of did). +// Safe to run repeatedly — skips tables/columns that don't need fixing. +func fixColumnNames(db *gorm.DB) { + renames := []struct { + table, from, to string + }{ + {"subscriptions", "d_id", "did"}, + {"comments", "d_id", "did"}, + {"comments", "a_t_u_r_i", "at_uri"}, + {"recommendations", "d_id", "did"}, + {"podcast_lists", "d_id", "did"}, + {"bookmarks", "d_id", "did"}, + {"profiles", "d_id", "did"}, + {"episode_states", "d_id", "did"}, + {"blocks", "d_id", "did"}, + {"blocks", "a_t_u_r_i", "at_uri"}, + {"blocks", "subject_d_i_d", "subject_did"}, + {"reports", "d_id", "did"}, + } + + for _, r := range renames { + if db.Migrator().HasColumn(r.table, r.from) { + if err := db.Exec(fmt.Sprintf(`ALTER TABLE %q RENAME COLUMN %q TO %q`, r.table, r.from, r.to)).Error; err != nil { + slog.Warn("column rename failed (may already be correct)", "table", r.table, "from", r.from, "to", r.to, "err", err) + } else { + slog.Info("renamed column", "table", r.table, "from", r.from, "to", r.to) + } + } + } +} + func AutoMigrateAll(db *gorm.DB) error { + fixColumnNames(db) + if err := db.AutoMigrate( &FirehoseCursor{}, &Subscription{},