Skip to content

Instantly share code, notes, and snippets.

@ZNS
Created April 22, 2017 11:19
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 ZNS/7564c7146b986a3cbb13ce3eda765b52 to your computer and use it in GitHub Desktop.
Save ZNS/7564c7146b986a3cbb13ce3eda765b52 to your computer and use it in GitHub Desktop.
public class ReplicationWithTriggers : ReplicationBase
{
public class DeleteTrigger : AbstractDeleteTrigger
{
public override void OnDelete(string key, TransactionInformation transactionInformation)
{
using (Database.DisableAllTriggersForCurrentThread())
{
RavenJObject doc = new RavenJObject();
doc.Add("DocId", RavenJToken.FromObject(key));
doc.Add("Action", "Delete");
doc.Add("When", RavenJToken.FromObject(DateTime.UtcNow));
RavenJObject meta = new RavenJObject();
meta.Add("Raven-Entity-Name", RavenJToken.FromObject("Audits"));
string docKey = "audits/" + DateTime.UtcNow.Ticks.ToString();
Database.Documents.Put(docKey, null, doc, meta, transactionInformation);
}
}
}
class Audit
{
public string Id { get; set; }
public string DocId { get; set; }
public string Action { get; set; }
public DateTime When { get; set; }
}
class TestDoc
{
public int Id { get; set; }
public string Name { get; set; }
}
[Fact]
public void TestReplicationWithTriggers()
{
using (var sourceServer = GetNewServer(8076, activeBundles: "Replication", configureConfig: (InMemoryRavenConfiguration config) => { config.Catalog.Catalogs.Add(new TypeCatalog(typeof(ReplicationWithTriggers.DeleteTrigger)));} ))
using (var source = NewRemoteDocumentStore(ravenDbServer: sourceServer))
using (var destinationServer1 = GetNewServer(8077, activeBundles: "Replication", configureConfig: (InMemoryRavenConfiguration config) => { config.Catalog.Catalogs.Add(new TypeCatalog(typeof(ReplicationWithTriggers.DeleteTrigger))); }))
using (var destination1 = NewRemoteDocumentStore(ravenDbServer: destinationServer1))
{
CreateDatabaseWithReplication(source, "testDB");
CreateDatabaseWithReplication(destination1, "testDB");
SetupReplication(source, "testDB", store => true, destination1);
using (var session = source.OpenSession("testDB"))
{
session.Store(new TestDoc() { Id = 1, Name = "Test #1" });
session.SaveChanges();
}
WaitForReplication(destination1, "TestDocs/1", "testDB");
using (var session = source.OpenSession("testDB"))
{
var docMaster = session.Load<TestDoc>(1);
Assert.True(docMaster != null);
session.Delete<TestDoc>(docMaster);
session.SaveChanges();
}
//Wait for deletion
TestDoc docSlave = null;
for (int i = 0; i < 20; i++)
{
using (var session = destination1.OpenSession("testDB"))
{
docSlave = session.Load<TestDoc>(1);
if (docSlave == null)
{
break;
}
}
Thread.Sleep(500);
}
Assert.True(docSlave == null, "Document was not deleted");
//Check that audit trigger was run
using (var session = source.OpenSession("testDB"))
{
var audits = session.Query<Audit>().Customize(x => x.WaitForNonStaleResults()).Take(10).ToList();
Assert.True(audits.Count != 0, "No audits found");
Assert.True(audits.Any(x => x.DocId == "TestDocs/1" && x.Action == "Delete"), "Audit for delete was not added");
}
}
}
private static void CreateDatabaseWithReplication(DocumentStore store, string databaseName)
{
store.DatabaseCommands.GlobalAdmin.CreateDatabase(new DatabaseDocument
{
Id = databaseName,
Settings =
{
{"Raven/DataDir", "~/Tenants/" + databaseName},
{"Raven/ActiveBundles", "Replication"}
}
});
}
private static List<ReplicationDestination> SetupReplication(IDocumentStore source, string databaseName, Func<IDocumentStore, bool> shouldSkipIndexReplication, params IDocumentStore[] destinations)
{
var replicationDocument = new ReplicationDocument
{
Destinations = new List<ReplicationDestination>(destinations.Select(destination =>
new ReplicationDestination
{
Database = databaseName,
Url = destination.Url,
}))
};
using (var session = source.OpenSession(databaseName))
{
session.Store(replicationDocument, Constants.RavenReplicationDestinations);
session.SaveChanges();
}
return replicationDocument.Destinations;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment