fix(model): block deletion when model is referenced by KB or agent

Reject model delete with HTTP 400 when the current tenant still has
knowledge bases or custom agents bound to the model ID.
This commit is contained in:
wizardchen
2026-06-23 20:33:01 +08:00
committed by lyingbug
parent 775cc9ef11
commit 7855f36799
11 changed files with 437 additions and 2 deletions
@@ -0,0 +1,49 @@
package repository
import (
"gorm.io/gorm"
)
// scopeKnowledgeBasesByModelID filters knowledge_bases rows that reference
// modelID in any model-binding field.
func scopeKnowledgeBasesByModelID(db *gorm.DB, modelID string) *gorm.DB {
if db.Dialector.Name() == "postgres" {
return db.Where(
"embedding_model_id = ? OR summary_model_id = ? OR "+
"image_processing_config->>'model_id' = ? OR "+
"vlm_config->>'model_id' = ? OR "+
"asr_config->>'model_id' = ? OR "+
"wiki_config->>'synthesis_model_id' = ?",
modelID, modelID, modelID, modelID, modelID, modelID,
)
}
return db.Where(
"embedding_model_id = ? OR summary_model_id = ? OR "+
"json_extract(image_processing_config, '$.model_id') = ? OR "+
"json_extract(vlm_config, '$.model_id') = ? OR "+
"json_extract(asr_config, '$.model_id') = ? OR "+
"json_extract(wiki_config, '$.synthesis_model_id') = ?",
modelID, modelID, modelID, modelID, modelID, modelID,
)
}
// scopeCustomAgentsByModelID filters custom_agents rows whose config JSON
// references modelID in any model-binding field.
func scopeCustomAgentsByModelID(db *gorm.DB, modelID string) *gorm.DB {
if db.Dialector.Name() == "postgres" {
return db.Where(
"config->>'model_id' = ? OR config->>'rerank_model_id' = ? OR "+
"config->>'vlm_model_id' = ? OR config->>'asr_model_id' = ? OR "+
"config->>'query_understand_model_id' = ?",
modelID, modelID, modelID, modelID, modelID,
)
}
return db.Where(
"json_extract(config, '$.model_id') = ? OR "+
"json_extract(config, '$.rerank_model_id') = ? OR "+
"json_extract(config, '$.vlm_model_id') = ? OR "+
"json_extract(config, '$.asr_model_id') = ? OR "+
"json_extract(config, '$.query_understand_model_id') = ?",
modelID, modelID, modelID, modelID, modelID,
)
}