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) } }