-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
117 lines (108 loc) · 2.63 KB
/
utils.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
package test
import (
"fmt"
"log"
"os"
"github.com/dzahariev/e2e-rest/api/model"
"github.com/gofrs/uuid"
"github.com/jinzhu/gorm"
"github.com/joho/godotenv"
)
// EntityType represent abstraction for the diferent entities
type EntityType struct {
Name string
Entity model.Object
NewEntity model.Object
NewEntity1 model.Object
}
// CreateDB creates the database
func CreateDB(DB *gorm.DB, dbUser, dbPassword, dbPort, dbHost, dbName string) error {
DBURL := fmt.Sprintf("host=%s port=%s user=%s dbname=%s sslmode=disable password=%s", dbHost, dbPort, dbUser, "postgres", dbPassword)
DB, err := gorm.Open("postgres", DBURL)
defer DB.Close()
if err != nil {
log.Printf("Unable to conect DB %s", dbName)
return err
}
statement := fmt.Sprintf("CREATE DATABASE %s;", dbName)
DB = DB.Exec(statement)
if DB.Error != nil {
log.Printf("Unable to create DB %s with error %v", dbName, err)
return err
}
return nil
}
// DropDB drops the database
func DropDB(DB *gorm.DB, dbUser, dbPassword, dbPort, dbHost, dbName string) error {
DBURL := fmt.Sprintf("host=%s port=%s user=%s dbname=%s sslmode=disable password=%s", dbHost, dbPort, dbUser, "postgres", dbPassword)
DB, err := gorm.Open("postgres", DBURL)
defer DB.Close()
if err != nil {
log.Printf("Unable to conect DB %s ", dbName)
return err
}
statement := fmt.Sprintf("DROP DATABASE IF EXISTS %s;", dbName)
DB = DB.Exec(statement)
if DB.Error != nil {
log.Printf("Unable to drop the DB %s", dbName)
return err
}
return nil
}
// RecreateTables recreates the tables in database
func RecreateTables(DB *gorm.DB) error {
err := DB.DropTableIfExists(&model.User{}).Error
if err != nil {
return err
}
err = DB.DropTableIfExists(&model.Event{}).Error
if err != nil {
return err
}
err = DB.DropTableIfExists(&model.Session{}).Error
if err != nil {
return err
}
err = DB.DropTableIfExists(&model.Subscription{}).Error
if err != nil {
return err
}
err = DB.DropTableIfExists(&model.Comment{}).Error
if err != nil {
return err
}
err = DB.AutoMigrate(&model.User{}).Error
if err != nil {
return err
}
err = DB.AutoMigrate(&model.Event{}).Error
if err != nil {
return err
}
err = DB.AutoMigrate(&model.Session{}).Error
if err != nil {
return err
}
err = DB.AutoMigrate(&model.Subscription{}).Error
if err != nil {
return err
}
err = DB.AutoMigrate(&model.Comment{}).Error
if err != nil {
return err
}
return nil
}
// LoadEnvironment for testing
func LoadEnvironment() error {
err := godotenv.Load(os.ExpandEnv("../.env"))
if err != nil {
return err
}
return nil
}
// GetID returns an ID
func GetID() uuid.UUID {
ID, _ := uuid.NewV4()
return ID
}