From b125f1835c8b04d7235c0cc41a972f5d31f487cb Mon Sep 17 00:00:00 2001 From: Hamidreza Ghavami Date: Sat, 6 May 2023 00:22:21 +0430 Subject: [PATCH] add MigrateDB func for a single source of truth --- main.go | 3 +-- web/service/inbound.go | 11 ++++++++++- web/service/server.go | 3 +-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 7fdfeeb8..4d2f05d5 100644 --- a/main.go +++ b/main.go @@ -211,8 +211,7 @@ func migrateDb() { log.Fatal(err) } fmt.Println("Start migrating database...") - inboundService.MigrationRequirements() - inboundService.RemoveOrphanedTraffics() + inboundService.MigrateDB() fmt.Println("Migration done!") } diff --git a/web/service/inbound.go b/web/service/inbound.go index bde6b1f9..119e497e 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -572,6 +572,7 @@ func (s *InboundService) DisableInvalidInbounds() (int64, error) { count := result.RowsAffected return count, err } + func (s *InboundService) DisableInvalidClients() (int64, error) { db := database.GetDB() now := time.Now().Unix() * 1000 @@ -582,7 +583,8 @@ func (s *InboundService) DisableInvalidClients() (int64, error) { count := result.RowsAffected return count, err } -func (s *InboundService) RemoveOrphanedTraffics() { + +func (s *InboundService) MigrationRemoveOrphanedTraffics() { db := database.GetDB() db.Exec(` DELETE FROM client_traffics @@ -593,6 +595,7 @@ func (s *InboundService) RemoveOrphanedTraffics() { ) `) } + func (s *InboundService) AddClientStat(inboundId int, client *model.Client) error { db := database.GetDB() @@ -611,6 +614,7 @@ func (s *InboundService) AddClientStat(inboundId int, client *model.Client) erro } return nil } + func (s *InboundService) UpdateClientStat(email string, client *model.Client) error { db := database.GetDB() @@ -917,3 +921,8 @@ func (s *InboundService) MigrationRequirements() { // Remove orphaned traffics db.Where("inbound_id = 0").Delete(xray.ClientTraffic{}) } + +func (s *InboundService) MigrateDB() { + s.MigrationRequirements() + s.MigrationRemoveOrphanedTraffics() +} diff --git a/web/service/server.go b/web/service/server.go index b4bce974..23b6da4e 100644 --- a/web/service/server.go +++ b/web/service/server.go @@ -467,8 +467,7 @@ func (s *ServerService) ImportDB(file multipart.File) error { defer os.Rename(fallbackPath, config.GetDBPath()) return common.NewErrorf("Error migrating db: %v", err) } - s.inboundService.MigrationRequirements() - s.inboundService.RemoveOrphanedTraffics() + s.inboundService.MigrateDB() // remove fallback file defer os.Remove(fallbackPath)