A common method to plan a migration.
(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int, version int64)
| 623 | |
| 624 | // A common method to plan a migration. |
| 625 | func (ms MigrationSet) planMigrationCommon(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int, version int64) ([]*PlannedMigration, *gorp.DbMap, error) { |
| 626 | dbMap, err := ms.getMigrationDbMap(db, dialect) |
| 627 | if err != nil { |
| 628 | return nil, nil, err |
| 629 | } |
| 630 | |
| 631 | migrations, err := m.FindMigrations() |
| 632 | if err != nil { |
| 633 | return nil, nil, err |
| 634 | } |
| 635 | |
| 636 | var migrationRecords []MigrationRecord |
| 637 | _, err = dbMap.Select(&migrationRecords, fmt.Sprintf("SELECT * FROM %s", dbMap.Dialect.QuotedTableForQuery(ms.SchemaName, ms.getTableName()))) |
| 638 | if err != nil { |
| 639 | return nil, nil, err |
| 640 | } |
| 641 | |
| 642 | // Sort migrations that have been run by Id. |
| 643 | existingMigrations := make([]*Migration, 0, len(migrationRecords)) |
| 644 | for _, migrationRecord := range migrationRecords { |
| 645 | existingMigrations = append(existingMigrations, &Migration{ |
| 646 | Id: migrationRecord.Id, |
| 647 | }) |
| 648 | } |
| 649 | sort.Sort(byId(existingMigrations)) |
| 650 | |
| 651 | // Make sure all migrations in the database are among the found migrations which |
| 652 | // are to be applied. |
| 653 | if !ms.IgnoreUnknown { |
| 654 | migrationsSearch := make(map[string]struct{}) |
| 655 | for _, migration := range migrations { |
| 656 | migrationsSearch[migration.Id] = struct{}{} |
| 657 | } |
| 658 | for _, existingMigration := range existingMigrations { |
| 659 | if _, ok := migrationsSearch[existingMigration.Id]; !ok { |
| 660 | return nil, nil, newPlanError(existingMigration, "unknown migration in database") |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | // Get last migration that was run |
| 666 | record := &Migration{} |
| 667 | if len(existingMigrations) > 0 { |
| 668 | record = existingMigrations[len(existingMigrations)-1] |
| 669 | } |
| 670 | |
| 671 | result := make([]*PlannedMigration, 0) |
| 672 | |
| 673 | // Add missing migrations up to the last run migration. |
| 674 | // This can happen for example when merges happened. |
| 675 | if len(existingMigrations) > 0 { |
| 676 | result = append(result, ToCatchup(migrations, existingMigrations, record)...) |
| 677 | } |
| 678 | |
| 679 | // Figure out which migrations to apply |
| 680 | toApply := ToApply(migrations, record.Id, dir) |
| 681 | toApplyCount := len(toApply) |
| 682 |
no test coverage detected