Explorar el Código

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 hace 7 horas
padre
commit
80110f9404
Se han modificado 1 ficheros con 17 adiciones y 1 borrados
  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 {