fix: migration runner idempotente e seeds com UNIQUE constraint

Runner agora checa schema_migrations antes de rodar cada SQL — sem
reexecução de seeds a cada startup. Quests: UNIQUE(title, quest_type).
Cosmetics: UNIQUE(name), tipos corrigidos para hair/shirt/pants/shoes.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
2026-05-26 22:01:48 -03:00
co-authored by Claude Sonnet 4.6
parent c1964ea036
commit 2277f4375d
3 changed files with 29 additions and 12 deletions
+17 -1
View File
@@ -33,10 +33,26 @@ var m007 string
var m008 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}
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", i+1, err)
return fmt.Errorf("migration %03d: %w", version, err)
}
}
return nil