Question

I have this code it works well EXCEPT I want to ensure that the expiration bundle is in the database and I dont want to create this database manually. What is the correct way to initialize a database with the expiration bundle enabled?

        _documentStore = new DocumentStore()
            {
                Url = SettingsManager.RavenDbUrl,
                DefaultDatabase = SettingsManager.RavenDbDatabaseName
            };
        _documentStore.Initialize();
Was it helpful?

Solution

Well I couldnt find docs and I couldnt find help so I cobbled this together:

static class RavenDbExtensions
{
    /// <summary>
    /// Ensure a bundle is activated
    /// </summary>
    /// <param name="documentStore"></param>
    /// <param name="bundleName"></param>
    /// <param name="databaseName"></param>
    public static void ActivateBundle(this IDocumentStore documentStore, string bundleName, string databaseName)
    {
        using (var session = documentStore.OpenSession())
        {
            var databaseDocument = session.Load<DatabaseDocument>("Raven/Databases/" + databaseName);
            var settings = databaseDocument.Settings;
            var activeBundles = settings.ContainsKey(Constants.ActiveBundles) ? settings[Constants.ActiveBundles] : null;
            if (string.IsNullOrEmpty(activeBundles))
                settings[Constants.ActiveBundles] = bundleName;
            else if (!activeBundles.Split(new char[]{';'}).Contains(bundleName, StringComparer.OrdinalIgnoreCase))
                settings[Constants.ActiveBundles] = activeBundles + ";" + bundleName;
            session.SaveChanges();
        }
    }
}

Then I do this little dance when initializing the document store. It seems to work well. It isnt clear whether the bundle is named Expiration or DocumentExpiration so I try both it doesnt crash and it seems to fold in the expiration functionality I need.

        _documentStore = new DocumentStore()
            {
                Url = SettingsManager.RavenDbUrl
            };
        _documentStore.Initialize();
        _documentStore.ActivateBundle("Expiration", Assembly.GetExecutingAssembly().GetName().Name);
        _documentStore.ActivateBundle("DocumentExpiration", Assembly.GetExecutingAssembly().GetName().Name);
        _documentStore.DefaultDatabase = Assembly.GetExecutingAssembly().GetName().Name;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top