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.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "github.com/OpenIMSDK/OpenKF/server/internal/config"
  18. "github.com/OpenIMSDK/OpenKF/server/internal/conn/db"
  19. customerroles "github.com/OpenIMSDK/OpenKF/server/internal/models/customer_roles"
  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 tables.
  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. customerroles.CustomerSlack{},
  49. }
  50. // drop tables if exist.
  51. if isDrop {
  52. for i := 0; i < len(tables); i++ {
  53. if db.Migrator().HasTable(&tables[i]) && db.Migrator().DropTable(&tables[i]) != nil {
  54. log.Panicf("OpenKF Table Migration", "Drop table %T... failed", tables[i])
  55. }
  56. log.Infof("OpenKF Table Migration", "Drop table %T... ok", tables[i])
  57. }
  58. }
  59. // migrate tables.
  60. for i := 0; i < len(tables); i++ {
  61. if db.AutoMigrate(&tables[i]) != nil {
  62. log.Panicf("OpenKF Table Migration", "Migrate table %T... failed", tables[i])
  63. }
  64. log.Infof("OpenKF Table Migration", "Migrate table %T... ok", tables[i])
  65. }
  66. }