You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 2.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright © 2023 OpenIM open source community. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "flag"
  17. "reflect"
  18. "github.com/OpenIMSDK/OpenKF/server/internal/config"
  19. "github.com/OpenIMSDK/OpenKF/server/internal/conn/db"
  20. systemroles "github.com/OpenIMSDK/OpenKF/server/internal/models/system_roles"
  21. "github.com/OpenIMSDK/OpenKF/server/internal/utils"
  22. "github.com/OpenIMSDK/OpenKF/server/pkg/log"
  23. )
  24. var (
  25. configPath string
  26. isDrop bool
  27. )
  28. func init() {
  29. // arg
  30. flag.StringVar(&configPath, "c", "../../config.yaml", "config file path")
  31. flag.BoolVar(&isDrop, "d", false, "drop table if exist")
  32. flag.Parse()
  33. config.ConfigInit(configPath)
  34. utils.OpenKFBanner()
  35. log.InitLogger()
  36. db.InitMysqlDB()
  37. }
  38. // migrate table.
  39. func main() {
  40. // get db instance.
  41. db := db.GetMysqlDB()
  42. // tables
  43. tables := []interface{}{
  44. systemroles.SysUser{},
  45. systemroles.SysCustomer{},
  46. systemroles.SysCommunity{},
  47. systemroles.SysBot{},
  48. }
  49. // drop tables if exist.
  50. if isDrop {
  51. for _, table := range tables {
  52. if db.Migrator().HasTable(&table) && db.Migrator().DropTable(&table) != nil {
  53. log.Panicf("OpenKF Table Migration", "Drop table %v... failed", reflect.TypeOf(table))
  54. }
  55. log.Infof("OpenKF Table Migration", "Drop table %v... ok", reflect.TypeOf(table))
  56. }
  57. }
  58. // migrate tables.
  59. for _, table := range tables {
  60. err := db.AutoMigrate(&table)
  61. if err != nil {
  62. log.Panicf("OpenKF Table Migration", "Migrate table %v... failed", reflect.TypeOf(table))
  63. }
  64. log.Infof("OpenKF Table Migration", "Migrate table %v... ok", reflect.TypeOf(table))
  65. }
  66. }