init.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/dto"
  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 *dto.Context, sql string) string {
  37. if ctx != nil {
  38. logId := ctx.GetValue(dto.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 _, ok := existTable[tableName]; !ok {
  63. _, err = Instance.Exec(createSQL)
  64. log.Infof("Create Table: %s", createSQL)
  65. if err != nil {
  66. panic(err)
  67. }
  68. if initData, ok := config.Instance.TablesInitData[tableName]; ok {
  69. _, err = Instance.Exec(initData)
  70. log.Infof("Init Table: %s", initData)
  71. if err != nil {
  72. panic(err)
  73. }
  74. }
  75. }
  76. }
  77. }
  78. type tableSQL struct {
  79. Table string `db:"Table"`
  80. CreateTable string `db:"Create Table"`
  81. }
  82. func databaseUpdate() {
  83. // 检查email表是否有group id
  84. var err error
  85. var res []tableSQL
  86. if config.Instance.DbType == "sqlite" {
  87. err = Instance.Select(&res, "select sql as `Create Table` from sqlite_master where type='table' and tbl_name = 'email'")
  88. } else {
  89. err = Instance.Select(&res, "show create table `email`")
  90. }
  91. if err != nil {
  92. panic(err)
  93. }
  94. if len(res) > 0 && !strings.Contains(res[0].CreateTable, "group_id") {
  95. Instance.Exec("alter table email add group_id integer default 0 not null;")
  96. }
  97. }