(ctx context.Context, db *sql.DB, rb rollbackEntry)
| 345 | } |
| 346 | |
| 347 | func applyRollback(ctx context.Context, db *sql.DB, rb rollbackEntry) error { |
| 348 | tx, err := db.BeginTx(ctx, nil) |
| 349 | if err != nil { |
| 350 | return xerrors.Errorf("begin: %w", err) |
| 351 | } |
| 352 | defer func() { _ = tx.Rollback() }() |
| 353 | |
| 354 | if _, err := tx.ExecContext(ctx, rb.downSQL); err != nil { |
| 355 | return xerrors.Errorf("execute down SQL: %w", err) |
| 356 | } |
| 357 | |
| 358 | targetVersion := rb.version - 1 |
| 359 | if _, err := tx.ExecContext(ctx, `TRUNCATE schema_migrations`); err != nil { |
| 360 | return xerrors.Errorf("truncate schema_migrations: %w", err) |
| 361 | } |
| 362 | if targetVersion >= 0 { |
| 363 | if _, err := tx.ExecContext(ctx, |
| 364 | `INSERT INTO schema_migrations (version, dirty) VALUES ($1, $2)`, |
| 365 | targetVersion, false); err != nil { |
| 366 | return xerrors.Errorf("set version: %w", err) |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | if _, err := tx.ExecContext(ctx, |
| 371 | `DELETE FROM _develop.applied_migrations WHERE version = $1`, |
| 372 | rb.version); err != nil { |
| 373 | return xerrors.Errorf("remove tracking entry: %w", err) |
| 374 | } |
| 375 | |
| 376 | return tx.Commit() |
| 377 | } |
| 378 | |
| 379 | // captureDownSQL scans migration files on disk and stores both |
| 380 | // up and down SQL content in the tracking table for versions |
no test coverage detected