How to import raven db with versioning bundle - ravendb

I have a database with versioning bundle turned on. I make an export and then try to import the exported dump to newly created db. I get the exception "Modifying a historical revisionis not allowed". I found this question and answer from Ayende, that its by design. But how do I import data to an empty database if the versioning bundle was turned on and there are revisions in it?
For now I did the following thing: I make a new database without a versioning bundle, but with replication bundle in it. Import to that db(and it works), but I have a lot of duplicates if I perform search.
After that I create another new database, with replication and versioning bundle turned on. And I replicate from the db with duplicates this db. And it works, but it seems its a lot of things todo.
Am I doing the right thing? Is there an easier way to get your data from the dump?

I've had the same issue. The ONLY way I could get the import to work was to do the following:
Create DB with Versioning enabled (They recommend that DBs be created with any bundles that may be used).
Disable the versioning bundle. I've done this by editing the database settings and removing the bundle from the "Raven/ActiveBundles" setting.
Import your database.
Enable the versioning bundle. Just add the "Versioning" bundle back to the "Raven/ActiveBundles" setting.
If anyone has a better idea, I'd love to hear it. :)

The new version of RavenDb 3.0 give the permission to import with the Bundle already activated. So all the trick of disabling and enabling is not of any use anymore. But you might have new issues like Index not in synch with your revision or having the "versioning" not displayed in the Settings=>Versioning window.
To fix your versioning if they don't show in the versioning window but that they are visible in the System Documents, you need to add the Id individually or run some kind of code.
Option parameter for import since RavenDb 3.0.3745:
--disable-versioning-during-import
Fix of Versioning not containing an Id :
private void UpdateVersioning(string destinationRavenDbServer, string databaseName)
{
using (var documentStore = new DocumentStore { Url = destinationRavenDbServer, DefaultDatabase = databaseName })
{
documentStore.Initialize();
using (var session = documentStore.OpenSession())
{
var versioningInfoList = session.Advanced.LoadStartingWith<RavenJObject>("Raven/Versioning/", pageSize: 1024 );
foreach (var versioningInfo in versioningInfoList)
{
if (!versioningInfo.ContainsKey("Id"))
{
var fullInternalId = session.Advanced.GetDocumentId(versioningInfo);
var idSplitted = fullInternalId.Split('/');
var newId = idSplitted[idSplitted.Length - 1];
versioningInfo.Add("Id", newId);
}
}
session.SaveChanges();
}
}
}
Fix for re-indexing all the database:
private void ResetAllIndexes(string destinationRavenDbServer, string databaseName)
{
using (var documentStore = new DocumentStore { Url = destinationRavenDbServer, DefaultDatabase = databaseName })
{
documentStore.Initialize();
var indexes = documentStore.DatabaseCommands.GetIndexNames(0, 1024); // Update the 1024 first index, but you get the idea
foreach (var indexName in indexes)
{
documentStore.DatabaseCommands.ResetIndex(indexName);
}
}
}

Related

How to verify if NHibernate hbm.xml are matching with a particular SQL schema?

I had an ASP.NET MVC web application, using NHibernate as ORM on SQL Server 2008 R2. When we deployed to the server, we can update our database any time (some are ad-hoc changes).
The problem is when the database schema change, the application crashed because NHibernate .hbm.xml files are no longer matching with the DB schema.
How do I verify that my current *.hbm.xml file are matching with the database schema ? And how to detect the mismatch early in ASP.NET MVC ?
You can do the checking when application runs, could be in the global asax.
protected void Application_Start()
{
}
The connection string is the key to get the expected schema.
<property name="connection.connection_string">Server=.;Initial Catalog=TheExpectedSchema; ..</property>
First read the expected schema by reading it from nhibernate config and retrieve it from Initial Catalog part (if the database is oracle, probably use the User ID part).
NHibernate.Cfg.Configuration config = ...;
var conStr = config.Properties["connection.connection_string"];
var match = Regex.Match(conStr, "Initial Catalog *= *([^;]*) *");
var expectedSchema = match.Groups[1].Value;
Then read the actual schema by reading it from *.hbm.xml file.
<hibernate-mapping schema="TheActualSchema"
If the files are put under App_Data directory, read each file and use xml document to get the schema.
var appDataDir = new DirectoryInfo(HttpContext.Server.MapPath("~/App_Data"));
var files = appDataDir.GetFiles("*.hbm.xml");
foreach (var file in files)
{
var doc = new XmlDocument();
doc.Load(file.FullName);
var actualSchema = doc.DocumentElement.GetAttribute("schema");
if (actualSchema != expectedSchema)
{
// Proper handling here (an example would be throwing exception).
throw new Exception(string.Format("Expected schema: {0}, actual schema {1}", expectedSchema, actualSchema));
}
}

