Configure RavenDB versioning via code

后端 未结 1 1609
猫巷女王i
猫巷女王i 2021-01-14 23:04

Is it possible to configure versioning on a new RavenDB database via code?

I\'m using the following code (based on http://ravendb.net/docs/2.0/server/extending/bundl

相关标签:
1条回答
  • 2021-01-14 23:17

    That code is good, but it just creates the configuration information that the Versioning Bundle will look for. It doesn't actually enable the bundle.

    For any named bundle, you enable it by including the name in the Raven/ActiveBundles setting, which is a semicolon-delimited list of bundle names.

    By "named" bundles, I mean those that export a "Bundle" name using the [ExportMetadata] attribute. All of the built-in bundles do this. (You can see that the Versioning Bundle exports the name "Versioning" if you dig into the source code of one of its triggers).

    If a bundle is unnamed then it is always enabled, as long as it exists either within the RavenDB server assemblies, or in a separate assembly in the \plugins folder.

    It looks like the documentation needs updating, as it still says to place the Raven.Bundles.Versioning.dll assembly in the plugins folder. That no longer exists, as it was moved into the main RavenDB server assemblies in 2.0. So for this particular bundle, just editing the settings will be sufficient.

    The settings for a named tenant database are kept in the system database in a document called Raven/Databases/<YourDatabaseName>. Simply edit this document once, and the bundle is activated. Here is an extension method that will do that for you:

    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(';').Contains(bundleName, StringComparer.OrdinalIgnoreCase))
                settings[Constants.ActiveBundles] = activeBundles + ";" + bundleName;
    
            session.SaveChanges();
        }
    }
    

    Using the above method, you can simply call:

    documentStore.ActivateBundle("Versioning", "YourDatabaseName");
    

    If you are running an embedded-mode database, there are no named tenant databases, so the procedure is a bit different. You can put settings into your own app.config file, or you can manipulate the documentStore.Configuration.Settings dictionary before your existing call to documentStore.Initialize(). Here is a revised version of the extension method that will work on an embedded database:

    public static void ActivateBundle(this EmbeddableDocumentStore documentStore, string bundleName)
    {
        var settings = documentStore.Configuration.Settings;
        var activeBundles = settings[Constants.ActiveBundles];
        if (string.IsNullOrEmpty(activeBundles))
            settings[Constants.ActiveBundles] = bundleName;
        else if (!activeBundles.Split(';').Contains(bundleName, StringComparer.OrdinalIgnoreCase))
            settings[Constants.ActiveBundles] = activeBundles + ";" + bundleName;
    }
    

    Using this method, you can simply do this:

    documentStore.ActivateBundle("Versioning");
    documentStore.Initialize();
    
    0 讨论(0)
提交回复
热议问题