Skip to content

Commit

Permalink
Improved database model migration and added indexing (#2655)
Browse files Browse the repository at this point in the history
  • Loading branch information
izzzzzi authored Feb 3, 2025
1 parent 8a7cffd commit b922d98
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
23 changes: 19 additions & 4 deletions database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,35 @@ const (
)

func initModels() error {
models := []interface{}{
// Order matters: first create tables without dependencies
baseModels := []interface{}{
&model.User{},
&model.Setting{},
}

// Migrate base models
for _, model := range baseModels {
if err := db.AutoMigrate(model); err != nil {
log.Printf("Error auto migrating base model: %v", err)
return err
}
}

// Then migrate models with dependencies
dependentModels := []interface{}{
&model.Inbound{},
&model.OutboundTraffics{},
&model.Setting{},
&model.InboundClientIps{},
&xray.ClientTraffic{},
}
for _, model := range models {

for _, model := range dependentModels {
if err := db.AutoMigrate(model); err != nil {
log.Printf("Error auto migrating model: %v", err)
log.Printf("Error auto migrating dependent model: %v", err)
return err
}
}

return nil
}

Expand Down
4 changes: 2 additions & 2 deletions database/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ type User struct {

type Inbound struct {
Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
UserId int `json:"-"`
UserId int `json:"-" gorm:"index"`
Up int64 `json:"up" form:"up"`
Down int64 `json:"down" form:"down"`
Total int64 `json:"total" form:"total"`
Remark string `json:"remark" form:"remark"`
Enable bool `json:"enable" form:"enable"`
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
Listen string `json:"listen" form:"listen"`
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,4 @@ install_x-ui() {

echo -e "${green}Running...${plain}"
install_base
install_x-ui $1
install_x-ui $1
2 changes: 1 addition & 1 deletion x-ui.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1912,4 +1912,4 @@ if [[ $# > 0 ]]; then
esac
else
show_menu
fi
fi
4 changes: 2 additions & 2 deletions xray/client_traffic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package xray

type ClientTraffic struct {
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"`
Email string `json:"email" form:"email" gorm:"unique"`
Email string `json:"email" form:"email" gorm:"uniqueIndex"`
Up int64 `json:"up" form:"up"`
Down int64 `json:"down" form:"down"`
ExpiryTime int64 `json:"expiryTime" form:"expiryTime"`
Expand Down

0 comments on commit b922d98

Please # to comment.