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

  1. package Database
  2. import (
  3. "log"
  4. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models"
  5. "gorm.io/driver/postgres"
  6. "gorm.io/gorm"
  7. )
  8. const dbUrl = "postgres://postgres:@localhost:5432/sudden_impact_records"
  9. const dbTestUrl = "postgres://postgres:@localhost:5432/sudden_impact_records_test"
  10. var (
  11. DB *gorm.DB
  12. )
  13. func GetModels() []interface{} {
  14. return []interface{}{
  15. &Models.User{},
  16. &Models.PostImage{},
  17. &Models.PostVideo{},
  18. &Models.PostAudio{},
  19. &Models.PostLink{},
  20. &Models.Post{},
  21. &Models.SubscriptionEmailAttachment{},
  22. &Models.SubscriptionEmail{},
  23. &Models.Subscription{},
  24. }
  25. }
  26. func Init() {
  27. var (
  28. model interface{}
  29. err error
  30. )
  31. log.Println("Initializing database...")
  32. DB, err = gorm.Open(postgres.Open(dbUrl), &gorm.Config{})
  33. if err != nil {
  34. log.Fatalln(err)
  35. }
  36. log.Println("Running AutoMigrate...")
  37. for _, model = range GetModels() {
  38. DB.AutoMigrate(model)
  39. }
  40. }
  41. func InitTest() {
  42. var (
  43. model interface{}
  44. err error
  45. )
  46. DB, err = gorm.Open(postgres.Open(dbTestUrl), &gorm.Config{})
  47. if err != nil {
  48. log.Fatalln(err)
  49. }
  50. for _, model = range GetModels() {
  51. DB.Migrator().DropTable(model)
  52. DB.AutoMigrate(model)
  53. }
  54. }