1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
6
// Phase values specify model migration phases.
9
// Enumerate all possible migration phases.
25
var phaseNames = []string{
40
// String returns the name of an model migration phase constant.
41
func (p Phase) String() string {
43
if i >= 0 && i < len(phaseNames) {
49
// CanTransitionTo returns true if the given phase is a valid next
50
// model migration phase.
51
func (p Phase) CanTransitionTo(targetPhase Phase) bool {
52
nextPhases, exists := validTransitions[p]
56
for _, nextPhase := range nextPhases {
57
if nextPhase == targetPhase {
64
// IsTerminal returns true if the phase is one which signifies the end
66
func (p Phase) IsTerminal() bool {
67
for _, t := range terminalPhases {
75
// Define all possible phase transitions.
77
// The keys are the "from" states and the values enumerate the
78
// possible "to" states.
79
var validTransitions = map[Phase][]Phase{
80
QUIESCE: {READONLY, ABORT},
81
READONLY: {PRECHECK, ABORT},
82
PRECHECK: {IMPORT, ABORT},
83
IMPORT: {VALIDATION, ABORT},
84
VALIDATION: {SUCCESS, ABORT},
85
SUCCESS: {LOGTRANSFER},
87
REAP: {DONE, REAPFAILED},
90
var terminalPhases []Phase
93
// Compute the terminal phases.
94
for p := 0; p <= len(phaseNames); p++ {
96
if _, exists := validTransitions[phase]; !exists {
97
terminalPhases = append(terminalPhases, phase)
102
// ParsePhase converts a string model migration phase name
103
// to its constant value.
104
func ParsePhase(target string) (Phase, bool) {
105
for p, name := range phaseNames {
107
return Phase(p), true
110
return UNKNOWN, false