-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration_json_provider.go
217 lines (172 loc) · 4.31 KB
/
migration_json_provider.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package migrator
import (
"encoding/json"
"fmt"
"io"
"os"
"sort"
"strings"
"time"
)
const migrationJSONFileName = "./migrations/migrations.json"
const migrationJSONReportFileName = "./migrations/migration_report.json"
type jsonMigration struct {
data map[string]string
timeString string
jsonFileName string
jsonReporFileName string
}
type jsonMigrationReport struct {
FileName string `json:"fileName"`
CreatedAt string `json:"createdAt"`
ResultStatus string `json:"resultStatus"`
Message string `json:"message"`
}
func newJSONMigration() (*jsonMigration, error) {
jsonMigration := &jsonMigration{}
jsonMigration.ResetDate()
err := jsonMigration.loadMigrationFile()
return jsonMigration, err
}
func (m *jsonMigration) ResetDate() {
m.timeString = time.Now().Format(timeFormat)
}
func (m *jsonMigration) Migrations(isLatest bool) ([]string, error) {
var latestDate string
var filtered []string
for _, dateString := range m.data {
if latestDate == "" || dateString > latestDate {
latestDate = dateString
}
}
for fileName, dateString := range m.data {
if dateString == latestDate || !isLatest {
filtered = append(filtered, fileName)
}
}
sort.Sort(sort.Reverse(sort.StringSlice(filtered)))
return filtered, nil
}
func (m *jsonMigration) loadMigrationFile() error {
m.data = make(map[string]string)
jsonFileName := m.GetJSONFileName()
if !fileExists(jsonFileName) {
return nil
}
jsonData, err := os.ReadFile(jsonFileName)
if err != nil {
return err
}
err = json.Unmarshal(jsonData, &m.data)
if err != nil {
return err
}
return nil
}
func (m *jsonMigration) saveMigrationFile() error {
jsonData, err := json.Marshal(&m.data)
if err != nil {
return err
}
jsonFileName := m.GetJSONFileName()
return os.WriteFile(jsonFileName, jsonData, 0644)
}
func (m *jsonMigration) AddToMigration(fileName string) error {
m.data[fileName] = m.timeString
return m.saveMigrationFile()
}
func (m *jsonMigration) RemoveFromMigration(fileName string) error {
delete(m.data, fileName)
return m.saveMigrationFile()
}
func (m *jsonMigration) MigrationExistsForFile(fileName string) (bool, error) {
return m.data[fileName] != "", nil
}
func (m *jsonMigration) GetJSONFileName() string {
if m.jsonFileName == "" {
return migrationJSONFileName
}
return m.jsonFileName
}
func (m *jsonMigration) getJSONReportFileName() string {
if m.jsonFileName == "" {
return migrationJSONReportFileName
}
return m.jsonReporFileName
}
func (m *jsonMigration) SetJSONFilePath(filePath string) {
m.jsonFileName = filePath + "/migrations.json"
m.jsonReporFileName = filePath + "/migration_reports.json"
}
func (m *jsonMigration) AddToMigrationReport(fileName string, errorToLog error) error {
storeFileName := m.getJSONReportFileName()
message := "ok"
status := statusSuccess
if errorToLog != nil {
message = errorToLog.Error()
status = statusError
}
file, err := os.OpenFile(storeFileName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
return err
}
defer file.Close()
newReportItem := jsonMigrationReport{
FileName: fileName,
ResultStatus: status,
CreatedAt: time.Now().Format(timeFormat),
Message: message,
}
newData, err := json.Marshal(newReportItem)
if err != nil {
return err
}
fileInfo, err := file.Stat()
if err != nil {
return err
}
if fileInfo.Size() > 0 {
newData = append([]byte(","), newData...)
}
if _, err := file.Write(newData); err != nil {
return err
}
return nil
}
func (m *jsonMigration) Report() (string, error) {
storeFileName := m.getJSONReportFileName()
_, err := os.Stat(storeFileName)
if os.IsNotExist(err) {
return "", nil
}
// Open the JSON file
jsonFile, err := os.Open(storeFileName)
if err != nil {
return "", err
}
defer jsonFile.Close()
// Read the JSON file
byteValue, err := io.ReadAll(jsonFile)
byteValue = append([]byte{'['}, byteValue...)
byteValue = append(byteValue, ']')
if err != nil {
return "", err
}
var collection []jsonMigrationReport
err = json.Unmarshal(byteValue, &collection)
if err != nil {
return "", err
}
var builder strings.Builder
for _, item := range collection {
str := fmt.Sprintf(
reportMessageText,
item.CreatedAt,
item.FileName,
item.ResultStatus,
item.Message,
)
builder.WriteString(str)
}
return builder.String(), nil
}