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.

75 lines
1.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package system
  2. import (
  3. "database/sql"
  4. "fmt"
  5. adapter "github.com/casbin/gorm-adapter/v3"
  6. "github.com/flipped-aurora/gin-vue-admin/server/global"
  7. "github.com/flipped-aurora/gin-vue-admin/server/model/example"
  8. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  9. "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  10. )
  11. type InitDBService struct{}
  12. // InitDB 创建数据库并初始化 总入口
  13. // Author [piexlmax](https://github.com/piexlmax)
  14. // Author [SliverHorn](https://github.com/SliverHorn)
  15. // Author [songzhibin97](https://github.com/songzhibin97)
  16. func (initDBService *InitDBService) InitDB(conf request.InitDB) error {
  17. switch conf.DBType {
  18. case "mysql":
  19. return initDBService.initMsqlDB(conf)
  20. case "pgsql":
  21. return initDBService.initPgsqlDB(conf)
  22. default:
  23. return initDBService.initMsqlDB(conf)
  24. }
  25. }
  26. // initTables 初始化表
  27. // Author [SliverHorn](https://github.com/SliverHorn)
  28. func (initDBService *InitDBService) initTables() error {
  29. return global.GVA_DB.AutoMigrate(
  30. system.SysApi{},
  31. system.SysUser{},
  32. system.SysBaseMenu{},
  33. system.SysAuthority{},
  34. system.JwtBlacklist{},
  35. system.SysDictionary{},
  36. system.SysAutoCodeHistory{},
  37. system.SysOperationRecord{},
  38. system.SysDictionaryDetail{},
  39. system.SysBaseMenuParameter{},
  40. adapter.CasbinRule{},
  41. example.ExaFile{},
  42. example.ExaCustomer{},
  43. example.ExaFileChunk{},
  44. example.ExaFileUploadAndDownload{},
  45. )
  46. }
  47. // createDatabase 创建数据库(mysql)
  48. // Author [SliverHorn](https://github.com/SliverHorn)
  49. // Author: [songzhibin97](https://github.com/songzhibin97)
  50. func (initDBService *InitDBService) createDatabase(dsn string, driver string, createSql string) error {
  51. db, err := sql.Open(driver, dsn)
  52. if err != nil {
  53. return err
  54. }
  55. defer func(db *sql.DB) {
  56. err = db.Close()
  57. if err != nil {
  58. fmt.Println(err)
  59. }
  60. }(db)
  61. if err = db.Ping(); err != nil {
  62. return err
  63. }
  64. _, err = db.Exec(createSql)
  65. return err
  66. }