Skip to content

Instantly share code, notes, and snippets.

@asapostolov
Created January 30, 2014 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asapostolov/8705990 to your computer and use it in GitHub Desktop.
Save asapostolov/8705990 to your computer and use it in GitHub Desktop.
Console application for fixing RavenDB conflicts through
namespace ConsoleApplication {
class Program {
static void Main( string[] args ) {
var store = new DocumentStore() {
Url = "<your-url-here>",
Credentials = new System.Net.NetworkCredential() {
UserName = "<your-username>",
Password = "<your-password>",
Domain = ""
},
DefaultDatabase="<your-database>"
};
store.RegisterListener( new TakeNewestConflictResolutionListener() );
store.Initialize();
var stop = false;
while ( !stop ) {
using ( var session = store.OpenSession() ) {
try {
var doc = session.Query<Order>( "ConflictsIndex" )
.Take( 1000 )
.ToList();
if ( doc.Count < 1000 ) {
stop = true;
}
} catch ( InvalidOperationException ex ) {
Console.WriteLine( "Exception: {0}", ex.Message );
}
}
}
Console.WriteLine("Documents are fixed!");
Console.ReadLine();
}
}
public class Order {
public string Id { get; set; }
public string OrderData { get; set; }
}
public class TakeNewestConflictResolutionListener: IDocumentConflictListener {
public bool TryResolveConflict( string key, JsonDocument[] conflictedDocs, out JsonDocument resolvedDocument ) {
var maxDate = conflictedDocs.Max( x => x.LastModified );
resolvedDocument = conflictedDocs.FirstOrDefault( x => x.LastModified == maxDate );
if ( resolvedDocument != null ) {
resolvedDocument.Metadata = new RavenJObject();
}
Console.WriteLine( "Resolved conflicts with ID {0}", key );
return resolvedDocument != null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment