MCPcopy Create free account
hub / github.com/53AI/53AIHub / DeleteLibrary

Function DeleteLibrary

api/model/library.go:356–417  ·  view source on GitHub ↗

DeleteLibrary 删除知识库

(eid int64, id int64)

Source from the content-addressed store, hash-verified

354
355// DeleteLibrary 删除知识库
356func DeleteLibrary(eid int64, id int64) error {
357 // 检查知识库是否存在
358 library, err := GetLibraryByID(eid, id)
359 if err != nil {
360 return err
361 }
362
363 // 检查知识库下是否有文件
364 files, err := GetFilesByLibraryID(eid, library.ID)
365 if err != nil {
366 return err
367 }
368 if len(files) > 0 {
369 // return fmt.Errorf("cannot delete library with files")
370 for _, file := range files {
371 if err := DeleteFile(eid, file.ID); err != nil {
372 // 忽略"record not found"错误,因为文件可能已被其他进程删除
373 if !errors.Is(err, gorm.ErrRecordNotFound) {
374 return err
375 }
376 }
377 }
378 }
379
380 // 开启事务
381 tx := DB.Begin()
382 defer func() {
383 if r := recover(); r != nil {
384 tx.Rollback()
385 }
386 }()
387
388 var entityIDs []int64
389 if err := tx.Model(&EntityChunkRelation{}).
390 Distinct("entity_id").
391 Where("eid = ? AND library_id = ?", eid, id).
392 Pluck("entity_id", &entityIDs).Error; err != nil {
393 tx.Rollback()
394 return err
395 }
396 if err := tx.Where("eid = ? AND library_id = ?", eid, id).Delete(&EntityChunkRelation{}).Error; err != nil {
397 tx.Rollback()
398 return err
399 }
400
401 // 删除知识库
402 if err := tx.Where("eid = ? AND id = ?", eid, id).Delete(&Library{}).Error; err != nil {
403 tx.Rollback()
404 return err
405 }
406
407 if err := DeleteOrphanEntitiesByIDsWithDB(tx, eid, entityIDs); err != nil {
408 tx.Rollback()
409 return err
410 }
411
412 if err := tx.Commit().Error; err != nil {
413 return err

Callers

nothing calls this directly

Calls 6

GetLibraryByIDFunction · 0.85
GetFilesByLibraryIDFunction · 0.85
DeleteFileFunction · 0.70
invalidateLibraryCacheFunction · 0.70
DeleteMethod · 0.65

Tested by

no test coverage detected