models.go 645 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package model
  2. import (
  3. "log"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "strings"
  8. "gorm.io/driver/sqlite"
  9. "gorm.io/gorm"
  10. )
  11. var db *gorm.DB
  12. type Model struct {
  13. gorm.Model
  14. }
  15. func getCurrentPath() string {
  16. s, err := exec.LookPath(os.Args[0])
  17. if err != nil {
  18. log.Println(err)
  19. }
  20. i := strings.LastIndex(s, "\\")
  21. path := string(s[0 : i+1])
  22. return path
  23. }
  24. func Setup() {
  25. var err error
  26. db, err = gorm.Open(sqlite.Open(filepath.Join(getCurrentPath(), "database.db")), &gorm.Config{})
  27. log.Println(filepath.Join(getCurrentPath(), "database.db"))
  28. if err != nil {
  29. log.Println(err)
  30. }
  31. // Migrate the schema
  32. db.AutoMigrate(&ConfigBackup{})
  33. }