This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
219 lines (201 loc) · 4.91 KB
/
main.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
218
219
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/fabmation-gmbh/briefkasten-go/internal/config"
"github.com/fabmation-gmbh/briefkasten-go/internal/log"
"github.com/fabmation-gmbh/briefkasten-go/migrations"
"github.com/fabmation-gmbh/briefkasten-go/models"
"github.com/uptrace/bun/migrate"
"github.com/urfave/cli/v2"
)
var migrator *migrate.Migrator
func main() {
cmds := []*cli.Command{
newDBCommand(),
}
// cmds = append(cmds, cmd.NewCommands()...)
cfgDefault := filepath.Join("/etc/briefkasten", "config.yaml")
app := &cli.App{
Name: "server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Usage: "path to the config file",
Aliases: []string{"c"},
Value: cfgDefault,
},
&cli.StringFlag{
Name: "level",
Usage: "logging level (allowed values: trace, debug, info, warn, error, fatal, panic)",
Aliases: []string{"log-level", "l"},
Value: "info",
},
},
Before: func(c *cli.Context) error {
return untilError(
func() error { return config.LoadConfig(c.String("config")) },
func() error {
log.InitLogging(c.String("level"), config.C.General.Environment)
return nil
},
func() error {
models.Connect()
migrator = migrate.NewMigrator(models.GetDB(), migrations.Migrations)
return nil
},
)
},
Commands: cmds,
}
fatalIf(app.Run(os.Args), "")
}
func fatalIf(err error, msg string) {
if err == nil {
return
}
fmt.Fprintf(os.Stderr, msg+": %+v\n", err)
os.Exit(1)
}
func untilError(f ...func() error) error {
for _, fc := range f {
if err := fc(); err != nil {
return err
}
}
return nil
}
func newDBCommand() *cli.Command {
return &cli.Command{
Name: "db",
Usage: "database migrations",
Before: func(c *cli.Context) error {
_ = c
models.Connect()
return nil
},
Subcommands: []*cli.Command{
{
Name: "init",
Usage: "create migration tables",
Action: func(c *cli.Context) error {
return migrator.Init(c.Context)
},
},
{
Name: "migrate",
Usage: "migrate database",
Action: func(c *cli.Context) error {
if err := migrator.Lock(c.Context); err != nil {
return err
}
defer migrator.Unlock(c.Context) //nolint:errcheck
group, err := migrator.Migrate(c.Context)
if err != nil {
return err
}
if group.IsZero() {
fmt.Printf("there are no new migrations to run (database is up to date)\n")
return nil
}
fmt.Printf("migrated to %s\n", group)
return nil
},
},
{
Name: "rollback",
Usage: "rollback the last migration group",
Action: func(c *cli.Context) error {
if err := migrator.Lock(c.Context); err != nil {
return err
}
defer migrator.Unlock(c.Context) //nolint:errcheck
group, err := migrator.Rollback(c.Context)
if err != nil {
return err
}
if group.IsZero() {
fmt.Printf("there are no groups to roll back\n")
return nil
}
fmt.Printf("rolled back %s\n", group)
return nil
},
},
{
Name: "lock",
Usage: "lock migrations",
Action: func(c *cli.Context) error {
return migrator.Lock(c.Context)
},
},
{
Name: "unlock",
Usage: "unlock migrations",
Action: func(c *cli.Context) error {
return migrator.Unlock(c.Context)
},
},
{
Name: "create_go",
Usage: "create Go migration",
Action: func(c *cli.Context) error {
name := strings.Join(c.Args().Slice(), "_")
mf, err := migrator.CreateGoMigration(c.Context, name)
if err != nil {
return err
}
fmt.Printf("created migration %s (%s)\n", mf.Name, mf.Path)
return nil
},
},
{
Name: "create_sql",
Usage: "create up and down SQL migrations",
Action: func(c *cli.Context) error {
name := strings.Join(c.Args().Slice(), "_")
files, err := migrator.CreateSQLMigrations(c.Context, name)
if err != nil {
return err
}
for _, mf := range files {
fmt.Printf("created migration %s (%s)\n", mf.Name, mf.Path)
}
return nil
},
},
{
Name: "status",
Usage: "print migrations status",
Action: func(c *cli.Context) error {
ms, err := migrator.MigrationsWithStatus(c.Context)
if err != nil {
return err
}
fmt.Printf("migrations: %s\n", ms)
fmt.Printf("unapplied migrations: %s\n", ms.Unapplied())
fmt.Printf("last migration group: %s\n", ms.LastGroup())
return nil
},
},
{
Name: "mark_applied",
Usage: "mark migrations as applied without actually running them",
Action: func(c *cli.Context) error {
group, err := migrator.Migrate(c.Context, migrate.WithNopMigration())
if err != nil {
return err
}
if group.IsZero() {
fmt.Printf("there are no new migrations to mark as applied\n")
return nil
}
fmt.Printf("marked as applied %s\n", group)
return nil
},
},
},
}
}