feat: 去掉 model.database
This commit is contained in:
committed by
zhengkunwang223
parent
5f9f728d13
commit
24f61b0a4e
@@ -1,10 +0,0 @@
|
|||||||
package model
|
|
||||||
|
|
||||||
type AppDatabase struct {
|
|
||||||
BaseModel
|
|
||||||
AppInstallId uint `json:"appInstallId" gorm:"type:integer;not null"`
|
|
||||||
Key string `json:"key" gorm:"type:varchar(64);not null"`
|
|
||||||
Dbname string `json:"dbname" gorm:"type:varchar(256);not null"`
|
|
||||||
Username string `json:"username" gorm:"type:varchar(256);not null"`
|
|
||||||
Password string `json:"password" gorm:"type:varchar(256);not null"`
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
package repo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
type DatabaseRepo struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d DatabaseRepo) ByAppInstallId(installId uint) DBOption {
|
|
||||||
return func(db *gorm.DB) *gorm.DB {
|
|
||||||
return db.Where("app_install_id = ?", installId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d DatabaseRepo) Create(ctx context.Context, database *model.AppDatabase) error {
|
|
||||||
return getTx(ctx).Model(&model.AppDatabase{}).Create(&database).Error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d DatabaseRepo) DeleteBy(ctx context.Context, opts ...DBOption) error {
|
|
||||||
return getTx(ctx, opts...).Model(&model.AppDatabase{}).Delete(&model.AppDatabase{}).Error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d DatabaseRepo) GetBy(opts ...DBOption) ([]model.AppDatabase, error) {
|
|
||||||
db := getDb(opts...)
|
|
||||||
var databases []model.AppDatabase
|
|
||||||
err := db.Find(&databases).Error
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return databases, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d DatabaseRepo) GetFirst(opts ...DBOption) (model.AppDatabase, error) {
|
|
||||||
db := getDb(opts...)
|
|
||||||
var database model.AppDatabase
|
|
||||||
err := db.Find(&database).Error
|
|
||||||
if err != nil {
|
|
||||||
return database, err
|
|
||||||
}
|
|
||||||
return database, nil
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package repo
|
package repo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
@@ -16,8 +17,8 @@ type IMysqlRepo interface {
|
|||||||
WithByMysqlName(mysqlName string) DBOption
|
WithByMysqlName(mysqlName string) DBOption
|
||||||
List(opts ...DBOption) ([]model.DatabaseMysql, error)
|
List(opts ...DBOption) ([]model.DatabaseMysql, error)
|
||||||
Page(limit, offset int, opts ...DBOption) (int64, []model.DatabaseMysql, error)
|
Page(limit, offset int, opts ...DBOption) (int64, []model.DatabaseMysql, error)
|
||||||
Create(mysql *model.DatabaseMysql) error
|
Create(ctx context.Context, mysql *model.DatabaseMysql) error
|
||||||
Delete(opts ...DBOption) error
|
Delete(ctx context.Context, opts ...DBOption) error
|
||||||
Update(id uint, vars map[string]interface{}) error
|
Update(id uint, vars map[string]interface{}) error
|
||||||
LoadRunningVersion(keys []string) ([]string, error)
|
LoadRunningVersion(keys []string) ([]string, error)
|
||||||
LoadBaseInfoByName(name string) (*RootInfo, error)
|
LoadBaseInfoByName(name string) (*RootInfo, error)
|
||||||
@@ -165,16 +166,12 @@ func (u *MysqlRepo) LoadRedisBaseInfo() (*RootInfo, error) {
|
|||||||
return &info, nil
|
return &info, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *MysqlRepo) Create(mysql *model.DatabaseMysql) error {
|
func (u *MysqlRepo) Create(ctx context.Context, mysql *model.DatabaseMysql) error {
|
||||||
return global.DB.Create(mysql).Error
|
return getTx(ctx).Create(mysql).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *MysqlRepo) Delete(opts ...DBOption) error {
|
func (u *MysqlRepo) Delete(ctx context.Context, opts ...DBOption) error {
|
||||||
db := global.DB
|
return getTx(ctx, opts...).Delete(&model.DatabaseMysql{}).Error
|
||||||
for _, opt := range opts {
|
|
||||||
db = opt(db)
|
|
||||||
}
|
|
||||||
return db.Delete(&model.DatabaseMysql{}).Error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *MysqlRepo) Update(id uint, vars map[string]interface{}) error {
|
func (u *MysqlRepo) Update(id uint, vars map[string]interface{}) error {
|
||||||
@@ -188,7 +185,7 @@ func (u *MysqlRepo) UpdateDatabaseInfo(id uint, vars map[string]interface{}) err
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MysqlRepo) WithByMysqlName(mysqlName string) DBOption {
|
func (u *MysqlRepo) WithByMysqlName(mysqlName string) DBOption {
|
||||||
return func(g *gorm.DB) *gorm.DB {
|
return func(g *gorm.DB) *gorm.DB {
|
||||||
return g.Where("mysql_name = ?", mysqlName)
|
return g.Where("mysql_name = ?", mysqlName)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ type RepoGroup struct {
|
|||||||
AppDetailRepo
|
AppDetailRepo
|
||||||
AppInstallRepo
|
AppInstallRepo
|
||||||
AppInstallResourceRpo
|
AppInstallResourceRpo
|
||||||
DatabaseRepo
|
|
||||||
AppInstallBackupRepo
|
AppInstallBackupRepo
|
||||||
ImageRepoRepo
|
ImageRepoRepo
|
||||||
ComposeTemplateRepo
|
ComposeTemplateRepo
|
||||||
|
|||||||
@@ -34,19 +34,19 @@ var (
|
|||||||
Delete DatabaseOp = "delete"
|
Delete DatabaseOp = "delete"
|
||||||
)
|
)
|
||||||
|
|
||||||
func execDockerCommand(database model.AppDatabase, dbInstall model.AppInstall, op DatabaseOp) error {
|
func execDockerCommand(database model.DatabaseMysql, dbInstall model.AppInstall, op DatabaseOp) error {
|
||||||
var auth dto.AuthParam
|
var auth dto.AuthParam
|
||||||
var dbConfig dto.AppDatabase
|
var dbConfig dto.AppDatabase
|
||||||
dbConfig.Password = database.Password
|
dbConfig.Password = database.Password
|
||||||
dbConfig.DbUser = database.Username
|
dbConfig.DbUser = database.Username
|
||||||
dbConfig.DbName = database.Dbname
|
dbConfig.DbName = database.Name
|
||||||
_ = json.Unmarshal([]byte(dbInstall.Param), &auth)
|
_ = json.Unmarshal([]byte(dbInstall.Param), &auth)
|
||||||
execConfig := dto.ContainerExec{
|
execConfig := dto.ContainerExec{
|
||||||
ContainerName: dbInstall.ContainerName,
|
ContainerName: dbInstall.ContainerName,
|
||||||
Auth: auth,
|
Auth: auth,
|
||||||
DbParam: dbConfig,
|
DbParam: dbConfig,
|
||||||
}
|
}
|
||||||
out, err := cmd.Exec(getSqlStr(database.Key, op, execConfig))
|
out, err := cmd.Exec(getSqlStr(dbInstall.Version, op, execConfig))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New(out)
|
return errors.New(out)
|
||||||
}
|
}
|
||||||
@@ -59,7 +59,7 @@ func getSqlStr(version string, operate DatabaseOp, exec dto.ContainerExec) strin
|
|||||||
|
|
||||||
if strings.Contains(version, "5.7") {
|
if strings.Contains(version, "5.7") {
|
||||||
if operate == Add {
|
if operate == Add {
|
||||||
str = fmt.Sprintf("docker exec -i %s mysql -uroot -p%s -e \"CREATE USER '%s'@'%%' IDENTIFIED BY '%s';\" -e \"create database %s;\" -e \"GRANT ALL ON %s.* TO '%s'@'%%' IDENTIFIED BY '%s';\" -e \"FLUSH PRIVILEGES;\"",
|
str = fmt.Sprintf("docker exec -i %s mysql -uroot -p%s -e \"CREATE USER IF NOT EXISTS '%s'@'%%' IDENTIFIED BY '%s';\" -e \"create database %s;\" -e \"GRANT ALL ON %s.* TO '%s'@'%%' IDENTIFIED BY '%s';\" -e \"FLUSH PRIVILEGES;\"",
|
||||||
exec.ContainerName, exec.Auth.RootPassword, param.DbUser, param.Password, param.DbName, param.DbName, param.DbUser, param.Password)
|
exec.ContainerName, exec.Auth.RootPassword, param.DbUser, param.Password, param.DbName, param.DbName, param.DbUser, param.Password)
|
||||||
}
|
}
|
||||||
if operate == Delete {
|
if operate == Delete {
|
||||||
@@ -70,7 +70,7 @@ func getSqlStr(version string, operate DatabaseOp, exec dto.ContainerExec) strin
|
|||||||
|
|
||||||
if strings.Contains(version, "8.0") {
|
if strings.Contains(version, "8.0") {
|
||||||
if operate == Add {
|
if operate == Add {
|
||||||
str = fmt.Sprintf("docker exec -i %s mysql -uroot -p%s -e \"CREATE USER '%s'@'%%' IDENTIFIED BY '%s';\" -e \"create database %s;\" -e \"GRANT ALL ON %s.* TO '%s'@'%%';\" -e \"FLUSH PRIVILEGES;\"",
|
str = fmt.Sprintf("docker exec -i %s mysql -uroot -p%s -e \"CREATE USER IF NOT EXISTS '%s'@'%%' IDENTIFIED BY '%s';\" -e \"create database %s;\" -e \"GRANT ALL ON %s.* TO '%s'@'%%';\" -e \"FLUSH PRIVILEGES;\"",
|
||||||
exec.ContainerName, exec.Auth.RootPassword, param.DbUser, param.Password, param.DbName, param.DbName, param.DbUser)
|
exec.ContainerName, exec.Auth.RootPassword, param.DbUser, param.Password, param.DbName, param.DbName, param.DbUser)
|
||||||
}
|
}
|
||||||
if operate == Delete {
|
if operate == Delete {
|
||||||
@@ -128,15 +128,25 @@ func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var database model.AppDatabase
|
var database model.DatabaseMysql
|
||||||
database.Dbname = dbConfig.DbName
|
database.Name = dbConfig.DbName
|
||||||
database.Username = dbConfig.DbUser
|
database.Username = dbConfig.DbUser
|
||||||
database.Password = dbConfig.Password
|
database.Password = dbConfig.Password
|
||||||
database.AppInstallId = dbInstall.ID
|
database.MysqlName = dbInstall.Name
|
||||||
database.Key = dbInstall.App.Key
|
database.Format = "utf8mb4"
|
||||||
if err := dataBaseRepo.Create(ctx, &database); err != nil {
|
database.Permission = "127.0.0.1"
|
||||||
|
if err := mysqlRepo.Create(ctx, &database); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
//var database model.AppDatabase
|
||||||
|
//database.Dbname = dbConfig.DbName
|
||||||
|
//database.Username = dbConfig.DbUser
|
||||||
|
//database.Password = dbConfig.Password
|
||||||
|
//database.AppInstallId = dbInstall.ID
|
||||||
|
//database.Key = dbInstall.App.Key
|
||||||
|
//if err := dataBaseRepo.Create(ctx, &database); err != nil {
|
||||||
|
// return err
|
||||||
|
//}
|
||||||
var installResource model.AppInstallResource
|
var installResource model.AppInstallResource
|
||||||
installResource.ResourceId = database.ID
|
installResource.ResourceId = database.ID
|
||||||
installResource.AppInstallId = appInstall.ID
|
installResource.AppInstallId = appInstall.ID
|
||||||
@@ -186,18 +196,18 @@ func deleteLink(ctx context.Context, install *model.AppInstall) error {
|
|||||||
}
|
}
|
||||||
for _, re := range resources {
|
for _, re := range resources {
|
||||||
if re.Key == "mysql" {
|
if re.Key == "mysql" {
|
||||||
database, _ := dataBaseRepo.GetFirst(commonRepo.WithByID(re.ResourceId))
|
database, _ := mysqlRepo.Get(commonRepo.WithByID(re.ResourceId))
|
||||||
if reflect.DeepEqual(database, model.AppDatabase{}) {
|
if reflect.DeepEqual(database, model.DatabaseMysql{}) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(database.AppInstallId))
|
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByName(database.MysqlName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
if err := execDockerCommand(database, appInstall, Delete); err != nil {
|
if err := execDockerCommand(database, appInstall, Delete); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := dataBaseRepo.DeleteBy(ctx, commonRepo.WithByID(database.ID)); err != nil {
|
if err := mysqlRepo.Delete(ctx, commonRepo.WithByID(database.ID)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package service
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -272,7 +273,7 @@ func (u *MysqlService) Create(mysqlDto dto.MysqlDBCreate) error {
|
|||||||
if err := excuteSql(app.ContainerName, app.Password, grantStr); err != nil {
|
if err := excuteSql(app.ContainerName, app.Password, grantStr); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := mysqlRepo.Create(&mysql); err != nil {
|
if err := mysqlRepo.Create(context.TODO(), &mysql); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -336,7 +337,7 @@ func (u *MysqlService) Delete(name string, ids []uint) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ = mysqlRepo.Delete(commonRepo.WithByID(db.ID))
|
_ = mysqlRepo.Delete(context.Background(), commonRepo.WithByID(db.ID))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,6 @@ var (
|
|||||||
tagRepo = repo.RepoGroupApp.TagRepo
|
tagRepo = repo.RepoGroupApp.TagRepo
|
||||||
appInstallRepo = repo.RepoGroupApp.AppInstallRepo
|
appInstallRepo = repo.RepoGroupApp.AppInstallRepo
|
||||||
appInstallResourceRepo = repo.RepoGroupApp.AppInstallResourceRpo
|
appInstallResourceRepo = repo.RepoGroupApp.AppInstallResourceRpo
|
||||||
dataBaseRepo = repo.RepoGroupApp.DatabaseRepo
|
|
||||||
|
|
||||||
mysqlRepo = repo.RepoGroupApp.MysqlRepo
|
mysqlRepo = repo.RepoGroupApp.MysqlRepo
|
||||||
|
|
||||||
|
|||||||
@@ -150,7 +150,7 @@ var AddTableCronjob = &gormigrate.Migration{
|
|||||||
var AddTableApp = &gormigrate.Migration{
|
var AddTableApp = &gormigrate.Migration{
|
||||||
ID: "20200921-add-table-app",
|
ID: "20200921-add-table-app",
|
||||||
Migrate: func(tx *gorm.DB) error {
|
Migrate: func(tx *gorm.DB) error {
|
||||||
return tx.AutoMigrate(&model.App{}, &model.AppDetail{}, &model.Tag{}, &model.AppTag{}, &model.AppInstall{}, &model.AppInstallResource{}, &model.AppDatabase{}, &model.AppInstallBackup{})
|
return tx.AutoMigrate(&model.App{}, &model.AppDetail{}, &model.Tag{}, &model.AppTag{}, &model.AppInstall{}, &model.AppInstallResource{}, &model.AppInstallBackup{})
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user