1
votes

I am working on integration test. As we are using NServiceBus Saga with ravendb as store, for integration test I need to delete all documents in raven store for each test case. Since this a NServiceBus saga store, document type in Raven Db is not known in integration test. Basically I need to read all documents (any type) and delete them. I am looking forward to write a query in ravendb session, to retrieve all documents from a particular database. When I checked the Raven Db Query method, it expect Type of the document. Whereas I need to query all the documents in the database without type. FYI as this the integration test, raven store will not have many records. So performance is not an issue.

Here is the query I am trying to execute.

"from doc in docs let DocId = doc[\"@metadata\"][\"@id\"] select new {DocId};"

Solution 1 :

I also tried to create index for the above query and executed DeleteByIndex. This actually deletes all the documents in the database. But it works for certain test cases and throw exception in another test case saying that "Stale Index". I also found a solution that code could wait until index is completed to avoid the stale exception. Or set allowstale to true to avoid this exception. I thought that instead going through all indexing stuff, why can't I just write a query to retrieve all documents and delete them.

Solution 2: I found an another solution that delete the ravendb document folder. This will not work for my situation, because NServices bus will throw exception if database is deleted while NServiceBus windows processor running. That breaks our integration test cases. Another reason that integration test will be running in different machine than ravendb machine. So integration test box may not have access to ravendb box's folder.

These are all the reasons I am looking to write a query to fetch all documents from the database and delete them.

Any help is appreciated.

2

2 Answers

1
votes

I would suggest that you use Raven's ability to run as an in-memory database within your integration tests. This will give you a completely fresh database anytime you want it.

For your tests, instead of a DocumentStore, create an EmbeddableDocumentStore (also implements IDocumentStore) and use the RunInMemory configuration option.

0
votes

Though there are options available to inject the in-memory database, I am going with following route for now. But I would mark the other answer as answered as it make sense to use in-memory database.

This code deletes all documents in raven database. For sake of simplicity ignored documentstore object initialization

 using (var session = documentStore.OpenSession(database))
            {
                var result = session.Advanced.LuceneQuery<dynamic>()
                       .SelectFields<dynamic>("@metadata")
                       .Select<dynamic, string>(x => x["__document_id"])
                       .ToArray();

                var loadedDocuments = session.Load<dynamic>(result);
                foreach (var document in loadedDocuments)
                {
                    session.Delete(document);
                }
                session.SaveChanges();
            }