RavenDB fails with ConcurrencyException when using new transaction

This code always fails with a ConcurrencyException:
[Test]
public void EventOrderingCode_Fails_WithConcurrencyException()
{
Guid id = Guid.NewGuid();
using (var scope1 = new TransactionScope())
using (var session = DataAccess.NewOpenSession)
{
session.Advanced.UseOptimisticConcurrency = true;
session.Advanced.AllowNonAuthoritativeInformation = false;
var ent1 = new CTEntity
{
Id = id,
Name = "George"
};
using (var scope2 = new TransactionScope(TransactionScopeOption.RequiresNew))
{
session.Store(ent1);
session.SaveChanges();
scope2.Complete();
}
var ent2 = session.Load<CTEntity>(id);
ent2.Name = "Gina";
session.SaveChanges();
scope1.Complete();
}
}
It fails at the last session.SaveChanges. Stating that it is using a NonCurrent etag. If I use Required instead of RequiresNew for scope2 - i.e. using the same Transaction. It works.
Now, since I load the entity (ent2) it should be using the newest Etag unless this is some cached value attached to scope1 that I am using (but I have disabled Caching). So I do not understand why this fails.
I really need this setup. In the production code the outer TransactionScope is created by NServiceBus, and the inner is for controlling an aspect of event ordering. It cannot be the same Transaction.
And I need the optimistic concurrency too - if other threads uses the entity at the same time.
BTW: This is using Raven 2.0.3.0
Since no one else have answered, I had better give it a go myself.
It turns out this was a human error. Due to a bad configuration of our IOC container the DataAccess.NewOpenSession gave me the same Session all the time (across other tests). In other words Raven works as expected :)
Before I found out about this I also experimented with using TransactionScopeOption.Suppress instead of RequiresNew. That also worked. Then I just had to make sure that whatever I did in the suppressed scope could not fail. Which was a valid option in my case.

Ravendb doesn't save propery Raven/Hilo/LoggingMessages

I have a problem saving an object to Ravendb.
Everytime i save the object into Ravendb ,
it only save this below
Raven/Hilo/LoggingMessages
{
"Max": 32
}
I don't even have property called Max on LoggingMessages class.
And, it kept doing that.
I used this Ravendb in a project that use NserviceBus.
My assumption is that the Ravendb Client library that is used by NserviceBus is different with other Ravendb Client.
Because I have no problem saving an object of type LoggingMessage in other project that doesn't have NserviceBus.
LoggingMessage errormessage = new LoggingMessage();
errormessage.MessageBody = "test";
errormessage.MessageId = "test";
using (var store = new DocumentStore { ConnectionStringName = "RavenDB" } )
{
store.Initialize();
using (var session = store.OpenSession())
{
session.Store(errormessage);
session.SaveChanges();
}
}
That's how RavenDB generates IDs. Its a system document. Don't worry about that.
What's probably happening is that you are saving this document to the database that NServiceBus is using, but you are looking at either the RavenDB System Database, or you are writing it to a separate one.
In Raven Studio, check the "databases" list in the upper-right corner.
In your code, you can set the database name either in the connection string, or as a parameter to the new DocumentStore constructor, or as a parameter to the OpenSession method.

RavenDB IsOperationAllowedOnDocument not supported in Embedded Mode

