Files
carvalho-finances/apps/api/internal/migration/migration.go
T
Mlcavalho1andClaude Sonnet 4.6 25156c36e1 fix: registrar migrations 009-011 e gravar schema_migrations após apply
As migrations de receitas recorrentes, CDI yield e cartão de crédito
foram criadas mas não embedadas no runner — causando 500 em produção.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
2026-05-27 15:22:22 -03:00

72 lines
1.7 KiB
Go

package migration
import (
"context"
_ "embed"
"fmt"
"github.com/jackc/pgx/v5/pgxpool"
)
//go:embed sql/001_initial.sql
var m001 string
//go:embed sql/002_add_external_id.sql
var m002 string
//go:embed sql/003_recurring_ignores.sql
var m003 string
//go:embed sql/004_accounts.sql
var m004 string
//go:embed sql/005_game_profile.sql
var m005 string
//go:embed sql/006_quests.sql
var m006 string
//go:embed sql/007_achievements.sql
var m007 string
//go:embed sql/008_cosmetics.sql
var m008 string
//go:embed sql/009_recurring_income.sql
var m009 string
//go:embed sql/010_cdi_yield.sql
var m010 string
//go:embed sql/011_credit_bills.sql
var m011 string
func Run(ctx context.Context, pool *pgxpool.Pool) error {
// Bootstrap: ensure schema_migrations table exists before checking versions.
if _, err := pool.Exec(ctx, `
CREATE TABLE IF NOT EXISTS schema_migrations (
version INTEGER PRIMARY KEY,
applied_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
)
`); err != nil {
return fmt.Errorf("bootstrap schema_migrations: %w", err)
}
migrations := []string{m001, m002, m003, m004, m005, m006, m007, m008, m009, m010, m011}
for i, sql := range migrations {
version := i + 1
var applied bool
_ = pool.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM schema_migrations WHERE version=$1)`, version).Scan(&applied)
if applied {
continue
}
if _, err := pool.Exec(ctx, sql); err != nil {
return fmt.Errorf("migration %03d: %w", version, err)
}
if _, err := pool.Exec(ctx, `INSERT INTO schema_migrations (version) VALUES ($1) ON CONFLICT DO NOTHING`, version); err != nil {
return fmt.Errorf("migration %03d mark applied: %w", version, err)
}
}
return nil
}