-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
119 lines (104 loc) · 3.05 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
package main
import (
"asira_borrower/asira"
"asira_borrower/migration"
"asira_borrower/router"
"database/sql"
"flag"
"fmt"
"log"
"os"
"github.com/labstack/echo/middleware"
"github.com/pressly/goose"
)
var (
flags = flag.NewFlagSet("goose", flag.ExitOnError)
)
func main() {
defer asira.App.Close()
flags.Usage = usage
flags.Parse(os.Args[1:])
args := flags.Args()
migrationDir := "migration" // migration directory
switch args[0] {
default:
flags.Usage()
break
case "run":
e := router.NewRouter()
if asira.App.Config.GetBool("react_cors") {
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowHeaders: []string{"*"},
}))
}
e.Logger.Fatal(e.Start(":" + asira.App.Port))
os.Exit(0)
break
case "seed":
migration.Seed()
os.Exit(0)
break
case "truncate":
err := migration.Truncate(args[1:])
if err != nil {
log.Fatalf("%v", err)
flags.Usage()
}
os.Exit(0)
break
case "create":
if err := goose.Run("create", nil, migrationDir, args[1:]...); err != nil {
log.Fatalf("goose create: %v", err)
flags.Usage()
}
return
case "migrate": // command example : [app name] migrate up
if err := goose.SetDialect("postgres"); err != nil {
log.Fatalf("goose set dialect : %v", err)
flags.Usage()
}
dbconf := asira.App.Config.GetStringMap(fmt.Sprintf("%s.database", asira.App.ENV))
connectionString := fmt.Sprintf("host=%s user=%s dbname=%s sslmode=%s password=%s", dbconf["host"].(string), dbconf["username"].(string), dbconf["table"].(string), dbconf["sslmode"].(string), dbconf["password"].(string))
db, err := sql.Open("postgres", connectionString)
if err != nil {
log.Fatalf("-connectionString=%q: %v\n", connectionString, err)
flags.Usage()
}
if err := goose.Run(args[1], db, migrationDir, args[2:]...); err != nil {
log.Fatalf("goose run: %v", err)
flags.Usage()
}
break
}
}
func usage() {
usagestring := `
to run the app :
[app_name] run
example : asira run
to update db :
[app_name] migrate [goose_command]
example : asira migrate up
goose command lists:
up Migrate the DB to the most recent version available
up-by-one Migrate the DB up by 1
up-to VERSION Migrate the DB to a specific VERSION
down Roll back the version by 1
down-to VERSION Roll back to a specific VERSION
redo Re-run the latest migration
reset Roll back all migrations
status Dump the migration status for the current DB
version Print the current version of the database
create NAME [sql|go] Creates new migration file with the current timestamp
fix Apply sequential ordering to migrations
database seeding : (development environment only)
[app_name] seed
example : asira seed
database truncate : (development environment only)
[app_name] truncate [table(s)]
example : asira truncate borrowers | asira truncate borrowers loans | asira truncate all
replace [table] with 'all' to truncate all tables
`
log.Print(usagestring)
}