RavenDB throws InvalidOperationException when IsOperationAllowedOnDocument is called using embedded mode.
I can see in the IsOperationAllowedOnDocument implementation a clause checking for calls in embedded mode.
namespace Raven.Client.Authorization
{
public static class AuthorizationClientExtensions
{
public static OperationAllowedResult[] IsOperationAllowedOnDocument(this ISyncAdvancedSessionOperation session, string userId, string operation, params string[] documentIds)
{
var serverClient = session.DatabaseCommands as ServerClient;
if (serverClient == null)
throw new InvalidOperationException("Cannot get whatever operation is allowed on document in embedded mode.");
Is there a workaround for this other than not using embedded mode?
Thanks for your time.
I encountered the same situation while writing some unit tests. The solution James provided worked; however, it resulted in having one code path for the unit test and another path for the production code, which defeated the purpose of the unit test. We were able to create a second document store and connect it to the first document store which allowed us to then access the authorization extension methods successfully. While this solution would probably not be good for production code (because creating Document Stores is expensive) it works nicely for unit tests. Here is a code sample:
using (var documentStore = new EmbeddableDocumentStore
{ RunInMemory = true,
UseEmbeddedHttpServer = true,
Configuration = {Port = EmbeddedModePort} })
{
documentStore.Initialize();
var url = documentStore.Configuration.ServerUrl;
using (var docStoreHttp = new DocumentStore {Url = url})
{
docStoreHttp.Initialize();
using (var session = docStoreHttp.OpenSession())
{
// now you can run code like:
// session.GetAuthorizationFor(),
// session.SetAuthorizationFor(),
// session.Advanced.IsOperationAllowedOnDocument(),
// etc...
}
}
}
There are couple of other items that should be mentioned:
The first document store needs to be run with the UseEmbeddedHttpServer set to true so that the second one can access it.
I created a constant for the Port so it would be used consistently and ensure use of a non reserved port.
I encountered this as well. Looking at the source, there's no way to do that operation as written. Not sure if there's some intrinsic reason why since I could easily replicate the functionality in my app by making a http request directly for the same info:
HttpClient http = new HttpClient();
http.BaseAddress = new Uri("http://localhost:8080");
var url = new StringBuilder("/authorization/IsAllowed/")
.Append(Uri.EscapeUriString(userid))
.Append("?operation=")
.Append(Uri.EscapeUriString(operation)
.Append("&id=").Append(Uri.EscapeUriString(entityid));
http.GetStringAsync(url.ToString()).ContinueWith((response) =>
{
var results = _session.Advanced.DocumentStore.Conventions.CreateSerializer()
.Deserialize<OperationAllowedResult[]>(
new RavenJTokenReader(RavenJToken.Parse(response.Result)));
}).Wait();

Error when trying add data to RavenDb

I'm using autofac and the interfaces are correctly resolved but this code fails with "No connection could be made because the target machine actively refused it 127.0.0.1:8081"
using (var store = GetService<IDocumentStore>())
{
using (var session = store.OpenSession())
{
session.Store(new Entry { Author = "bob", Comment = "My son says this", EntryId = Guid.NewGuid(), EntryTime = DateTime.Now, Quote = "I hate you dad." });
session.SaveChanges();
}
}
Here is the registration
builder.Register<IDocumentStore>(c =>
{
var store = new DocumentStore { Url = "http://localhost:8081" };
store.Initialize();
return store;
}).SingleInstance();
When I navigate to http://localhost:8081 I do get the silverlight management UI. Although I'm running a Windows VM and vmware and Silverlight5 don't play together. That's another issue entirely. Anyways does anyone see what I'm doing wrong here or what I should be doing differently? Thanks for any code, tips, or tricks.
On a side note, can I enter some dummy records from a command line interface? Any docs or examples of how I can do that?
Thanks All.
Just curious, are you switching RavenDB to listen on 8081? The default is 8080. If you're getting the management studio to come up, I suspect you are.
I'm not too familiar with autofac but, it looks like you're wrapping your singleton DocumentStore in a using statement.
Try:
using (var session = GetService<IDocumentStore>().OpenSession())
{
}
As far as dummy records go, the management studio will ask you if you want to generate some dummy data if your DB is empty. If you can't get silverlight to work in the VM, I'm not sure if there's another automated way to do it.
Perhaps using smuggler:
http://ravendb.net/docs/server/administration/export-import
But you'd have to find something to import.