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.

88 lines
2.4 KiB

3 years ago
  1. package system
  2. import (
  3. "path/filepath"
  4. "github.com/flipped-aurora/gin-vue-admin/server/config"
  5. "github.com/flipped-aurora/gin-vue-admin/server/global"
  6. model "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  7. "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  8. "github.com/flipped-aurora/gin-vue-admin/server/source/example"
  9. "github.com/flipped-aurora/gin-vue-admin/server/source/system"
  10. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  11. uuid "github.com/satori/go.uuid"
  12. "gorm.io/driver/postgres"
  13. "gorm.io/gorm"
  14. )
  15. // writePgsqlConfig pgsql 回写配置
  16. // Author [SliverHorn](https://github.com/SliverHorn)
  17. func (initDBService *InitDBService) writePgsqlConfig(pgsql config.Pgsql) error {
  18. global.GVA_CONFIG.System.DbType = "pgsql"
  19. global.GVA_CONFIG.Pgsql = pgsql
  20. cs := utils.StructToMap(global.GVA_CONFIG)
  21. for k, v := range cs {
  22. global.GVA_VP.Set(k, v)
  23. }
  24. global.GVA_VP.Set("jwt.signing-key", uuid.NewV4().String())
  25. return global.GVA_VP.WriteConfig()
  26. }
  27. func (initDBService *InitDBService) initPgsqlDB(conf request.InitDB) error {
  28. dsn := conf.PgsqlEmptyDsn()
  29. createSql := "CREATE DATABASE " + conf.DBName
  30. if err := initDBService.createDatabase(dsn, "pgx", createSql); err != nil {
  31. return err
  32. } // 创建数据库
  33. pgsqlConfig := conf.ToPgsqlConfig()
  34. if pgsqlConfig.Dbname == "" {
  35. return nil
  36. } // 如果没有数据库名, 则跳出初始化数据
  37. if db, err := gorm.Open(postgres.New(postgres.Config{
  38. DSN: pgsqlConfig.Dsn(), // DSN data source name
  39. PreferSimpleProtocol: false,
  40. }), &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}); err != nil {
  41. return nil
  42. } else {
  43. global.GVA_DB = db
  44. }
  45. if err := initDBService.initTables(); err != nil {
  46. global.GVA_DB = nil
  47. return err
  48. }
  49. if err := initDBService.initPgsqlData(); err != nil {
  50. global.GVA_DB = nil
  51. return err
  52. }
  53. if err := initDBService.writePgsqlConfig(pgsqlConfig); err != nil {
  54. return err
  55. }
  56. global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..")
  57. return nil
  58. }
  59. // initPgsqlData pgsql 初始化数据
  60. // Author [SliverHorn](https://github.com/SliverHorn)
  61. func (initDBService *InitDBService) initPgsqlData() error {
  62. return model.PgsqlDataInitialize(
  63. system.Api,
  64. system.User,
  65. system.Casbin,
  66. system.BaseMenu,
  67. system.Authority,
  68. system.Dictionary,
  69. system.UserAuthority,
  70. system.DataAuthorities,
  71. system.AuthoritiesMenus,
  72. system.DictionaryDetail,
  73. system.ViewAuthorityMenuPostgres,
  74. example.FilePgsql,
  75. )
  76. }