2021-05-09 15:07:23 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/Damillora/Shioriko/pkg/config"
|
2021-05-11 11:00:05 +00:00
|
|
|
"github.com/google/uuid"
|
2021-05-09 15:07:23 +00:00
|
|
|
"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",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-11 11:00:05 +00:00
|
|
|
var tagme Tag
|
|
|
|
result = db.Where("name = ?", "tagme").First(&tagme)
|
|
|
|
if result.Error != nil {
|
|
|
|
db.Create(&Tag{
|
|
|
|
ID: uuid.NewString(),
|
|
|
|
Name: "tagme",
|
|
|
|
TagType: general,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-09 15:07:23 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Unable to connect to database" + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
DB = db
|
|
|
|
}
|