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.

70 lines
1.1 KiB

package Database
import (
"log"
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
const dbUrl = "postgres://postgres:@localhost:5432/sudden_impact_records"
const dbTestUrl = "postgres://postgres:@localhost:5432/sudden_impact_records_test"
var (
DB *gorm.DB
)
func GetModels() []interface{} {
return []interface{}{
&Models.User{},
&Models.PostImage{},
&Models.PostVideo{},
&Models.PostAudio{},
&Models.PostLink{},
&Models.Post{},
&Models.SubscriptionEmailAttachment{},
&Models.SubscriptionEmail{},
&Models.Subscription{},
}
}
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)
}
}