Skip to content

Commit b922d98

Browse files
authored
Improved database model migration and added indexing (#2655)
1 parent 8a7cffd commit b922d98

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

database/db.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,35 @@ const (
2626
)
2727

2828
func initModels() error {
29-
models := []interface{}{
29+
// Order matters: first create tables without dependencies
30+
baseModels := []interface{}{
3031
&model.User{},
32+
&model.Setting{},
33+
}
34+
35+
// Migrate base models
36+
for _, model := range baseModels {
37+
if err := db.AutoMigrate(model); err != nil {
38+
log.Printf("Error auto migrating base model: %v", err)
39+
return err
40+
}
41+
}
42+
43+
// Then migrate models with dependencies
44+
dependentModels := []interface{}{
3145
&model.Inbound{},
3246
&model.OutboundTraffics{},
33-
&model.Setting{},
3447
&model.InboundClientIps{},
3548
&xray.ClientTraffic{},
3649
}
37-
for _, model := range models {
50+
51+
for _, model := range dependentModels {
3852
if err := db.AutoMigrate(model); err != nil {
39-
log.Printf("Error auto migrating model: %v", err)
53+
log.Printf("Error auto migrating dependent model: %v", err)
4054
return err
4155
}
4256
}
57+
4358
return nil
4459
}
4560

database/model/model.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ type User struct {
2929

3030
type Inbound struct {
3131
Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
32-
UserId int `json:"-"`
32+
UserId int `json:"-" gorm:"index"`
3333
Up int64 `json:"up" form:"up"`
3434
Down int64 `json:"down" form:"down"`
3535
Total int64 `json:"total" form:"total"`
3636
Remark string `json:"remark" form:"remark"`
3737
Enable bool `json:"enable" form:"enable"`
3838
ExpiryTime int64 `json:"expiryTime" form:"expiryTime"`
39-
ClientStats []xray.ClientTraffic `gorm:"foreignKey:InboundId;references:Id" json:"clientStats" form:"clientStats"`
39+
ClientStats []xray.ClientTraffic `gorm:"foreignKey:InboundId;references:Id;constraint:OnDelete:CASCADE" json:"clientStats"`
4040

4141
// config part
4242
Listen string `json:"listen" form:"listen"`

install.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,4 @@ install_x-ui() {
283283

284284
echo -e "${green}Running...${plain}"
285285
install_base
286-
install_x-ui $1
286+
install_x-ui $1

x-ui.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1912,4 +1912,4 @@ if [[ $# > 0 ]]; then
19121912
esac
19131913
else
19141914
show_menu
1915-
fi
1915+
fi

xray/client_traffic.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package xray
22

33
type ClientTraffic struct {
44
Id int `json:"id" form:"id" gorm:"primaryKey;autoIncrement"`
5-
InboundId int `json:"inboundId" form:"inboundId"`
5+
InboundId int `json:"inboundId" form:"inboundId" gorm:"index;not null"`
66
Enable bool `json:"enable" form:"enable"`
7-
Email string `json:"email" form:"email" gorm:"unique"`
7+
Email string `json:"email" form:"email" gorm:"uniqueIndex"`
88
Up int64 `json:"up" form:"up"`
99
Down int64 `json:"down" form:"down"`
1010
ExpiryTime int64 `json:"expiryTime" form:"expiryTime"`

0 commit comments

Comments
 (0)