Created
January 30, 2014 10:30
-
-
Save asapostolov/8705990 to your computer and use it in GitHub Desktop.
Console application for fixing RavenDB conflicts through
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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