finclip-app-manager/infrastructure/db/repo/mysql/mysql.go

140 lines
3.6 KiB
Go

package mysql
import (
"finclip-app-manager/domain/repository"
"finclip-app-manager/infrastructure/config"
"finclip-app-manager/infrastructure/db/entity/sql"
"finclip-app-manager/infrastructure/logger"
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var (
log = logger.GetLogger()
DB *gorm.DB
)
func StartUp(url string) {
var err error
/*dbConfig := &gorm.Config{}
DB, err = gorm.Open(mysql.Open(url), dbConfig)
if err != nil {
panic(err)
}*/
DB, err = gorm.Open(mysql.New(mysql.Config{
DriverName: config.Cfg.DriverName,
DSN: url, // data source name, refer https://github.com/go-sql-driver/mysql#dsn-data-source-name
}), &gorm.Config{})
if err != nil {
panic(err)
}
DB.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1;")
if !DB.Debug().Migrator().HasTable(&sql.AppV2{}) {
err = DB.Omit("Languages").AutoMigrate(&sql.AppV2{})
}
if !DB.Debug().Migrator().HasTable(&sql.AppVersionV2{}) {
err = DB.Omit("Languages").AutoMigrate(&sql.AppVersionV2{})
//DB.Model(&sql.AppVersion{}).AddUniqueIndex("idx_user_name", "name")
}
if !DB.Debug().Migrator().HasTable(&sql.AppStatusInfo{}) {
err = DB.Omit("Languages").AutoMigrate(&sql.AppStatusInfo{})
}
if !DB.Debug().Migrator().HasTable(&sql.AppBuildInfo{}) {
err = DB.Omit("Languages").AutoMigrate(&sql.AppBuildInfo{})
}
if !DB.Debug().Migrator().HasTable(&sql.AppVersionStatusInfo{}) {
err = DB.Omit("Languages").AutoMigrate(&sql.AppVersionStatusInfo{})
}
if !DB.Debug().Migrator().HasTable(&BundleInfo{}) {
err = DB.Omit("Languages").AutoMigrate(&BundleInfo{})
}
if !DB.Debug().Migrator().HasTable(&LinkAppInfo{}) {
err = DB.Omit("Languages").AutoMigrate(&LinkAppInfo{})
}
if !DB.Debug().Migrator().HasTable(&BindingMysqlV2{}) {
err = DB.Omit("Languages").AutoMigrate(&BindingMysqlV2{})
}
if !DB.Debug().Migrator().HasTable(&Bundle{}) {
err = DB.Omit("Languages").AutoMigrate(&Bundle{})
}
if !DB.Debug().Migrator().HasTable(&LinkAuditMysql{}) {
err = DB.Omit("Languages").AutoMigrate(&LinkAuditMysql{})
}
if !DB.Debug().Migrator().HasTable(&MenuInfoMysql{}) {
err = DB.Omit("Languages").AutoMigrate(&MenuInfoMysql{})
}
if !DB.Debug().Migrator().HasTable(&QrCodeInfoMysql{}) {
err = DB.Omit("Languages").AutoMigrate(&QrCodeInfoMysql{})
}
if !DB.Debug().Migrator().HasTable(&TypeConfigMysql{}) {
err = DB.Omit("Languages").AutoMigrate(&TypeConfigMysql{})
}
if !DB.Debug().Migrator().HasTable(&AppTempInfoMysql{}) {
err = DB.Omit("Languages").AutoMigrate(&AppTempInfoMysql{})
}
if !DB.Debug().Migrator().HasTable(&sql.PrivacySettingInfo{}) {
err = DB.Omit("Languages").AutoMigrate(&sql.PrivacySettingInfo{})
}
if !DB.Debug().Migrator().HasTable(&repository.AppOperConfig{}) {
err = DB.Omit("Languages").AutoMigrate(&repository.AppOperConfig{})
}
//
/*err = DB.Debug().AutoMigrate(
&sql.App{},
&sql.AppStatusInfo{},
&sql.AppVersion{},
&sql.AppBuildInfo{},
&sql.AppVersionStatusInfo{},
&AppTempInfoMysql{},
&BindingMysql{},
&BundleInfo{},
&LinkAppInfo{},
&LinkAuditMysql{},
&MenuInfoMysql{},
&QrCodeInfoMysql{},
&TypeConfigMysql{},
&BundleMysql{},
)
if err != nil {
panic(err)
}*/
fmt.Println("start up mysql success ...")
}
func DbNotFound(err error) bool {
return err == gorm.ErrRecordNotFound
}
func genQuery(searchStr, assKey, cond string) string {
if searchStr != "" {
searchStr += " " + assKey + " " + cond
} else {
searchStr = cond
}
return searchStr
}
func genLike(searchText string) string {
return "%" + searchText + "%"
}
func genOffset(pageNo, pageSize int) int {
return (pageNo - 1) * pageSize
}