func migrateToNextVersion(tx *sql.Tx, nextVersion *int64, m migrations) error {
	// Obtain exclusive transaction level advisory that doesn't depend on any table.
	// Once obtained, the lock is held for the remainder of the current transaction.
	// (There is no UNLOCK TABLE command; locks are always released at transaction end.)
	if _, err := tx.Exec("SELECT pg_advisory_xact_lock($1)", AdvisoryLockID); err != nil {
		return errs.Errorf("Failed to acquire lock: %s\n", err)
	}

	// Determine current version and adjust the outmost loop
	// iterator variable "version"
	currentVersion, err := getCurrentVersion(tx)
	if err != nil {
		return errs.WithStack(err)
	}
	*nextVersion = currentVersion + 1
	if *nextVersion >= int64(len(m)) {
		// No further updates to apply (this is NOT an error)
		log.Info(nil, map[string]interface{}{
			"next_version":    *nextVersion,
			"current_version": currentVersion,
		}, "Current version %d. Nothing to update.", currentVersion)
		return nil
	}

	log.Info(nil, map[string]interface{}{
		"next_version":    *nextVersion,
		"current_version": currentVersion,
	}, "Attempt to update DB to version %v", *nextVersion)

	// Apply all the updates of the next version
	for j := range m[*nextVersion] {
		if err := m[*nextVersion][j](tx); err != nil {
			return errs.Errorf("Failed to execute migration of step %d of version %d: %s\n", j, *nextVersion, err)
		}
	}

	if _, err := tx.Exec("INSERT INTO version(version) VALUES($1)", *nextVersion); err != nil {
		return errs.Errorf("Failed to update DB to version %d: %s\n", *nextVersion, err)
	}

	log.Info(nil, map[string]interface{}{
		"next_version":    *nextVersion,
		"current_version": currentVersion,
	}, "Successfully updated DB to version %v", *nextVersion)

	return nil
}
// migrateToNextVersion migrates the database to the nextVersion.
// If the database is already at nextVersion or higher, the nextVersion
// will be set to the actual next version.
migrateToNextVersion migrates the database to the nextVersion.
If the database is already at nextVersion or higher, the nextVersion
will be set to the actual next version.
// migrateToNextVersion migrates the database to the nextVersion . // If the database is already at nextVersion or higher the nextVersion // will be set to the actual next version .
func migrateToNextVersion(tx *sql.Tx, nextVersion *int64, m migrations) error {
	if _, err := tx.Exec("SELECT pg_advisory_xact_lock($1)", AdvisoryLockID); err != nil {
		return errs.Errorf("Failed to acquire lock: %s\n", err)
	}
	currentVersion, err := getCurrentVersion(tx)
	if err != nil {
		return errs.WithStack(err)
	}
	*nextVersion = currentVersion + 1
	if *nextVersion >= int64(len(m)) {
		log.Info(nil, map[string]interface{}{
			"next_version":    *nextVersion,
			"current_version": currentVersion,
		}, "Current version %d. Nothing to update.", currentVersion)
		return nil
	}
	log.Info(nil, map[string]interface{}{
		"next_version":    *nextVersion,
		"current_version": currentVersion,
	}, "Attempt to update DB to version %v", *nextVersion)
	for j := range m[*nextVersion] {
		if err := m[*nextVersion][j](tx); err != nil {
			return errs.Errorf("Failed to execute migration of step %d of version %d: %s\n", j, *nextVersion, err)
		}
	}
	if _, err := tx.Exec("INSERT INTO version(version) VALUES($1)", *nextVersion); err != nil {
		return errs.Errorf("Failed to update DB to version %d: %s\n", *nextVersion, err)
	}
	log.Info(nil, map[string]interface{}{
		"next_version":    *nextVersion,
		"current_version": currentVersion,
	}, "Successfully updated DB to version %v", *nextVersion)
	return nil
}
func migrateToNextVersion ( tx * sql . Tx , nextVersion * int64 , m migrations ) error { if _ , err := tx . Exec ( " " , AdvisoryLockID ) ; err != nil { return errs . Errorf ( " \n " , err ) } currentVersion , err := getCurrentVersion ( tx ) if err != nil { return errs . WithStack ( err ) } * nextVersion = currentVersion + 1 if * nextVersion >= int64 ( len ( m ) ) { log . Info ( nil , map [ string ] interface { } { " " : * nextVersion , " " : currentVersion , } , " " , currentVersion ) return nil } log . Info ( nil , map [ string ] interface { } { " " : * nextVersion , " " : currentVersion , } , " " , * nextVersion ) for j := range m [ * nextVersion ] { if err := m [ * nextVersion ] [ j ] ( tx ) ; err != nil { return errs . Errorf ( " \n " , j , * nextVersion , err ) } } if _ , err := tx . Exec ( " " , * nextVersion ) ; err != nil { return errs . Errorf ( " \n " , * nextVersion , err ) } log . Info ( nil , map [ string ] interface { } { " " : * nextVersion , " " : currentVersion , } , " " , * nextVersion ) return nil }
migrateToNextVersion migrates the database to the nextVersion.
migrateToNextVersion migrates the database to the nextVersion .
// Obtain exclusive transaction level advisory that doesn't depend on any table. // Once obtained, the lock is held for the remainder of the current transaction. // (There is no UNLOCK TABLE command; locks are always released at transaction end.) // Determine current version and adjust the outmost loop // iterator variable "version" // No further updates to apply (this is NOT an error) // Apply all the updates of the next version