package Database
|
|
|
|
import (
|
|
"log"
|
|
|
|
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
|
|
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const dbUrl = "postgres://postgres:@localhost:5432/envelope"
|
|
const dbTestUrl = "postgres://postgres:@localhost:5432/envelope_test"
|
|
|
|
var (
|
|
DB *gorm.DB
|
|
)
|
|
|
|
func GetModels() []interface{} {
|
|
return []interface{}{
|
|
&Models.Session{},
|
|
&Models.User{},
|
|
&Models.Friend{},
|
|
&Models.FriendRequest{},
|
|
&Models.MessageData{},
|
|
&Models.Message{},
|
|
&Models.ConversationDetail{},
|
|
&Models.UserConversation{},
|
|
}
|
|
}
|
|
|
|
func Init() {
|
|
var (
|
|
model interface{}
|
|
err error
|
|
)
|
|
|
|
log.Println("Initializing database...")
|
|
|
|
DB, err = gorm.Open(postgres.Open(dbUrl), &gorm.Config{})
|
|
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
log.Println("Running AutoMigrate...")
|
|
|
|
for _, model = range GetModels() {
|
|
DB.AutoMigrate(model)
|
|
}
|
|
}
|
|
|
|
func InitTest() {
|
|
var (
|
|
model interface{}
|
|
err error
|
|
)
|
|
|
|
DB, err = gorm.Open(postgres.Open(dbTestUrl), &gorm.Config{})
|
|
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
for _, model = range GetModels() {
|
|
DB.Migrator().DropTable(model)
|
|
DB.AutoMigrate(model)
|
|
}
|
|
}
|