func Migrate(db *sql.DB) error {
	var err error

	if db == nil {
		return errs.Errorf("Database handle is nil\n")
	}

	m := getMigrations()

	var tx *sql.Tx
	for nextVersion := int64(0); nextVersion < int64(len(m)) && err == nil; nextVersion++ {

		tx, err = db.Begin()
		if err != nil {
			return errs.Errorf("Failed to start transaction: %s\n", err)
		}

		err = migrateToNextVersion(tx, &nextVersion, m)

		if err != nil {
			oldErr := err
			log.Info(nil, map[string]interface{}{
				"next_version": nextVersion,
				"migrations":   m,
				"err":          err,
			}, "Rolling back transaction due to: %v", err)

			if err = tx.Rollback(); err != nil {
				log.Error(nil, map[string]interface{}{
					"next_version": nextVersion,
					"migrations":   m,
					"err":          err,
				}, "error while rolling back transaction: ", err)
				return errs.Errorf("Error while rolling back transaction: %s\n", err)
			}
			return oldErr
		}

		if err = tx.Commit(); err != nil {
			log.Error(nil, map[string]interface{}{
				"migrations": m,
				"err":        err,
			}, "error during transaction commit: %v", err)
			return errs.Errorf("Error during transaction commit: %s\n", err)
		}

	}

	if err != nil {
		log.Error(nil, map[string]interface{}{
			"migrations": m,
			"err":        err,
		}, "migration failed with error: %v", err)
		return errs.Errorf("Migration failed with error: %s\n", err)
	}

	return nil
}
// Migrate executes the required migration of the database on startup.
// For each successful migration, an entry will be written into the "version"
// table, that states when a certain version was reached.
Migrate executes the required migration of the database on startup.
For each successful migration, an entry will be written into the "version"
table, that states when a certain version was reached.
// Migrate executes the required migration of the database on startup . // For each successful migration an entry will be written into the " version " // table that states when a certain version was reached .
func Migrate(db *sql.DB) error {
	var err error
	if db == nil {
		return errs.Errorf("Database handle is nil\n")
	}
	m := getMigrations()
	var tx *sql.Tx
	for nextVersion := int64(0); nextVersion < int64(len(m)) && err == nil; nextVersion++ {
		tx, err = db.Begin()
		if err != nil {
			return errs.Errorf("Failed to start transaction: %s\n", err)
		}
		err = migrateToNextVersion(tx, &nextVersion, m)
		if err != nil {
			oldErr := err
			log.Info(nil, map[string]interface{}{
				"next_version": nextVersion,
				"migrations":   m,
				"err":          err,
			}, "Rolling back transaction due to: %v", err)
			if err = tx.Rollback(); err != nil {
				log.Error(nil, map[string]interface{}{
					"next_version": nextVersion,
					"migrations":   m,
					"err":          err,
				}, "error while rolling back transaction: ", err)
				return errs.Errorf("Error while rolling back transaction: %s\n", err)
			}
			return oldErr
		}
		if err = tx.Commit(); err != nil {
			log.Error(nil, map[string]interface{}{
				"migrations": m,
				"err":        err,
			}, "error during transaction commit: %v", err)
			return errs.Errorf("Error during transaction commit: %s\n", err)
		}
	}
	if err != nil {
		log.Error(nil, map[string]interface{}{
			"migrations": m,
			"err":        err,
		}, "migration failed with error: %v", err)
		return errs.Errorf("Migration failed with error: %s\n", err)
	}
	return nil
}
func Migrate ( db * sql . DB ) error { var err error if db == nil { return errs . Errorf ( " \n " ) } m := getMigrations ( ) var tx * sql . Tx for nextVersion := int64 ( 0 ) ; nextVersion < int64 ( len ( m ) ) && err == nil ; nextVersion ++ { tx , err = db . Begin ( ) if err != nil { return errs . Errorf ( " \n " , err ) } err = migrateToNextVersion ( tx , & nextVersion , m ) if err != nil { oldErr := err log . Info ( nil , map [ string ] interface { } { " " : nextVersion , " " : m , " " : err , } , " " , err ) if err = tx . Rollback ( ) ; err != nil { log . Error ( nil , map [ string ] interface { } { " " : nextVersion , " " : m , " " : err , } , " " , err ) return errs . Errorf ( " \n " , err ) } return oldErr } if err = tx . Commit ( ) ; err != nil { log . Error ( nil , map [ string ] interface { } { " " : m , " " : err , } , " " , err ) return errs . Errorf ( " \n " , err ) } } if err != nil { log . Error ( nil , map [ string ] interface { } { " " : m , " " : err , } , " " , err ) return errs . Errorf ( " \n " , err ) } return nil }
Migrate executes the required migration of the database on startup.
Migrate executes the required migration of the database on startup .