support.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package support
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "regexp"
  8. "strings"
  9. "golang-fave/engine/sqlw"
  10. "golang-fave/engine/utils"
  11. "golang-fave/support/migrate"
  12. )
  13. type Support struct {
  14. }
  15. func New() *Support {
  16. sup := Support{}
  17. return &sup
  18. }
  19. func (this *Support) isSettingsTableDoesntExist(err error) bool {
  20. error_msg := strings.ToLower(err.Error())
  21. if match, _ := regexp.MatchString(`^error 1146`, error_msg); match {
  22. if match, _ := regexp.MatchString(`'[^\.]+\.fave_settings'`, error_msg); match {
  23. if match, _ := regexp.MatchString(`doesn't exist$`, error_msg); match {
  24. return true
  25. }
  26. }
  27. }
  28. return false
  29. }
  30. func (this *Support) Migration(ctx context.Context, dir string) error {
  31. fmt.Printf("[MIGRATION] PLEASE WAIT UNTIL THIS WILL BE DONE!\n")
  32. files, err := ioutil.ReadDir(dir)
  33. if err != nil {
  34. return err
  35. }
  36. for _, file := range files {
  37. if utils.IsDir(dir + string(os.PathSeparator) + file.Name()) {
  38. if err := this.Migrate(ctx, dir+string(os.PathSeparator)+file.Name()); err != nil {
  39. return err
  40. }
  41. }
  42. }
  43. return nil
  44. }
  45. func (this *Support) Migrate(ctx context.Context, host string) error {
  46. mysql_config_file := host + string(os.PathSeparator) + "config" + string(os.PathSeparator) + "mysql.json"
  47. if utils.IsMySqlConfigExists(mysql_config_file) {
  48. mc, err := utils.MySqlConfigRead(mysql_config_file)
  49. if err != nil {
  50. return err
  51. }
  52. db, err := sqlw.Open("mysql", mc.User+":"+mc.Password+"@tcp("+mc.Host+":"+mc.Port+")/"+mc.Name)
  53. if err != nil {
  54. return err
  55. }
  56. if err := db.Ping(ctx); err != nil {
  57. return err
  58. }
  59. defer db.Close()
  60. var table string
  61. if err := db.QueryRow(ctx, `SHOW TABLES LIKE 'settings';`).Scan(&table); err == nil {
  62. if table == "settings" {
  63. if _, err := db.Exec(ctx, `RENAME TABLE settings TO fave_settings;`); err != nil {
  64. return err
  65. }
  66. }
  67. }
  68. var version string
  69. if err := db.QueryRow(ctx, `SELECT value FROM fave_settings WHERE name = 'database_version' LIMIT 1;`).Scan(&version); err != nil {
  70. if this.isSettingsTableDoesntExist(err) {
  71. if _, err := db.Exec(
  72. ctx,
  73. `CREATE TABLE fave_settings (
  74. name varchar(255) NOT NULL COMMENT 'Setting name',
  75. value text NOT NULL COMMENT 'Setting value'
  76. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
  77. ); err != nil {
  78. return err
  79. }
  80. if _, err := db.Exec(
  81. ctx,
  82. `INSERT INTO fave_settings (name, value) VALUES ('database_version', '000000002');`,
  83. ); err != nil {
  84. return err
  85. }
  86. version = "000000002"
  87. err = nil
  88. }
  89. return err
  90. }
  91. return this.Process(ctx, db, version, host)
  92. }
  93. return nil
  94. }
  95. func (this *Support) Process(ctx context.Context, db *sqlw.DB, version string, host string) error {
  96. return migrate.Run(ctx, db, utils.StrToInt(version), host)
  97. }