mirror of
https://github.com/alireza0/x-ui.git
synced 2026-03-14 05:23:09 +00:00
add MigrateDB func for a single source of truth
This commit is contained in:
3
main.go
3
main.go
@@ -211,8 +211,7 @@ func migrateDb() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Println("Start migrating database...")
|
fmt.Println("Start migrating database...")
|
||||||
inboundService.MigrationRequirements()
|
inboundService.MigrateDB()
|
||||||
inboundService.RemoveOrphanedTraffics()
|
|
||||||
fmt.Println("Migration done!")
|
fmt.Println("Migration done!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -572,6 +572,7 @@ func (s *InboundService) DisableInvalidInbounds() (int64, error) {
|
|||||||
count := result.RowsAffected
|
count := result.RowsAffected
|
||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *InboundService) DisableInvalidClients() (int64, error) {
|
func (s *InboundService) DisableInvalidClients() (int64, error) {
|
||||||
db := database.GetDB()
|
db := database.GetDB()
|
||||||
now := time.Now().Unix() * 1000
|
now := time.Now().Unix() * 1000
|
||||||
@@ -582,7 +583,8 @@ func (s *InboundService) DisableInvalidClients() (int64, error) {
|
|||||||
count := result.RowsAffected
|
count := result.RowsAffected
|
||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
func (s *InboundService) RemoveOrphanedTraffics() {
|
|
||||||
|
func (s *InboundService) MigrationRemoveOrphanedTraffics() {
|
||||||
db := database.GetDB()
|
db := database.GetDB()
|
||||||
db.Exec(`
|
db.Exec(`
|
||||||
DELETE FROM client_traffics
|
DELETE FROM client_traffics
|
||||||
@@ -593,6 +595,7 @@ func (s *InboundService) RemoveOrphanedTraffics() {
|
|||||||
)
|
)
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *InboundService) AddClientStat(inboundId int, client *model.Client) error {
|
func (s *InboundService) AddClientStat(inboundId int, client *model.Client) error {
|
||||||
db := database.GetDB()
|
db := database.GetDB()
|
||||||
|
|
||||||
@@ -611,6 +614,7 @@ func (s *InboundService) AddClientStat(inboundId int, client *model.Client) erro
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *InboundService) UpdateClientStat(email string, client *model.Client) error {
|
func (s *InboundService) UpdateClientStat(email string, client *model.Client) error {
|
||||||
db := database.GetDB()
|
db := database.GetDB()
|
||||||
|
|
||||||
@@ -917,3 +921,8 @@ func (s *InboundService) MigrationRequirements() {
|
|||||||
// Remove orphaned traffics
|
// Remove orphaned traffics
|
||||||
db.Where("inbound_id = 0").Delete(xray.ClientTraffic{})
|
db.Where("inbound_id = 0").Delete(xray.ClientTraffic{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *InboundService) MigrateDB() {
|
||||||
|
s.MigrationRequirements()
|
||||||
|
s.MigrationRemoveOrphanedTraffics()
|
||||||
|
}
|
||||||
|
|||||||
@@ -467,8 +467,7 @@ func (s *ServerService) ImportDB(file multipart.File) error {
|
|||||||
defer os.Rename(fallbackPath, config.GetDBPath())
|
defer os.Rename(fallbackPath, config.GetDBPath())
|
||||||
return common.NewErrorf("Error migrating db: %v", err)
|
return common.NewErrorf("Error migrating db: %v", err)
|
||||||
}
|
}
|
||||||
s.inboundService.MigrationRequirements()
|
s.inboundService.MigrateDB()
|
||||||
s.inboundService.RemoveOrphanedTraffics()
|
|
||||||
|
|
||||||
// remove fallback file
|
// remove fallback file
|
||||||
defer os.Remove(fallbackPath)
|
defer os.Remove(fallbackPath)
|
||||||
|
|||||||
Reference in New Issue
Block a user