Applies the planned migrations and returns the number of applied migrations.
(ctx context.Context, dir MigrationDirection, migrations []*PlannedMigration, dbMap *gorp.DbMap)
| 532 | |
| 533 | // Applies the planned migrations and returns the number of applied migrations. |
| 534 | func (MigrationSet) applyMigrations(ctx context.Context, dir MigrationDirection, migrations []*PlannedMigration, dbMap *gorp.DbMap) (int, error) { |
| 535 | applied := 0 |
| 536 | for _, migration := range migrations { |
| 537 | var executor SqlExecutor |
| 538 | var err error |
| 539 | |
| 540 | if migration.DisableTransaction { |
| 541 | executor = dbMap.WithContext(ctx) |
| 542 | } else { |
| 543 | e, err := dbMap.Begin() |
| 544 | if err != nil { |
| 545 | return applied, newTxError(migration, err) |
| 546 | } |
| 547 | executor = e.WithContext(ctx) |
| 548 | } |
| 549 | |
| 550 | for _, stmt := range migration.Queries { |
| 551 | // remove the semicolon from stmt, fix ORA-00922 issue in database oracle |
| 552 | stmt = strings.TrimSuffix(stmt, "\n") |
| 553 | stmt = strings.TrimSuffix(stmt, " ") |
| 554 | stmt = strings.TrimSuffix(stmt, ";") |
| 555 | if _, err := executor.Exec(stmt); err != nil { |
| 556 | if trans, ok := executor.(*gorp.Transaction); ok { |
| 557 | _ = trans.Rollback() |
| 558 | } |
| 559 | |
| 560 | return applied, newTxError(migration, err) |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | switch dir { |
| 565 | case Up: |
| 566 | err = executor.Insert(&MigrationRecord{ |
| 567 | Id: migration.Id, |
| 568 | AppliedAt: time.Now(), |
| 569 | }) |
| 570 | if err != nil { |
| 571 | if trans, ok := executor.(*gorp.Transaction); ok { |
| 572 | _ = trans.Rollback() |
| 573 | } |
| 574 | |
| 575 | return applied, newTxError(migration, err) |
| 576 | } |
| 577 | case Down: |
| 578 | _, err := executor.Delete(&MigrationRecord{ |
| 579 | Id: migration.Id, |
| 580 | }) |
| 581 | if err != nil { |
| 582 | if trans, ok := executor.(*gorp.Transaction); ok { |
| 583 | _ = trans.Rollback() |
| 584 | } |
| 585 | |
| 586 | return applied, newTxError(migration, err) |
| 587 | } |
| 588 | default: |
| 589 | panic("Not possible") |
| 590 | } |
| 591 |
no test coverage detected