mirror of
https://github.com/Damillora/Shioriko.git
synced 2024-11-22 20:37:32 +00:00
38 lines
644 B
Go
38 lines
644 B
Go
|
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
|
||
|
}
|