示例#1
0
func runMigrationsUpTo(b backend.Backend, targetVersion int64) (bool, error) {
	migrationsRun := 0

	currentVersion := b.CurrentSchemaVersion()

	for i := int64(1); i <= latestMigrationVersion; i++ {
		m := availableMigrations[i]

		// skip migrations we've already run
		if currentVersion >= m.Version {
			continue
		}

		// run migrations up to and through our target version.
		if m.Version > targetVersion {
			break
		}

		if err := m.Run(b); err != nil {
			return false, err
		}

		migrationsRun++
	}

	if migrationsRun == 0 {
		fmt.Println("No migrations needed to be run.")
	} else {
		fmt.Println("All migrations ran successfully.")
	}

	return false, nil
}
示例#2
0
// Run executes the migration using a supplied backend.Backend
func (m *Migration) Run(b backend.Backend) error {
	fmt.Printf("Running migration #%d against %s backend\n", m.Version, b.Name())

	if err := m.runner(b); err != nil {
		return errored.Errorf("Encountered error during migration %d", m.Version).Combine(err)
	}

	if err := b.UpdateSchemaVersion(m.Version); err != nil {
		return errored.Errorf("Successfully applied migration but failed to update schema version key").Combine(err)
	}

	fmt.Println("")

	return nil
}