mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 04:22:33 +08:00
feat: 优化数据库模型
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package biz
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/go-rat/utils/crypt"
|
||||
@@ -10,27 +9,25 @@ import (
|
||||
"github.com/TheTNB/panel/internal/app"
|
||||
)
|
||||
|
||||
type DatabaseType string
|
||||
type DatabaseStatus string
|
||||
|
||||
const (
|
||||
DatabaseTypeMysql DatabaseType = "mysql"
|
||||
DatabaseTypePostgresql DatabaseType = "postgresql"
|
||||
DatabaseTypeRedis DatabaseType = "redis"
|
||||
DatabaseStatusNormal DatabaseStatus = "normal"
|
||||
DatabaseStatusInvalid DatabaseStatus = "invalid"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Name string `gorm:"not null;unique" json:"name"`
|
||||
Type DatabaseType `gorm:"not null" json:"type"`
|
||||
Host string `gorm:"not null" json:"host"`
|
||||
Port int `gorm:"not null" json:"port"`
|
||||
Username string `gorm:"not null" json:"username"`
|
||||
Password string `gorm:"not null" json:"password"`
|
||||
Remark string `gorm:"not null" json:"remark"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
DatabaseID uint `gorm:"not null" json:"database_id"`
|
||||
Name string `gorm:"not null" json:"name"`
|
||||
Status DatabaseStatus `gorm:"not null" json:"status"`
|
||||
Username string `gorm:"not null" json:"username"`
|
||||
Password string `gorm:"not null" json:"password"`
|
||||
Remark string `gorm:"not null" json:"remark"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
DatabaseItems []*DatabaseItem `json:"-"`
|
||||
DatabaseServer *DatabaseServer `json:"database_server"`
|
||||
}
|
||||
|
||||
func (r *Database) BeforeSave(tx *gorm.DB) error {
|
||||
@@ -45,7 +42,6 @@ func (r *Database) BeforeSave(tx *gorm.DB) error {
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (r *Database) AfterFind(tx *gorm.DB) error {
|
||||
@@ -61,17 +57,3 @@ func (r *Database) AfterFind(tx *gorm.DB) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Database) BeforeDelete(tx *gorm.DB) error {
|
||||
if r.Name == "local_mysql" && !app.IsCli {
|
||||
return errors.New("can't delete local_mysql, if you must delete it, please uninstall mysql")
|
||||
}
|
||||
if r.Name == "local_postgresql" && !app.IsCli {
|
||||
return errors.New("can't delete local_postgresql, if you must delete it, please uninstall postgresql")
|
||||
}
|
||||
if r.Name == "local_redis" && !app.IsCli {
|
||||
return errors.New("can't delete local_redis, if you must delete it, please uninstall redis")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
package biz
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/go-rat/utils/crypt"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/TheTNB/panel/internal/app"
|
||||
)
|
||||
|
||||
type DatabaseItemStatus string
|
||||
|
||||
const (
|
||||
DatabaseItemStatusNormal DatabaseItemStatus = "normal"
|
||||
DatabaseItemStatusInvalid DatabaseItemStatus = "invalid"
|
||||
)
|
||||
|
||||
type DatabaseItem struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
DatabaseID uint `gorm:"not null" json:"database_id"`
|
||||
Name string `gorm:"not null" json:"name"`
|
||||
Status DatabaseItemStatus `gorm:"not null" json:"status"`
|
||||
Username string `gorm:"not null" json:"username"`
|
||||
Password string `gorm:"not null" json:"password"`
|
||||
Remark string `gorm:"not null" json:"remark"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
Database *Database `json:"database"`
|
||||
}
|
||||
|
||||
func (r *DatabaseItem) BeforeSave(tx *gorm.DB) error {
|
||||
crypter, err := crypt.NewXChacha20Poly1305([]byte(app.Key))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.Password, err = crypter.Encrypt([]byte(r.Password))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *DatabaseItem) AfterFind(tx *gorm.DB) error {
|
||||
crypter, err := crypt.NewXChacha20Poly1305([]byte(app.Key))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
password, err := crypter.Decrypt(r.Password)
|
||||
if err == nil {
|
||||
r.Password = string(password)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
77
internal/biz/database_server.go
Normal file
77
internal/biz/database_server.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package biz
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/go-rat/utils/crypt"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/TheTNB/panel/internal/app"
|
||||
)
|
||||
|
||||
type DatabaseType string
|
||||
|
||||
const (
|
||||
DatabaseTypeMysql DatabaseType = "mysql"
|
||||
DatabaseTypePostgresql DatabaseType = "postgresql"
|
||||
DatabaseTypeRedis DatabaseType = "redis"
|
||||
)
|
||||
|
||||
type DatabaseServer struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Name string `gorm:"not null;unique" json:"name"`
|
||||
Type DatabaseType `gorm:"not null" json:"type"`
|
||||
Host string `gorm:"not null" json:"host"`
|
||||
Port int `gorm:"not null" json:"port"`
|
||||
Username string `gorm:"not null" json:"username"`
|
||||
Password string `gorm:"not null" json:"password"`
|
||||
Remark string `gorm:"not null" json:"remark"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
Databases []*Database `json:"-"`
|
||||
}
|
||||
|
||||
func (r *DatabaseServer) BeforeSave(tx *gorm.DB) error {
|
||||
crypter, err := crypt.NewXChacha20Poly1305([]byte(app.Key))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.Password, err = crypter.Encrypt([]byte(r.Password))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (r *DatabaseServer) AfterFind(tx *gorm.DB) error {
|
||||
crypter, err := crypt.NewXChacha20Poly1305([]byte(app.Key))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
password, err := crypter.Decrypt(r.Password)
|
||||
if err == nil {
|
||||
r.Password = string(password)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *DatabaseServer) BeforeDelete(tx *gorm.DB) error {
|
||||
if r.Name == "local_mysql" && !app.IsCli {
|
||||
return errors.New("can't delete local_mysql, if you must delete it, please uninstall mysql")
|
||||
}
|
||||
if r.Name == "local_postgresql" && !app.IsCli {
|
||||
return errors.New("can't delete local_postgresql, if you must delete it, please uninstall postgresql")
|
||||
}
|
||||
if r.Name == "local_redis" && !app.IsCli {
|
||||
return errors.New("can't delete local_redis, if you must delete it, please uninstall redis")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -17,7 +17,6 @@ func init() {
|
||||
&biz.CertDNS{},
|
||||
&biz.CertAccount{},
|
||||
&biz.Cron{},
|
||||
&biz.Database{},
|
||||
&biz.Monitor{},
|
||||
&biz.App{},
|
||||
&biz.Setting{},
|
||||
@@ -32,7 +31,6 @@ func init() {
|
||||
&biz.CertDNS{},
|
||||
&biz.CertAccount{},
|
||||
&biz.Cron{},
|
||||
&biz.Database{},
|
||||
&biz.Monitor{},
|
||||
&biz.App{},
|
||||
&biz.Setting{},
|
||||
@@ -56,15 +54,18 @@ func init() {
|
||||
},
|
||||
})
|
||||
Migrations = append(Migrations, &gormigrate.Migration{
|
||||
ID: "20241107-database-item",
|
||||
ID: "20241107-database",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
_ = tx.Migrator().DropTable(&biz.Database{})
|
||||
return tx.AutoMigrate(
|
||||
&biz.DatabaseItem{},
|
||||
&biz.DatabaseServer{},
|
||||
&biz.Database{},
|
||||
)
|
||||
},
|
||||
Rollback: func(tx *gorm.DB) error {
|
||||
return tx.Migrator().DropTable(
|
||||
&biz.DatabaseItem{},
|
||||
&biz.DatabaseServer{},
|
||||
&biz.Database{},
|
||||
)
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user