init.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package db
  2. import (
  3. "fmt"
  4. _ "github.com/go-sql-driver/mysql"
  5. "github.com/jmoiron/sqlx"
  6. log "github.com/sirupsen/logrus"
  7. _ "modernc.org/sqlite"
  8. "pmail/config"
  9. "pmail/utils/context"
  10. "pmail/utils/errors"
  11. "strings"
  12. )
  13. var Instance *sqlx.DB
  14. func Init() error {
  15. dsn := config.Instance.DbDSN
  16. var err error
  17. switch config.Instance.DbType {
  18. case "mysql":
  19. Instance, err = sqlx.Open("mysql", dsn)
  20. case "sqlite":
  21. Instance, err = sqlx.Open("sqlite", dsn)
  22. default:
  23. return errors.New("Database Type Error!")
  24. }
  25. if err != nil {
  26. return errors.Wrap(err)
  27. }
  28. Instance.SetMaxOpenConns(100)
  29. Instance.SetMaxIdleConns(10)
  30. //showMySQLCharacterSet()
  31. checkTable()
  32. // 处理版本升级带来的数据表变更
  33. databaseUpdate()
  34. return nil
  35. }
  36. func WithContext(ctx *context.Context, sql string) string {
  37. if ctx != nil {
  38. logId := ctx.GetValue(context.LogID)
  39. return fmt.Sprintf("/* %s */ %s", logId, sql)
  40. }
  41. return sql
  42. }
  43. type tables struct {
  44. TablesInPmail string `db:"Tables_in_pmail"`
  45. }
  46. func checkTable() {
  47. var res []*tables
  48. var err error
  49. if config.Instance.DbType == "sqlite" {
  50. err = Instance.Select(&res, "select name as `Tables_in_pmail` from sqlite_master where type='table'")
  51. } else {
  52. err = Instance.Select(&res, "show tables")
  53. }
  54. if err != nil {
  55. panic(err)
  56. }
  57. existTable := map[string]struct{}{}
  58. for _, tableName := range res {
  59. existTable[tableName.TablesInPmail] = struct{}{}
  60. }
  61. for tableName, createSQL := range config.Instance.Tables {
  62. if createSQL == "" {
  63. continue
  64. }
  65. if _, ok := existTable[tableName]; !ok {
  66. _, err = Instance.Exec(createSQL)
  67. log.Infof("Create Table: %s", createSQL)
  68. if err != nil {
  69. panic(err)
  70. }
  71. if initData, ok := config.Instance.TablesInitData[tableName]; ok {
  72. if initData != "" {
  73. _, err = Instance.Exec(initData)
  74. log.Infof("Init Table: %s", initData)
  75. if err != nil {
  76. panic(err)
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. type tableSQL struct {
  84. Table string `db:"Table"`
  85. CreateTable string `db:"Create Table"`
  86. }
  87. func databaseUpdate() {
  88. // 检查email表是否有group id
  89. var err error
  90. var res []tableSQL
  91. if config.Instance.DbType == "sqlite" {
  92. err = Instance.Select(&res, "select sql as `Create Table` from sqlite_master where type='table' and tbl_name = 'email'")
  93. } else {
  94. err = Instance.Select(&res, "show create table `email`")
  95. }
  96. if err != nil {
  97. panic(err)
  98. }
  99. if len(res) > 0 && !strings.Contains(res[0].CreateTable, "group_id") {
  100. Instance.Exec("alter table email add group_id integer default 0 not null;")
  101. }
  102. }