-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (62 loc) · 1.42 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
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/dudakovict/social-network/business/data/post/dbschema"
"github.com/dudakovict/social-network/business/sys/database"
)
func main() {
err := migrate()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func seed() error {
cfg := database.Config{
User: "postgres",
Password: "postgres",
Host: "localhost:5433",
Name: "postgres",
MaxIdleConns: 0,
MaxOpenConns: 0,
DisableTLS: true,
}
db, err := database.Open(cfg)
if err != nil {
return fmt.Errorf("connect database: %w", err)
}
defer db.Close()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := dbschema.Seed(ctx, db); err != nil {
return fmt.Errorf("seed database: %w", err)
}
fmt.Println("seed data complete")
return nil
}
func migrate() error {
cfg := database.Config{
User: "postgres",
Password: "postgres",
Host: "localhost:5433",
Name: "postgres",
MaxIdleConns: 0,
MaxOpenConns: 0,
DisableTLS: true,
}
db, err := database.Open(cfg)
if err != nil {
return fmt.Errorf("connect database: %w", err)
}
defer db.Close()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := dbschema.Migrate(ctx, db); err != nil {
return fmt.Errorf("migrate database: %w", err)
}
fmt.Println("migrations complete")
return seed()
}