github.com/safing/portbase@v0.19.5/database/migration/error.go (about)

     1  package migration
     2  
     3  import "errors"
     4  
     5  // DiagnosticStep describes one migration step in the Diagnostics.
     6  type DiagnosticStep struct {
     7  	Version     string
     8  	Description string
     9  }
    10  
    11  // Diagnostics holds a detailed error report about a failed migration.
    12  type Diagnostics struct { //nolint:errname
    13  	// Message holds a human readable message of the encountered
    14  	// error.
    15  	Message string
    16  	// Wrapped must be set to the underlying error that was encountered
    17  	// while preparing or executing migrations.
    18  	Wrapped error
    19  	// StartOfMigration is set to the version of the database before
    20  	// any migrations are applied.
    21  	StartOfMigration string
    22  	// LastSuccessfulMigration is set to the version of the database
    23  	// which has been applied successfully before the error happened.
    24  	LastSuccessfulMigration string
    25  	// TargetVersion is set to the version of the database that the
    26  	// migration run aimed for. That is, it's the last available version
    27  	// added to the registry.
    28  	TargetVersion string
    29  	// ExecutionPlan is a list of migration steps that were planned to
    30  	// be executed.
    31  	ExecutionPlan []DiagnosticStep
    32  	// FailedMigration is the description of the migration that has
    33  	// failed.
    34  	FailedMigration string
    35  }
    36  
    37  // Error returns a string representation of the migration error.
    38  func (err *Diagnostics) Error() string {
    39  	msg := ""
    40  	if err.FailedMigration != "" {
    41  		msg = err.FailedMigration + ": "
    42  	}
    43  	if err.Message != "" {
    44  		msg += err.Message + ": "
    45  	}
    46  	msg += err.Wrapped.Error()
    47  	return msg
    48  }
    49  
    50  // Unwrap returns the actual error that happened when executing
    51  // a migration. It implements the interface required by the stdlib
    52  // errors package to support errors.Is() and errors.As().
    53  func (err *Diagnostics) Unwrap() error {
    54  	if u := errors.Unwrap(err.Wrapped); u != nil {
    55  		return u
    56  	}
    57  	return err.Wrapped
    58  }