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.

115 lines
3.4 KiB

  1. package system
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "github.com/flipped-aurora/gin-vue-admin/server/config"
  6. "github.com/flipped-aurora/gin-vue-admin/server/global"
  7. model "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  8. "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  9. "github.com/flipped-aurora/gin-vue-admin/server/source/example"
  10. "github.com/flipped-aurora/gin-vue-admin/server/source/system"
  11. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  12. uuid "github.com/satori/go.uuid"
  13. "gorm.io/driver/mysql"
  14. "gorm.io/gorm"
  15. "path/filepath"
  16. )
  17. // writeMysqlConfig mysql回写配置
  18. // Author [SliverHorn](https://github.com/SliverHorn)
  19. // Author [songzhibin97](https://github.com/songzhibin97)
  20. func (initDBService *InitDBService) writeMysqlConfig(mysql config.Mysql) error {
  21. global.GVA_CONFIG.Mysql = mysql
  22. cs := utils.StructToMap(global.GVA_CONFIG)
  23. for k, v := range cs {
  24. global.GVA_VP.Set(k, v)
  25. }
  26. global.GVA_VP.Set("jwt.signing-key", uuid.NewV4())
  27. return global.GVA_VP.WriteConfig()
  28. }
  29. // createDatabase 创建数据库(mysql)
  30. // Author [SliverHorn](https://github.com/SliverHorn)
  31. // Author: [songzhibin97](https://github.com/songzhibin97)
  32. func (initDBService *InitDBService) createDatabase(dsn string, driver string, createSql string) error {
  33. db, err := sql.Open(driver, dsn)
  34. if err != nil {
  35. return err
  36. }
  37. defer func(db *sql.DB) {
  38. err = db.Close()
  39. if err != nil {
  40. fmt.Println(err)
  41. }
  42. }(db)
  43. if err = db.Ping(); err != nil {
  44. return err
  45. }
  46. _, err = db.Exec(createSql)
  47. return err
  48. }
  49. // initMsqlDB 创建数据库并初始化 mysql
  50. // Author [piexlmax](https://github.com/piexlmax)
  51. // Author [SliverHorn](https://github.com/SliverHorn)
  52. // Author: [songzhibin97](https://github.com/songzhibin97)
  53. func (initDBService *InitDBService) initMsqlDB(conf request.InitDB) error {
  54. dsn := conf.MysqlEmptyDsn()
  55. createSql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s` DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci;", conf.DBName)
  56. if err := initDBService.createDatabase(dsn, "mysql", createSql); err != nil {
  57. return err
  58. } // 创建数据库
  59. mysqlConfig := conf.ToMysqlConfig()
  60. if mysqlConfig.Dbname == "" {
  61. return nil
  62. } // 如果没有数据库名, 则跳出初始化数据
  63. if db, err := gorm.Open(mysql.New(mysql.Config{
  64. DSN: mysqlConfig.Dsn(), // DSN data source name
  65. DefaultStringSize: 191, // string 类型字段的默认长度
  66. SkipInitializeWithVersion: true, // 根据版本自动配置
  67. }), &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}); err != nil {
  68. return nil
  69. } else {
  70. global.GVA_DB = db
  71. }
  72. if err := initDBService.initTables(); err != nil {
  73. global.GVA_DB = nil
  74. return err
  75. }
  76. if err := initDBService.initMysqlData(); err != nil {
  77. global.GVA_DB = nil
  78. return err
  79. }
  80. if err := initDBService.writeMysqlConfig(mysqlConfig); err != nil {
  81. return err
  82. }
  83. global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..")
  84. return nil
  85. }
  86. // initData mysql 初始化数据
  87. // Author [SliverHorn](https://github.com/SliverHorn)
  88. // Author: [songzhibin97](https://github.com/songzhibin97)
  89. func (initDBService *InitDBService) initMysqlData() error {
  90. return model.MysqlDataInitialize(
  91. system.Api,
  92. system.User,
  93. system.Casbin,
  94. system.BaseMenu,
  95. system.Authority,
  96. system.Dictionary,
  97. system.UserAuthority,
  98. system.DataAuthorities,
  99. system.AuthoritiesMenus,
  100. system.DictionaryDetail,
  101. system.ViewAuthorityMenuMysql,
  102. example.File,
  103. )
  104. }