mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-03-01 01:20:49 +03:00
Improved database model migration and added indexing (#2655)
This commit is contained in:
parent
8a7cffd63f
commit
b922d986d6
@ -26,20 +26,35 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func initModels() error {
|
func initModels() error {
|
||||||
models := []interface{}{
|
// Order matters: first create tables without dependencies
|
||||||
|
baseModels := []interface{}{
|
||||||
&model.User{},
|
&model.User{},
|
||||||
&model.Inbound{},
|
|
||||||
&model.OutboundTraffics{},
|
|
||||||
&model.Setting{},
|
&model.Setting{},
|
||||||
&model.InboundClientIps{},
|
|
||||||
&xray.ClientTraffic{},
|
|
||||||
}
|
}
|
||||||
for _, model := range models {
|
|
||||||
|
// Migrate base models
|
||||||
|
for _, model := range baseModels {
|
||||||
if err := db.AutoMigrate(model); err != nil {
|
if err := db.AutoMigrate(model); err != nil {
|
||||||
log.Printf("Error auto migrating model: %v", err)
|
log.Printf("Error auto migrating base model: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Then migrate models with dependencies
|
||||||
|
dependentModels := []interface{}{
|
||||||
|
&model.Inbound{},
|
||||||
|
&model.OutboundTraffics{},
|
||||||
|
&model.InboundClientIps{},
|
||||||
|
&xray.ClientTraffic{},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, model := range dependentModels {
|
||||||
|
if err := db.AutoMigrate(model); err != nil {
|
||||||
|
log.Printf("Error auto migrating dependent model: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,14 +29,14 @@ type User struct {
|
|||||||
|
|
||||||
type Inbound struct {
|
type Inbound struct {
|
||||||
Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
|
Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
|
||||||
UserId int `json:"-"`
|
UserId int `json:"-" gorm:"index"`
|
||||||
Up int64 `json:"up" form:"up"`
|
Up int64 `json:"up" form:"up"`
|
||||||
Down int64 `json:"down" form:"down"`
|
Down int64 `json:"down" form:"down"`
|
||||||
Total int64 `json:"total" form:"total"`
|
Total int64 `json:"total" form:"total"`
|
||||||
Remark string `json:"remark" form:"remark"`
|
Remark string `json:"remark" form:"remark"`
|
||||||
Enable bool `json:"enable" form:"enable"`
|
Enable bool `json:"enable" form:"enable"`
|
||||||
ExpiryTime int64 `json:"expiryTime" form:"expiryTime"`
|
ExpiryTime int64 `json:"expiryTime" form:"expiryTime"`
|
||||||
ClientStats []xray.ClientTraffic `gorm:"foreignKey:InboundId;references:Id" json:"clientStats" form:"clientStats"`
|
ClientStats []xray.ClientTraffic `gorm:"foreignKey:InboundId;references:Id;constraint:OnDelete:CASCADE" json:"clientStats"`
|
||||||
|
|
||||||
// config part
|
// config part
|
||||||
Listen string `json:"listen" form:"listen"`
|
Listen string `json:"listen" form:"listen"`
|
||||||
|
@ -283,4 +283,4 @@ install_x-ui() {
|
|||||||
|
|
||||||
echo -e "${green}Running...${plain}"
|
echo -e "${green}Running...${plain}"
|
||||||
install_base
|
install_base
|
||||||
install_x-ui $1
|
install_x-ui $1
|
2
x-ui.sh
2
x-ui.sh
@ -1912,4 +1912,4 @@ if [[ $# > 0 ]]; then
|
|||||||
esac
|
esac
|
||||||
else
|
else
|
||||||
show_menu
|
show_menu
|
||||||
fi
|
fi
|
@ -2,9 +2,9 @@ package xray
|
|||||||
|
|
||||||
type ClientTraffic struct {
|
type ClientTraffic struct {
|
||||||
Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
|
Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
|
||||||
InboundId int `json:"inboundId" form:"inboundId"`
|
InboundId int `json:"inboundId" form:"inboundId" gorm:"index;not null"`
|
||||||
Enable bool `json:"enable" form:"enable"`
|
Enable bool `json:"enable" form:"enable"`
|
||||||
Email string `json:"email" form:"email" gorm:"unique"`
|
Email string `json:"email" form:"email" gorm:"uniqueIndex"`
|
||||||
Up int64 `json:"up" form:"up"`
|
Up int64 `json:"up" form:"up"`
|
||||||
Down int64 `json:"down" form:"down"`
|
Down int64 `json:"down" form:"down"`
|
||||||
ExpiryTime int64 `json:"expiryTime" form:"expiryTime"`
|
ExpiryTime int64 `json:"expiryTime" form:"expiryTime"`
|
||||||
|
Loading…
Reference in New Issue
Block a user