Cleaning up database
For the purpose of automated testing where you need to carefully control the state of the database, Marten supplies few helper functions.
Tearing Down Document Storage
Marten supplies the IDocumentCleaner
service to quickly remove persisted document state or even to completely tear down the entire document storage.
This service is exposed as the IDocumentStore.Advanced.Clean
property. You can see the usages of the document cleaner below:
public async Task clean_out_documents(IDocumentStore store)
{
// Completely remove all the database schema objects related
// to the User document type
await store.Advanced.Clean.CompletelyRemoveAsync(typeof(User));
// Tear down and remove all Marten related database schema objects
await store.Advanced.Clean.CompletelyRemoveAllAsync();
// Deletes all the documents stored in a Marten database
await store.Advanced.Clean.DeleteAllDocumentsAsync();
// Deletes all the event data stored in a Marten database
await store.Advanced.Clean.DeleteAllEventDataAsync();
// Deletes all of the persisted User documents
await store.Advanced.Clean.DeleteDocumentsByTypeAsync(typeof(User));
// For cases where you may want to keep some document types,
// but eliminate everything else. This is here specifically to support
// automated testing scenarios where you have some static data that can
// be safely reused across tests
await store.Advanced.Clean.DeleteDocumentsExceptAsync(typeof(Company), typeof(User));
}
You can also tear down all Data from the IHost
instance using the IHost.CleanAllMartenDataAsync()
method.
public async Task clean_out_documents(IHost host)
{
// Clean off all Marten data in the default DocumentStore for this host
await host.CleanAllMartenDataAsync();
}
If you're working with multiple Marten databases, you can use IHost.CleanAllMartenDataAsync<TStore>()
to clean out all data in a specific database:
public async Task clean_out_database_documents(IHost host)
{
// Clean off all Marten data in the IInvoicing DocumentStore for this host
await host.CleanAllMartenDataAsync<IInvoicingStore>();
}
Reset all data
Use IDocumentStore.Advanced.ResetAllData()
to deletes all current document, event data and then (re)applies the configured initial data.
theStore.Advanced.InitialDataCollection.Add(new Users());
await theStore.Advanced.ResetAllData();
Use IHost.ResetAllMartenDataAsync()
to delete all current document, event data and then (re)applies the configured initial data from the IHost
instance.
using var host = await Host.CreateDefaultBuilder()
.ConfigureServices(
services =>
{
services.AddMarten(
opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.Logger(new TestOutputMartenLogger(_output));
}
)
.InitializeWith(new Users());
}
)
.StartAsync();
await host.ResetAllMartenDataAsync();
If you're working with multiple Marten databases, you can use IHost.ResetAllMartenDataAsync<TStore>()
to reset all data in a specific database:
using var host = await Host.CreateDefaultBuilder()
.ConfigureServices(
services =>
{
services.AddMartenStore<IInvoicingStore>(
opts =>
{
opts.Connection(ConnectionSource.ConnectionString);
opts.Logger(new TestOutputMartenLogger(_output));
}
)
.InitializeWith(new Users());
}
)
.StartAsync();
await host.ResetAllMartenDataAsync<IInvoicingStore>();