github.com/m3db/m3@v1.5.0/src/dbnode/persist/fs/migration/version.go (about)

     1  // Copyright (c) 2020 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package migration
    22  
    23  import "fmt"
    24  
    25  const (
    26  	// MigrationVersionNone indicates node should not attempt to perform any migrations.
    27  	MigrationVersionNone MigrationVersion = iota
    28  	// MigrationVersion_1_1 indicates node should attempt to migrate data files up to version 1.1.
    29  	MigrationVersion_1_1
    30  )
    31  
    32  var (
    33  	validMigrationVersions = []MigrationVersion{
    34  		MigrationVersionNone,
    35  		MigrationVersion_1_1,
    36  	}
    37  )
    38  
    39  func (m *MigrationVersion) String() string {
    40  	switch *m {
    41  	case MigrationVersionNone:
    42  		return "none"
    43  	case MigrationVersion_1_1:
    44  		return "1.1"
    45  	default:
    46  		return "unknown"
    47  	}
    48  }
    49  
    50  // ParseMigrationVersion parses a string for a MigrationVersion.
    51  func ParseMigrationVersion(str string) (MigrationVersion, error) {
    52  	for _, valid := range validMigrationVersions {
    53  		if str == valid.String() {
    54  			return valid, nil
    55  		}
    56  	}
    57  
    58  	return 0, fmt.Errorf("unrecognized migrate version: %v", str)
    59  }
    60  
    61  // ValidateMigrationVersion validates a stored metrics type.
    62  func ValidateMigrationVersion(m MigrationVersion) error {
    63  	for _, valid := range validMigrationVersions {
    64  		if valid == m {
    65  			return nil
    66  		}
    67  	}
    68  
    69  	return fmt.Errorf("invalid migrate version '%v': should be one of %v",
    70  		m, validMigrationVersions)
    71  }
    72  
    73  // UnmarshalYAML unmarshals a migrate version.
    74  func (m *MigrationVersion) UnmarshalYAML(unmarshal func(interface{}) error) error {
    75  	var str string
    76  	if err := unmarshal(&str); err != nil {
    77  		return err
    78  	}
    79  
    80  	if value, err := ParseMigrationVersion(str); err == nil {
    81  		*m = value
    82  		return nil
    83  	}
    84  
    85  	return fmt.Errorf("invalid MigrationVersion '%s' valid types are: %v",
    86  		str, validMigrationVersions)
    87  }