Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
990 B

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.User{},
&Models.MessageData{},
&Models.MessageKey{},
}
}
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)
}
}