소스 검색

fix(inbounds): reset id sequence on delete so old ids are reused

SQLite AUTOINCREMENT keeps a high-water mark in sqlite_sequence that
deleting rows never lowers, so after removing inbounds the next add kept
climbing instead of reusing freed ids. DelInbound now realigns the
counter to MAX(id) after each delete, clearing the sqlite_sequence row
entirely when the table is empty so the next inbound starts at id 1.
Guarded behind !IsPostgres(); Postgres sequences are left untouched.
MHSanaei 2 달 전
부모
커밋
80110f9404
1개의 변경된 파일17개의 추가작업 그리고 1개의 파일을 삭제
  1. 17 1
      web/service/inbound.go

+ 17 - 1
web/service/inbound.go

@@ -614,7 +614,23 @@ func (s *InboundService) DelInbound(id int) (bool, error) {
 		return false, err
 	}
 
-	return needRestart, db.Delete(model.Inbound{}, id).Error
+	if err := db.Delete(model.Inbound{}, id).Error; err != nil {
+		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 {
+			return needRestart, err
+		}
+		if maxId == 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
 }
 
 type BulkDelInboundResult struct {