From 575355e4f1750e552c6a7b9427733e580ad43284 Mon Sep 17 00:00:00 2001 From: MHSanaei Date: Sun, 31 May 2026 23:01:25 +0200 Subject: [PATCH] fix(inbounds): only reset id sequence when all inbounds are deleted Commit 80110f9 realigned sqlite_sequence to MAX(id) after every delete, which recycled freed ids and let a newly added inbound take an old inbound id. Now the sequence row is cleared only when the table is empty, so the counter keeps climbing while any inbound remains and existing ids are never reused. Still guarded behind !IsPostgres(). @ --- web/service/inbound.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/web/service/inbound.go b/web/service/inbound.go index f75868cb..0916f6d0 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -130,7 +130,7 @@ func (s *InboundService) enrichClientStats(db *gorm.DB, inbounds []*model.Inboun func (s *InboundService) GetInbounds(userId int) ([]*model.Inbound, error) { db := database.GetDB() var inbounds []*model.Inbound - err := db.Model(model.Inbound{}).Preload("ClientStats").Where("user_id = ?", userId).Find(&inbounds).Error + err := db.Model(model.Inbound{}).Preload("ClientStats").Where("user_id = ?", userId).Order("id ASC").Find(&inbounds).Error if err != nil && err != gorm.ErrRecordNotFound { return nil, err } @@ -152,7 +152,7 @@ func (s *InboundService) GetInbounds(userId int) ([]*model.Inbound, error) { func (s *InboundService) GetInboundsSlim(userId int) ([]*model.Inbound, error) { db := database.GetDB() var inbounds []*model.Inbound - err := db.Model(model.Inbound{}).Preload("ClientStats").Where("user_id = ?", userId).Find(&inbounds).Error + err := db.Model(model.Inbound{}).Preload("ClientStats").Where("user_id = ?", userId).Order("id ASC").Find(&inbounds).Error if err != nil && err != gorm.ErrRecordNotFound { return nil, err } @@ -618,16 +618,14 @@ func (s *InboundService) DelInbound(id int) (bool, error) { return needRestart, err } if !database.IsPostgres() { - var maxId int - if err := db.Model(&model.Inbound{}).Select("COALESCE(MAX(id), 0)").Scan(&maxId).Error; err != nil { + var count int64 + if err := db.Model(&model.Inbound{}).Count(&count).Error; err != nil { return needRestart, err } - if maxId == 0 { + if count == 0 { if err := db.Exec("DELETE FROM sqlite_sequence WHERE name = ?", "inbounds").Error; err != nil { return needRestart, err } - } else if err := db.Exec("UPDATE sqlite_sequence SET seq = ? WHERE name = ?", maxId, "inbounds").Error; err != nil { - return needRestart, err } } return needRestart, nil