Shioriko/pkg/database/main.go

38 lines
644 B
Go
Raw Normal View History

2021-05-09 15:07:23 +00:00
package database
import (
"log"
"github.com/Damillora/Shioriko/pkg/config"
"gorm.io/gorm"
"gorm.io/driver/postgres"
)
var DB *gorm.DB
func Initialize() {
dsn := config.CurrentConfig.PostgresDatabase
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
db.AutoMigrate(&User{})
db.AutoMigrate(&TagType{})
db.AutoMigrate(&Tag{})
db.AutoMigrate(&Blob{})
db.AutoMigrate(&Post{})
var general TagType
result := db.Where("name = ?", "general").First(&general)
if result.Error != nil {
db.Create(&TagType{
Name: "general",
})
}
if err != nil {
log.Fatal("Unable to connect to database" + err.Error())
}
DB = db
}