-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.go
More file actions
133 lines (107 loc) · 2.96 KB
/
files.go
File metadata and controls
133 lines (107 loc) · 2.96 KB
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
package go_migration
import (
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
)
// listMigrationFiles will get migration files from the configured path.
func (a *Action) listMigrationFiles() ([]string, error) {
fileFilter := "."+ a.Action +".sql"
var files []string
f, err := ioutil.ReadDir(a.Path)
if err != nil {
return files, fmt.Errorf("unable to list migration files. %w", err)
}
for _, file := range f {
if strings.Contains(file.Name(), fileFilter) {
files = append(files, file.Name())
}
}
return files, nil
}
// getPendingMigrationFiles will loop through all migration files and return the ones that haven't been run yet.
func (a *Action) getPendingMigrationFiles() ([]string, error) {
var pendingFiles []string
var foundLastRan = false
lastRanMigration, err := a.getLatestRanMigration()
if err != nil {
return pendingFiles, err
}
allFiles, err := a.listMigrationFiles()
if err != nil {
return pendingFiles, err
}
if lastRanMigration == "" { // No migrations have run yet.
foundLastRan = true // If no migrations have previously ran, set found as true to start from first file.
}
var i = 0
for _, file := range allFiles {
if i == a.Number {
break
}
if file == lastRanMigration {
foundLastRan = true
continue
}
if !foundLastRan {
continue
}
pendingFiles = append(pendingFiles, file)
i++
}
return pendingFiles, nil
}
// getPreviouslyMigratedFiles will loop through all migration files and return the ones that have already been run.
func (a *Action) getMigrationFilesToRollBack() ([]string, error) {
var migrationsToRollBack []string
var foundLastRan = false
lastRanMigration, err := a.getLatestRanMigration()
if err != nil {
return migrationsToRollBack, err
}
allFiles, err := a.listMigrationFiles()
if err != nil {
return migrationsToRollBack, err
}
sort.Sort(sort.Reverse(sort.StringSlice(allFiles)))
if lastRanMigration == "" { // No migrations have run yet.
return migrationsToRollBack, nil
}
lastRanMigrationShortName := TrimExtension(lastRanMigration)
var i = 0
for _, file := range allFiles {
shortFileName := TrimExtension(file)
if i == a.Number {
break
}
if shortFileName == lastRanMigrationShortName {
foundLastRan = true
migrationsToRollBack = append(migrationsToRollBack, file)
i++
continue
}
if !foundLastRan {
continue
}
migrationsToRollBack = append(migrationsToRollBack, file)
i++
}
return migrationsToRollBack, nil
}
// parseMigrations will iterate through the files and unmarshal the JSON and add to the files slice.
func (a *Action) parseMigrations(filesToParse []string) ([]File, error) {
var files []File
for _, file := range filesToParse {
thisFile := File{Filename: file}
data, err := os.ReadFile(a.Path+"/"+file)
if err != nil {
return files, fmt.Errorf("unable to read file: %s. %s", file, err)
}
queries := strings.Split(string(data), QuerySeparator)
thisFile.Queries = queries
files = append(files, thisFile)
}
return files, nil
}