server.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package migrate
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strconv"
  6. "text/template"
  7. "time"
  8. "github.com/go-admin-team/go-admin-core/sdk"
  9. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  10. "github.com/go-admin-team/go-admin-core/config/source/file"
  11. "github.com/spf13/cobra"
  12. "git.cestong.com.cn/cecf/hx-admin-server/cmd/migrate/migration"
  13. _ "git.cestong.com.cn/cecf/hx-admin-server/cmd/migrate/migration/version"
  14. _ "git.cestong.com.cn/cecf/hx-admin-server/cmd/migrate/migration/version-local"
  15. "git.cestong.com.cn/cecf/hx-admin-server/common/database"
  16. "git.cestong.com.cn/cecf/hx-admin-server/common/models"
  17. "github.com/go-admin-team/go-admin-core/sdk/config"
  18. )
  19. var (
  20. configYml string
  21. generate bool
  22. goAdmin bool
  23. host string
  24. StartCmd = &cobra.Command{
  25. Use: "migrate",
  26. Short: "Initialize the database",
  27. Example: "go-admin migrate -c config/settings.yml",
  28. Run: func(cmd *cobra.Command, args []string) {
  29. run()
  30. },
  31. }
  32. )
  33. // fixme 在您看不见代码的时候运行迁移,我觉得是不安全的,所以编译后最好不要去执行迁移
  34. func init() {
  35. StartCmd.PersistentFlags().StringVarP(&configYml, "config", "c", "config/settings.yml", "Start server with provided configuration file")
  36. StartCmd.PersistentFlags().BoolVarP(&generate, "generate", "g", false, "generate migration file")
  37. StartCmd.PersistentFlags().BoolVarP(&goAdmin, "goAdmin", "a", false, "generate go-admin migration file")
  38. StartCmd.PersistentFlags().StringVarP(&host, "domain", "d", "*", "select tenant host")
  39. }
  40. func run() {
  41. if !generate {
  42. fmt.Println(`start init`)
  43. //1. 读取配置
  44. config.Setup(
  45. file.NewSource(file.WithPath(configYml)),
  46. initDB,
  47. )
  48. } else {
  49. fmt.Println(`generate migration file`)
  50. _ = genFile()
  51. }
  52. }
  53. func migrateModel() error {
  54. if host == "" {
  55. host = "*"
  56. }
  57. db := sdk.Runtime.GetDbByKey(host)
  58. if config.DatabasesConfig[host].Driver == "mysql" {
  59. //初始化数据库时候用
  60. db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4")
  61. }
  62. err := db.Debug().AutoMigrate(&models.Migration{})
  63. if err != nil {
  64. return err
  65. }
  66. migration.Migrate.SetDb(db.Debug())
  67. migration.Migrate.Migrate()
  68. return err
  69. }
  70. func initDB() {
  71. //3. 初始化数据库链接
  72. database.Setup()
  73. //4. 数据库迁移
  74. fmt.Println("数据库迁移开始")
  75. _ = migrateModel()
  76. fmt.Println(`数据库基础数据初始化成功`)
  77. }
  78. func genFile() error {
  79. t1, err := template.ParseFiles("template/migrate.template")
  80. if err != nil {
  81. return err
  82. }
  83. m := map[string]string{}
  84. m["GenerateTime"] = strconv.FormatInt(time.Now().UnixNano()/1e6, 10)
  85. m["Package"] = "version_local"
  86. if goAdmin {
  87. m["Package"] = "version"
  88. }
  89. var b1 bytes.Buffer
  90. err = t1.Execute(&b1, m)
  91. if goAdmin {
  92. pkg.FileCreate(b1, "./cmd/migrate/migration/version/"+m["GenerateTime"]+"_migrate.go")
  93. } else {
  94. pkg.FileCreate(b1, "./cmd/migrate/migration/version-local/"+m["GenerateTime"]+"_migrate.go")
  95. }
  96. return nil
  97. }