MCPcopy
hub / github.com/rubenv/sql-migrate / applyMigrations

Method applyMigrations

migrate.go:534–602  ·  view source on GitHub ↗

Applies the planned migrations and returns the number of applied migrations.

(ctx context.Context, dir MigrationDirection, migrations []*PlannedMigration, dbMap *gorp.DbMap)

Source from the content-addressed store, hash-verified

532
533// Applies the planned migrations and returns the number of applied migrations.
534func (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

Callers 2

ExecMaxContextMethod · 0.95
ExecVersionContextMethod · 0.95

Calls 4

ExecMethod · 0.95
InsertMethod · 0.95
DeleteMethod · 0.95
newTxErrorFunction · 0.85

Tested by

no test coverage detected