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

Method planMigrationCommon

migrate.go:625–720  ·  view source on GitHub ↗

A common method to plan a migration.

(db *sql.DB, dialect string, m MigrationSource, dir MigrationDirection, max int, version int64)

Source from the content-addressed store, hash-verified

623
624// A common method to plan a migration.
625func (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

Callers 2

PlanMigrationMethod · 0.95

Calls 9

getMigrationDbMapMethod · 0.95
getTableNameMethod · 0.95
byIdTypeAlias · 0.85
newPlanErrorFunction · 0.85
ToCatchupFunction · 0.85
ToApplyFunction · 0.85
VersionIntMethod · 0.80
FindMigrationsMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected