-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathmain.go
108 lines (95 loc) · 2.99 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
package main
import (
"context"
"fmt"
"github.com/sysadminsmedia/homebox/backend/internal/sys/config"
"log"
"os"
"path/filepath"
"strings"
"github.com/sysadminsmedia/homebox/backend/internal/data/ent/migrate"
atlas "ariga.io/atlas/sql/migrate"
_ "ariga.io/atlas/sql/sqlite"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql/schema"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
func main() {
cfg, err := config.New(build(), "Homebox inventory management system")
if err != nil {
panic(err)
}
sqlDialect := ""
switch strings.ToLower(cfg.Database.Driver) {
case "sqlite3":
sqlDialect = dialect.SQLite
case "postgres":
sqlDialect = dialect.Postgres
default:
log.Fatalf("unsupported database driver: %s", cfg.Database.Driver)
}
ctx := context.Background()
// Create a local migration directory able to understand Atlas migration file format for replay.
safePath := filepath.Clean(fmt.Sprintf("internal/data/migrations/%s", sqlDialect))
dir, err := atlas.NewLocalDir(safePath)
if err != nil {
log.Fatalf("failed creating atlas migration directory: %v", err)
}
// Migrate diff options.
opts := []schema.MigrateOption{
schema.WithDir(dir), // provide migration directory
schema.WithMigrationMode(schema.ModeReplay), // provide migration mode
schema.WithDialect(sqlDialect), // Ent dialect to use
schema.WithFormatter(atlas.DefaultFormatter),
schema.WithDropIndex(true),
schema.WithDropColumn(true),
}
if len(os.Args) != 2 {
log.Fatalln("migration name is required. Use: 'go run -mod=mod ent/migrate/main.go <name>'")
}
if sqlDialect == dialect.Postgres {
if !validatePostgresSSLMode(cfg.Database.SslMode) {
log.Fatalf("invalid sslmode: %s", cfg.Database.SslMode)
}
}
databaseURL := ""
switch cfg.Database.Driver {
case "sqlite3":
databaseURL = fmt.Sprintf("sqlite://%s", cfg.Database.SqlitePath)
case "postgres":
databaseURL = fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=%s", cfg.Database.Username, cfg.Database.Password, cfg.Database.Host, cfg.Database.Port, cfg.Database.Database, cfg.Database.SslMode)
default:
log.Fatalf("unsupported database driver: %s", cfg.Database.Driver)
}
// Generate migrations using Atlas support for MySQL (note the Ent dialect option passed above).
err = migrate.NamedDiff(ctx, databaseURL, os.Args[1], opts...)
if err != nil {
log.Fatalf("failed generating migration file: %v", err)
}
fmt.Println("Migration file generated successfully.")
}
var (
version = "nightly"
commit = "HEAD"
buildTime = "now"
)
func build() string {
short := commit
if len(short) > 7 {
short = short[:7]
}
return fmt.Sprintf("%s, commit %s, built at %s", version, short, buildTime)
}
func validatePostgresSSLMode(sslMode string) bool {
validModes := map[string]bool{
"": true,
"disable": true,
"allow": true,
"prefer": true,
"require": true,
"verify-ca": true,
"verify-full": true,
}
return validModes[strings.ToLower(strings.TrimSpace(sslMode))]
}