Skip to content

Commit 5861af3

Browse files
authored
[BUG] Fix property failure for soft delete. (#3220)
[BUG] Fix property failure for soft delete. Renaming soft deleted collection at delete time since modify and create can try to re-create a collection with the same name. ## Description of changes *Summarize the changes made by this PR.* - Improvements & Bug fixes - Fix property failure for soft delete. ## Test plan *How are these changes tested?* - [ ] Tests pass locally with `pytest` for python, `yarn test` for js, `cargo test` for rust ## Documentation Changes *Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the [docs repository](https://github.com/chroma-core/docs)?* n/a
1 parent 0df312a commit 5861af3

File tree

1 file changed

+23
-34
lines changed

1 file changed

+23
-34
lines changed

go/pkg/sysdb/coordinator/table_catalog.go

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package coordinator
33
import (
44
"context"
55
"fmt"
6+
"math/rand"
67
"time"
78

89
"github.com/chroma-core/chroma/go/pkg/common"
@@ -244,20 +245,6 @@ func (tc *Catalog) createCollectionImpl(txCtx context.Context, createCollection
244245
} else {
245246
return nil, false, common.ErrCollectionUniqueConstraintViolation
246247
}
247-
} else {
248-
// If collection is soft-deleted, then new collection will throw an error since name should be unique, so we need to rename it.
249-
isSoftDeleted, sdCollectionID, err := tc.metaDomain.CollectionDb(txCtx).CheckCollectionIsSoftDeleted(createCollection.Name, tenantID, databaseName)
250-
if err != nil {
251-
return nil, false, fmt.Errorf("failed to create collection: %w", err)
252-
}
253-
if isSoftDeleted {
254-
log.Info("new collection create request with same name as collection that was soft deleted", zap.Any("collection", createCollection))
255-
// Rename the soft deleted collection to a new name with timestamp
256-
err = tc.renameSoftDeletedCollection(txCtx, sdCollectionID, createCollection.Name, tenantID, databaseName)
257-
if err != nil {
258-
return nil, false, fmt.Errorf("failed to create collection: %w", err)
259-
}
260-
}
261248
}
262249

263250
dbCollection := &dbmodel.Collection{
@@ -389,25 +376,6 @@ func (tc *Catalog) hardDeleteCollection(ctx context.Context, deleteCollection *m
389376
})
390377
}
391378

392-
func (tc *Catalog) renameSoftDeletedCollection(ctx context.Context, collectionID string, collectionName string, tenantID string, databaseName string) error {
393-
log.Info("Renaming soft deleted collection", zap.String("collectionID", collectionID), zap.String("collectionName", collectionName), zap.Any("tenantID", tenantID), zap.String("databaseName", databaseName))
394-
// Generate new name with timestamp
395-
newName := fmt.Sprintf("deleted_%s_%d", collectionName, time.Now().Unix())
396-
397-
dbCollection := &dbmodel.Collection{
398-
ID: collectionID,
399-
Name: &newName,
400-
IsDeleted: true,
401-
} // Not updating the timestamp or updated_at.
402-
403-
err := tc.metaDomain.CollectionDb(ctx).Update(dbCollection)
404-
if err != nil {
405-
log.Error("rename soft deleted collection failed", zap.Error(err))
406-
return fmt.Errorf("collection rename failed due to update error: %w", err)
407-
}
408-
return nil
409-
}
410-
411379
func (tc *Catalog) softDeleteCollection(ctx context.Context, deleteCollection *model.DeleteCollection) error {
412380
log.Info("Soft deleting collection", zap.Any("softDeleteCollection", deleteCollection))
413381
return tc.txImpl.Transaction(ctx, func(txCtx context.Context) error {
@@ -420,8 +388,13 @@ func (tc *Catalog) softDeleteCollection(ctx context.Context, deleteCollection *m
420388
return common.ErrCollectionDeleteNonExistingCollection
421389
}
422390

391+
// Generate new name with timestamp and random number
392+
oldName := *collections[0].Collection.Name
393+
newName := fmt.Sprintf("_deleted_%s_%d_%d", oldName, time.Now().Unix(), rand.Intn(1000))
394+
423395
dbCollection := &dbmodel.Collection{
424396
ID: deleteCollection.ID.String(),
397+
Name: &newName,
425398
IsDeleted: true,
426399
Ts: deleteCollection.Ts,
427400
UpdatedAt: time.Now(),
@@ -461,13 +434,29 @@ func (tc *Catalog) UpdateCollection(ctx context.Context, updateCollection *model
461434
var result *model.Collection
462435

463436
err := tc.txImpl.Transaction(ctx, func(txCtx context.Context) error {
437+
// Check if collection exists
438+
collections, err := tc.metaDomain.CollectionDb(txCtx).GetCollections(
439+
types.FromUniqueID(updateCollection.ID),
440+
nil,
441+
updateCollection.TenantID,
442+
updateCollection.DatabaseName,
443+
nil,
444+
nil,
445+
)
446+
if err != nil {
447+
return err
448+
}
449+
if len(collections) == 0 {
450+
return common.ErrCollectionNotFound
451+
}
452+
464453
dbCollection := &dbmodel.Collection{
465454
ID: updateCollection.ID.String(),
466455
Name: updateCollection.Name,
467456
Dimension: updateCollection.Dimension,
468457
Ts: ts,
469458
}
470-
err := tc.metaDomain.CollectionDb(txCtx).Update(dbCollection)
459+
err = tc.metaDomain.CollectionDb(txCtx).Update(dbCollection)
471460
if err != nil {
472461
return err
473462
}

0 commit comments

Comments
 (0)