Squash migrations from version v into a new migration. Returns a list of migrations that are squashed: vs the squashed metadata for all UP steps: um the squashed SQL for all UP steps: us the squashed metadata for all down steps: dm the squashed SQL for all down steps: ds
(v1 uint64, v2 int64)
| 474 | // the squashed metadata for all down steps: dm |
| 475 | // the squashed SQL for all down steps: ds |
| 476 | func (m *Migrate) Squash(v1 uint64, v2 int64) (vs []int64, us []byte, ds []byte, err error) { |
| 477 | var op herrors.Op = "migrate.Migrate.Squash" |
| 478 | // check the migration mode on the database |
| 479 | mode, err := m.databaseDrv.GetSetting("migration_mode") |
| 480 | if err != nil { |
| 481 | return vs, us, ds, herrors.E(op, err) |
| 482 | } |
| 483 | |
| 484 | // if migration_mode is false, set err to ErrNoMigrationMode and return |
| 485 | if mode != "true" { |
| 486 | return vs, us, ds, herrors.E(op, ErrNoMigrationMode) |
| 487 | } |
| 488 | |
| 489 | // concurrently squash all the up migrations |
| 490 | // read all up migrations from source and send each migration |
| 491 | // to the returned channel |
| 492 | retUp := make(chan interface{}, m.PrefetchMigrations) |
| 493 | go m.squashUp(v1, v2, retUp) |
| 494 | |
| 495 | // concurrently squash all down migrations |
| 496 | // read all down migrations from source and send each migration |
| 497 | // to the returned channel |
| 498 | retDown := make(chan interface{}, m.PrefetchMigrations) |
| 499 | go m.squashDown(v1, v2, retDown) |
| 500 | |
| 501 | // combine squashed up and down migrations into a single one when they're ready |
| 502 | dataUp := make(chan interface{}, m.PrefetchMigrations) |
| 503 | dataDown := make(chan interface{}, m.PrefetchMigrations) |
| 504 | retVersions := make(chan int64, m.PrefetchMigrations) |
| 505 | go func() { |
| 506 | if err := m.squashMigrations(retUp, retDown, dataUp, dataDown, retVersions); err != nil { |
| 507 | m.Logger.Error(err) |
| 508 | } |
| 509 | }() |
| 510 | |
| 511 | // make a chan for errors |
| 512 | errChn := make(chan error, 2) |
| 513 | |
| 514 | // create a waitgroup to wait for all goroutines to finish execution |
| 515 | var wg sync.WaitGroup |
| 516 | // add three tasks to waitgroup since we used 3 goroutines above |
| 517 | wg.Add(3) |
| 518 | |
| 519 | // read from dataUp chan when all up migrations are squashed and compiled |
| 520 | go func() { |
| 521 | // defer to mark one task in the waitgroup as complete |
| 522 | defer wg.Done() |
| 523 | |
| 524 | buf := &bytes.Buffer{} |
| 525 | for r := range dataUp { |
| 526 | // check the type of value returned through the chan |
| 527 | switch data := r.(type) { |
| 528 | case error: |
| 529 | // it's an error, set error and return |
| 530 | // note: this return is returning the goroutine, not the current function |
| 531 | m.isGracefulStop = true |
| 532 | errChn <- r.(error) |
| 533 | return |
nothing calls this directly
no test coverage detected