浏览代码

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 7 小时之前
父节点
当前提交
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 {