-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.go
More file actions
212 lines (193 loc) · 5.38 KB
/
Copy pathmigrate.go
File metadata and controls
212 lines (193 loc) · 5.38 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
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
//nolint:gocritic
package cmd
import (
"fmt"
"log"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"time"
elemental "github.com/elcengine/elemental/core"
"github.com/elcengine/elemental/utils"
"github.com/samber/lo"
"github.com/spf13/cast"
"github.com/spf13/cobra"
)
const (
TargetSeed = "seed"
TargetMigrate = "migrate"
)
func run(rollback bool, target string) {
cfg := readConfigFile()
dir := cfg.MigrationsDir
if target == TargetSeed {
dir = cfg.SeedsDir
}
files, err := os.ReadDir(dir)
if err != nil {
log.Fatalf("Failed to read %ss: %v", target, err)
}
files = lo.Filter(files, func(file os.DirEntry, index int) bool {
return file.Name() != ".gitkeep"
})
if len(files) == 0 {
log.Fatalf("No %ss to run", target)
}
extractTimestamp := func(fileName string) int64 {
fileName = strings.TrimSuffix(fileName, ".go")
parts := strings.Split(fileName, "_")
timestampStr := parts[len(parts)-1]
return lo.Must(strconv.ParseInt(timestampStr, 10, 64))
}
sort.Slice(files, func(i, j int) bool {
file1Timestamp := extractTimestamp(files[i].Name())
file2Timestamp := extractTimestamp(files[j].Name())
return file1Timestamp < file2Timestamp
})
module := string(lo.Must(exec.Command("go", "list", "-m").Output()))
var template = fmt.Sprintf(`package main
import (
"context"
"strings"
"strconv"
"time"
"go.mongodb.org/mongo-driver/mongo"
"github.com/elcengine/elemental/core"
"%s"
)
func extractTimestamp(fileName string) int64 {
fileName = strings.TrimSuffix(fileName, ".go")
parts := strings.Split(fileName, "_")
timestampStr := parts[len(parts)-1]
timestamp, err := strconv.ParseInt(timestampStr, 10, 64)
if err != nil {
panic(err)
}
return timestamp
}
func main() {
client := elemental.Connect("%s")
db := elemental.UseDefaultDatabase()
go db.Collection("%s").Indexes().CreateOne(context.Background(), mongo.IndexModel{
Keys: map[string]any{"type": 1},
})
files := []string{%s}
functions := []func(context.Context, *mongo.Database, *mongo.Client){%s}
ctx := context.Background()
for i, f := range files {
functionToRun := functions[i]
var entry = map[string]any{}
db.Collection("%s").FindOne(ctx, map[string]any{"name": f, "type": "%s"}).Decode(&entry)
if %t {
if entry["name"] != nil {
functionToRun(ctx, db, &client)
db.Collection("%s").DeleteOne(ctx, map[string]any{"name": f})
}
} else {
if entry["name"] == nil {
functionToRun(ctx, db, &client)
db.Collection("%s").InsertOne(ctx, map[string]any{
"name": f,
"type": "%s",
"created_at": time.Now(),
})
}
}
}
}
`,
strings.TrimSpace(module)+"/"+dir,
cfg.ConnectionStr, cfg.ChangelogCollection,
strings.Join(lo.Map(files, func(file os.DirEntry, index int) string {
return fmt.Sprintf("\"%s\"", strings.TrimSuffix(file.Name(), ".go"))
}), ","),
strings.Join(lo.Map(files, func(file os.DirEntry, index int) string {
if rollback {
return fmt.Sprintf("%ss.Down_%d", target, extractTimestamp(file.Name()))
} else {
return fmt.Sprintf("%ss.Up_%d", target, extractTimestamp(file.Name()))
}
}), ","),
cfg.ChangelogCollection,
target,
rollback, cfg.ChangelogCollection, cfg.ChangelogCollection, target,
)
elemental.Connect(cfg.ConnectionStr)
defer elemental.Disconnect()
os.MkdirAll(".elemental/"+target+"s", os.ModePerm)
utils.CreateAndWriteToFile(fmt.Sprintf(".elemental/%ss/main.go", target), template)
err = exec.Command("go", "run", ".elemental/"+target+"s/main.go").Run()
if err != nil {
elemental.Disconnect()
log.Fatalf("Failed to run %ss: %s", target, err.Error())
} else {
if rollback {
log.Printf("Successfully rolled back %ss", target)
} else {
log.Printf("Successfully ran %ss", target)
}
}
os.Remove(fmt.Sprintf(".elemental/%ss/main.go", target))
}
func create(args []string, target string) {
if len(args) == 0 {
log.Fatalf("Please provide a name for the %s", target)
}
cfg := readConfigFile()
if target != TargetSeed {
os.MkdirAll(cfg.MigrationsDir, os.ModePerm)
} else {
os.MkdirAll(cfg.SeedsDir, os.ModePerm)
}
timestamp := cast.ToString(time.Now().UnixMilli())
var template = fmt.Sprintf(`package %ss
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
)
func Up_%s(ctx context.Context, db *mongo.Database, client *mongo.Client) {
// Write your %s here
}
func Down_%s(ctx context.Context, db *mongo.Database, client *mongo.Client) {
// Write your rollback here
}`, target, timestamp, target, timestamp)
dir := cfg.MigrationsDir
if target == TargetSeed {
dir = cfg.SeedsDir
}
outputFile := dir + "/" + args[0] + "_" + timestamp + ".go"
utils.CreateAndWriteToFile(outputFile, template)
log.Printf("%s file created at %s", target, outputFile)
}
var migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Run database migrations",
}
var createMigrationCmd = &cobra.Command{
Use: "create",
Short: "Create a new migration",
Run: func(cmd *cobra.Command, args []string) {
create(args, "migration")
},
}
var runMigrationCmd = &cobra.Command{
Use: "up",
Short: "Run database migrations",
Run: func(cmd *cobra.Command, args []string) {
run(false, "migration")
},
}
var rollbackMigrationCmd = &cobra.Command{
Use: "down",
Short: "Rollback database migrations",
Run: func(cmd *cobra.Command, args []string) {
run(true, "migration")
},
}
func init() {
migrateCmd.AddCommand(createMigrationCmd)
migrateCmd.AddCommand(runMigrationCmd)
migrateCmd.AddCommand(rollbackMigrationCmd)
}