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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user