-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigration_runner.go
153 lines (129 loc) · 4.04 KB
/
migration_runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package migration
import (
"fmt"
"github.com/aleksanderaleksic/tgmigrate/common"
"github.com/aleksanderaleksic/tgmigrate/config"
"github.com/aleksanderaleksic/tgmigrate/history"
"github.com/aleksanderaleksic/tgmigrate/state"
)
type Runner struct {
Context *common.Context
Config *config.Config
HistoryInterface history.History
StateInterface state.State
}
func (r Runner) Apply(environment *string) error {
defer r.HistoryInterface.Cleanup()
defer r.StateInterface.Cleanup()
_, err := r.HistoryInterface.InitializeHistory()
if err != nil {
return fmt.Errorf("unable to initialize history, error: %s", err)
}
migrationFiles, err := GetMigrationFiles(r.Config.AbsoluteMigrationDir)
if err != nil {
return fmt.Errorf("could not get migration files from '%s', error: %s", r.Config.AbsoluteMigrationDir, err)
}
migrationsToBeApplied, err := r.getMigrationsToBeApplied(*migrationFiles, environment)
if err != nil {
return err
}
if len(*migrationsToBeApplied) == 0 {
fmt.Println("No migrations will be applied")
return nil
}
err = r.StateInterface.InitializeState()
if err != nil {
return err
}
for _, migrationFile := range *migrationsToBeApplied {
fmt.Printf("Migrations for %s will be applied\n", migrationFile.Metadata.FileName)
var isSuccess = true
var migrationError error
var failingMigration = ""
for _, migration := range migrationFile.Migrations {
if isSuccess == false {
break
}
switch migration.Type {
case "remove":
success, removeError := r.StateInterface.Remove(
state.ResourceContext{
State: migration.Remove.State,
Resource: migration.Remove.Resource,
},
)
if !success {
isSuccess = false
migrationError = removeError
failingMigration = fmt.Sprintf("Remove %s %s",
migration.Remove.State,
migration.Remove.Resource,
)
break
}
case "move":
success, moveError := r.StateInterface.Move(
state.ResourceContext{
State: migration.Move.From.State,
Resource: migration.Move.From.Resource,
}, state.ResourceContext{
State: migration.Move.To.State,
Resource: migration.Move.To.Resource,
},
)
if !success {
isSuccess = false
migrationError = moveError
failingMigration = fmt.Sprintf("Move %s %s -> %s %s",
migration.Move.From.State,
migration.Move.From.Resource,
migration.Move.To.State,
migration.Move.To.Resource,
)
break
}
}
}
if !isSuccess && migrationError != nil {
r.HistoryInterface.StoreFailedMigration(&history.FailedStorageHistoryObject{
SchemaVersion: history.StorageHistoryObjectVersion,
Hash: migrationFile.Metadata.FileHash,
Name: migrationFile.Metadata.FileName,
})
_ = r.HistoryInterface.WriteToStorage()
return fmt.Errorf("failed to apply migrtaion '%s' '%s' \n with error: %s", migrationFile.Metadata.FileName, failingMigration, migrationError)
}
metadata, err := r.StateInterface.Complete()
if err != nil {
return err
}
r.HistoryInterface.StoreAppliedMigration(&history.AppliedStorageHistoryObject{
SchemaVersion: history.StorageHistoryObjectVersion,
Hash: migrationFile.Metadata.FileHash,
Name: migrationFile.Metadata.FileName,
Metadata: *metadata,
})
err = r.HistoryInterface.WriteToStorage()
if err != nil {
return err
}
}
return nil
}
func (r Runner) getMigrationsToBeApplied(migrationFiles []File, environment *string) (*[]File, error) {
var migrationsToBeApplied []File
for _, migration := range migrationFiles {
if environment != nil && !common.StringListContains(migration.Config.Environments, *environment) {
continue
}
shouldBeApplied, _ := r.HistoryInterface.ShouldMigrationBeApplied(migration.Metadata.FileName)
isApplied, err := r.HistoryInterface.IsMigrationApplied(migration.Metadata.FileHash)
if err != nil {
return nil, err
}
if !isApplied && shouldBeApplied {
migrationsToBeApplied = append(migrationsToBeApplied, migration)
}
}
return &migrationsToBeApplied, nil
}