mirror of
https://github.com/pikami/cosmium.git
synced 2025-10-13 15:26:00 +01:00
Fix database and collection deletion
This commit is contained in:
parent
4d67212f1b
commit
11f3a1ad01
@ -121,5 +121,26 @@ func Test_Collections(t *testing.T) {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Should delete collection with exactly matching name", func(t *testing.T) {
|
||||
ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{
|
||||
ID: testCollectionName + "extra",
|
||||
})
|
||||
ts.DataStore.CreateCollection(testDatabaseName, datastore.Collection{
|
||||
ID: testCollectionName,
|
||||
})
|
||||
|
||||
collectionResponse, err := databaseClient.NewContainer(testCollectionName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
readResponse, err := collectionResponse.Delete(context.TODO(), &azcosmos.DeleteContainerOptions{})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, readResponse.RawResponse.StatusCode, http.StatusNoContent)
|
||||
|
||||
collections, status := ts.DataStore.GetAllCollections(testDatabaseName)
|
||||
assert.Equal(t, status, datastore.StatusOk)
|
||||
assert.Len(t, collections, 1)
|
||||
assert.Equal(t, collections[0].ID, testCollectionName+"extra")
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -109,5 +109,26 @@ func Test_Databases(t *testing.T) {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Should delete database with exactly matching name", func(t *testing.T) {
|
||||
ts.DataStore.CreateDatabase(datastore.Database{
|
||||
ID: testDatabaseName + "extra",
|
||||
})
|
||||
ts.DataStore.CreateDatabase(datastore.Database{
|
||||
ID: testDatabaseName,
|
||||
})
|
||||
|
||||
databaseResponse, err := client.NewDatabase(testDatabaseName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
readResponse, err := databaseResponse.Delete(context.TODO(), &azcosmos.DeleteDatabaseOptions{})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, readResponse.RawResponse.StatusCode, http.StatusNoContent)
|
||||
|
||||
dbs, status := ts.DataStore.GetAllDatabases()
|
||||
assert.Equal(t, status, datastore.StatusOk)
|
||||
assert.Len(t, dbs, 1)
|
||||
assert.Equal(t, dbs[0].ID, testDatabaseName+"extra")
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -425,7 +425,7 @@ func Test_Documents(t *testing.T) {
|
||||
assert.Equal(t, int32(http.StatusNoContent), operationResponse.StatusCode)
|
||||
|
||||
_, status := ts.DataStore.GetDocument(testDatabaseName, testCollectionName, "12345")
|
||||
assert.Equal(t, datastore.StatusNotFound, int(status))
|
||||
assert.Equal(t, datastore.StatusNotFound, status)
|
||||
})
|
||||
|
||||
t.Run("Should execute REPLACE transactional batch", func(t *testing.T) {
|
||||
|
@ -54,7 +54,6 @@ func (r *BadgerDataStore) DeleteCollection(databaseId string, collectionId strin
|
||||
generateKey(resourceid.ResourceTypeTrigger, databaseId, collectionId, "") + "/",
|
||||
generateKey(resourceid.ResourceTypeStoredProcedure, databaseId, collectionId, "") + "/",
|
||||
generateKey(resourceid.ResourceTypeUserDefinedFunction, databaseId, collectionId, "") + "/",
|
||||
collectionKey,
|
||||
}
|
||||
for _, prefix := range prefixes {
|
||||
if err := deleteKeysByPrefix(txn, prefix); err != nil {
|
||||
@ -62,6 +61,8 @@ func (r *BadgerDataStore) DeleteCollection(databaseId string, collectionId strin
|
||||
}
|
||||
}
|
||||
|
||||
deleteKey(txn, collectionKey)
|
||||
|
||||
err := txn.Commit()
|
||||
if err != nil {
|
||||
logger.ErrorLn("Error while committing transaction:", err)
|
||||
|
@ -43,7 +43,6 @@ func (r *BadgerDataStore) DeleteDatabase(id string) datastore.DataStoreStatus {
|
||||
generateKey(resourceid.ResourceTypeTrigger, id, "", "") + "/",
|
||||
generateKey(resourceid.ResourceTypeStoredProcedure, id, "", "") + "/",
|
||||
generateKey(resourceid.ResourceTypeUserDefinedFunction, id, "", "") + "/",
|
||||
databaseKey,
|
||||
}
|
||||
for _, prefix := range prefixes {
|
||||
if err := deleteKeysByPrefix(txn, prefix); err != nil {
|
||||
@ -51,6 +50,8 @@ func (r *BadgerDataStore) DeleteDatabase(id string) datastore.DataStoreStatus {
|
||||
}
|
||||
}
|
||||
|
||||
deleteKey(txn, databaseKey)
|
||||
|
||||
err := txn.Commit()
|
||||
if err != nil {
|
||||
logger.ErrorLn("Error while committing transaction:", err)
|
||||
|
@ -202,3 +202,22 @@ func deleteKeysByPrefix(txn *badger.Txn, prefix string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func deleteKey(txn *badger.Txn, key string) error {
|
||||
_, err := txn.Get([]byte(key))
|
||||
if err == badger.ErrKeyNotFound {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
logger.ErrorLn("Error while checking if key exists:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = txn.Delete([]byte(key))
|
||||
if err != nil {
|
||||
logger.ErrorLn("Error while deleting key:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -11,12 +11,12 @@ type Database struct {
|
||||
type DataStoreStatus int
|
||||
|
||||
const (
|
||||
StatusOk = 1
|
||||
StatusNotFound = 2
|
||||
Conflict = 3
|
||||
BadRequest = 4
|
||||
IterEOF = 5
|
||||
Unknown = 6
|
||||
StatusOk DataStoreStatus = 1
|
||||
StatusNotFound DataStoreStatus = 2
|
||||
Conflict DataStoreStatus = 3
|
||||
BadRequest DataStoreStatus = 4
|
||||
IterEOF DataStoreStatus = 5
|
||||
Unknown DataStoreStatus = 6
|
||||
)
|
||||
|
||||
type TriggerOperation string
|
||||
|
Loading…
x
Reference in New Issue
Block a user