@@ -14,6 +14,7 @@ import (
1414 "github.com/chroma-core/chroma/go/pkg/sysdb/metastore/db/dbcore"
1515 "github.com/chroma-core/chroma/go/pkg/sysdb/metastore/db/dbmodel"
1616 s3metastore "github.com/chroma-core/chroma/go/pkg/sysdb/metastore/s3"
17+ "github.com/chroma-core/chroma/go/pkg/types"
1718 "github.com/google/uuid"
1819 "github.com/pingcap/log"
1920 "github.com/stretchr/testify/suite"
@@ -116,7 +117,7 @@ func (suite *TenantDatabaseServiceTestSuite) TestServer_DeleteDatabase() {
116117 tenantName := "TestDeleteDatabase"
117118 databaseName := "TestDeleteDatabase"
118119 // Generate random uuid for db id
119- databaseeId := uuid .New ().String ()
120+ databaseId := uuid .New ().String ()
120121
121122 _ , err := suite .catalog .CreateTenant (context .Background (), & model.CreateTenant {
122123 Name : tenantName ,
@@ -127,29 +128,61 @@ func (suite *TenantDatabaseServiceTestSuite) TestServer_DeleteDatabase() {
127128 _ , err = suite .catalog .CreateDatabase (context .Background (), & model.CreateDatabase {
128129 Tenant : tenantName ,
129130 Name : databaseName ,
130- ID : databaseeId ,
131+ ID : databaseId ,
131132 Ts : time .Now ().Unix (),
132133 }, time .Now ().Unix ())
133134 suite .NoError (err )
134135
136+ collectionID := types .NewUniqueID ()
135137 _ , _ , err = suite .catalog .CreateCollection (context .Background (), & model.CreateCollection {
138+ ID : collectionID ,
136139 TenantID : tenantName ,
137140 DatabaseName : databaseName ,
138141 Name : "TestCollection" ,
139142 }, time .Now ().Unix ())
140143 suite .NoError (err )
141144
145+ timeBeforeSoftDelete := time .Now ()
146+
142147 err = suite .catalog .DeleteDatabase (context .Background (), & model.DeleteDatabase {
143148 Tenant : tenantName ,
144149 Name : databaseName ,
145150 })
146151 suite .NoError (err )
147152
148- // Check that associated collection was deleted
149- var count int64
153+ // Check that associated collection was soft deleted
150154 var collections []* dbmodel.Collection
151- suite .NoError (suite .db .Find (& collections ).Count (& count ).Error )
152- suite .Equal (int64 (0 ), count )
155+ suite .NoError (suite .db .Find (& collections ).Error )
156+ suite .Equal (1 , len (collections ))
157+ suite .Equal (true , collections [0 ].IsDeleted )
158+
159+ // Database should not be eligible for hard deletion yet because it still has a (soft deleted) collection
160+ numDeleted , err := suite .catalog .FinishDatabaseDeletion (context .Background (), time .Now ())
161+ suite .NoError (err )
162+ suite .Equal (uint64 (0 ), numDeleted )
163+
164+ // Hard delete associated collection
165+ suite .NoError (err )
166+ suite .NoError (suite .catalog .DeleteCollection (context .Background (), & model.DeleteCollection {
167+ TenantID : tenantName ,
168+ DatabaseName : databaseName ,
169+ ID : collectionID ,
170+ }, false ))
171+
172+ // Database should now be eligible for hard deletion, but first verify that database is not deleted if cutoff time is prior to soft delete
173+ numDeleted , err = suite .catalog .FinishDatabaseDeletion (context .Background (), timeBeforeSoftDelete )
174+ suite .NoError (err )
175+ suite .Equal (uint64 (0 ), numDeleted )
176+
177+ // Hard delete database
178+ numDeleted , err = suite .catalog .FinishDatabaseDeletion (context .Background (), time .Now ())
179+ suite .NoError (err )
180+ suite .Equal (uint64 (1 ), numDeleted )
181+
182+ // Verify that database is hard deleted
183+ var databases []* dbmodel.Database
184+ suite .NoError (suite .db .Debug ().Where ("id = ?" , databaseId ).Find (& databases ).Error )
185+ suite .Equal (0 , len (databases ))
153186}
154187
155188func TestTenantDatabaseServiceTestSuite (t * testing.T ) {
0 commit comments