Encrypted messaging app
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.

65 lines
1007 B

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