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.

105 lines
2.9 KiB

  1. package system
  2. import (
  3. "context"
  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. "github.com/jackc/pgx/v4"
  12. uuid "github.com/satori/go.uuid"
  13. "gorm.io/driver/postgres"
  14. "gorm.io/gorm"
  15. "path/filepath"
  16. )
  17. // writePgsqlConfig pgsql 回写配置
  18. // Author [SliverHorn](https://github.com/SliverHorn)
  19. func (initDBService *InitDBService) writePgsqlConfig(pgsql config.Pgsql) error {
  20. global.GVA_CONFIG.Pgsql = pgsql
  21. cs := utils.StructToMap(global.GVA_CONFIG)
  22. for k, v := range cs {
  23. global.GVA_VP.Set(k, v)
  24. }
  25. global.GVA_VP.Set("jwt.signing-key", uuid.NewV4())
  26. return global.GVA_VP.WriteConfig()
  27. }
  28. // createPgsqlDatabase 根据页面传递的数据库名 创建数据库
  29. // Author [SliverHorn](https://github.com/SliverHorn)
  30. func (initDBService *InitDBService) createPgsqlDatabase(dsn string, dbName string) error {
  31. ctx := context.Background()
  32. _sql := "CREATE DATABASE " + dbName
  33. db, err := pgx.Connect(ctx, dsn)
  34. if err != nil {
  35. return err
  36. }
  37. defer func() {
  38. _ = db.Close(ctx)
  39. }()
  40. if err = db.Ping(ctx); err != nil {
  41. return err
  42. }
  43. _, err = db.Exec(ctx, _sql)
  44. return err
  45. }
  46. func (initDBService *InitDBService) initPgsqlDB(conf request.InitDB) error {
  47. dsn := conf.PgsqlEmptyDsn()
  48. if err := initDBService.createPgsqlDatabase(dsn, conf.DBName); err != nil {
  49. return err
  50. } // 创建数据库
  51. pgsqlConfig := conf.ToPgsqlConfig()
  52. if pgsqlConfig.Dbname == "" {
  53. return nil
  54. } // 如果没有数据库名, 则跳出初始化数据
  55. if db, err := gorm.Open(postgres.New(postgres.Config{
  56. DSN: pgsqlConfig.Dsn(), // DSN data source name
  57. PreferSimpleProtocol: false,
  58. }), &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}); err != nil {
  59. return nil
  60. } else {
  61. global.GVA_DB = db
  62. }
  63. if err := initDBService.initTables(); err != nil {
  64. global.GVA_DB = nil
  65. return err
  66. }
  67. if err := initDBService.initPgsqlData(); err != nil {
  68. global.GVA_DB = nil
  69. return err
  70. }
  71. if err := initDBService.writePgsqlConfig(pgsqlConfig); err != nil {
  72. return err
  73. }
  74. global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..")
  75. return nil
  76. }
  77. // initPgsqlData pgsql 初始化数据
  78. // Author [SliverHorn](https://github.com/SliverHorn)
  79. func (initDBService *InitDBService) initPgsqlData() error {
  80. return model.MysqlDataInitialize(
  81. system.Api,
  82. system.User,
  83. system.Casbin,
  84. system.BaseMenu,
  85. system.Authority,
  86. system.Dictionary,
  87. system.UserAuthority,
  88. system.DataAuthorities,
  89. system.AuthoritiesMenus,
  90. system.DictionaryDetail,
  91. system.ViewAuthorityMenuPostgres,
  92. example.File,
  93. )
  94. }