Created
August 10, 2012 09:22
-
-
Save AlexZeitler/3312858 to your computer and use it in GitHub Desktop.
Renaming entities with RavenDb patching API
This file contains hidden or 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
using System.Linq; | |
using System.Threading; | |
using Raven.Abstractions.Data; | |
using Raven.Abstractions.Indexing; | |
using Raven.Client; | |
using Raven.Client.Embedded; | |
using Raven.Json.Linq; | |
using Xunit; | |
namespace RavenDbPatching.StringIdPatching { | |
public class StringIdPatchingTests { | |
[Fact] | |
public void CanRenameEntityWithStringId() { | |
var documentStore = new EmbeddableDocumentStore() { RunInMemory = true }.Initialize(); | |
createDocumentsByEntityNameIndex(documentStore as EmbeddableDocumentStore); | |
var group = new Group { | |
Name = "Cars" | |
}; | |
using (var session = documentStore.OpenSession()) { | |
session.Store(group); | |
session.SaveChanges(); | |
} | |
while (documentStore.DatabaseCommands.GetStatistics().StaleIndexes.Length != 0) { | |
Thread.Sleep(10); | |
} | |
new Migrations(documentStore).Migrate(); | |
while (documentStore.DatabaseCommands.GetStatistics().StaleIndexes.Length != 0) { | |
Thread.Sleep(10); | |
} | |
using (var session = documentStore.OpenSession()) { | |
var category = session.Query<Category>().FirstOrDefault(); | |
Assert.NotNull(category); // fails | |
} | |
} | |
private void createDocumentsByEntityNameIndex(EmbeddableDocumentStore store) { | |
var database = store.DocumentDatabase; | |
if (database.GetIndexDefinition("Raven/DocumentsByEntityName") == null) { | |
database.PutIndex("Raven/DocumentsByEntityName", new IndexDefinition { | |
Map = | |
@"from doc in docs | |
let Tag = doc[""@metadata""][""Raven-Entity-Name""] | |
select new { Tag, LastModified = (DateTime)doc[""@metadata""][""Last-Modified""] };", | |
Indexes = | |
{ | |
{"Tag", FieldIndexing.NotAnalyzed}, | |
}, | |
Stores = | |
{ | |
{"Tag", FieldStorage.No}, | |
{"LastModified", FieldStorage.No} | |
} | |
}); | |
} | |
} | |
} | |
public class Group { | |
public string Id { get; set; } | |
public string Name { get; set; } | |
} | |
public class Category { | |
public string Id { get; set; } | |
public string Name { get; set; } | |
} | |
public class Migrations { | |
readonly IDocumentStore _documentStore; | |
public Migrations(IDocumentStore documentStore) { | |
_documentStore = documentStore; | |
} | |
public void Migrate() { | |
_documentStore.DatabaseCommands.UpdateByIndex( | |
"Raven/DocumentsByEntityName", | |
new IndexQuery { | |
Query = "Tag:Group" | |
}, | |
new[] | |
{ | |
new PatchRequest | |
{ | |
Type = PatchCommandType.Modify, | |
Name = "@metadata", | |
Nested = new[] | |
{ | |
new PatchRequest | |
{ | |
Type = PatchCommandType.Set, | |
Name = "Raven-Entity-Name", | |
Value = new RavenJValue("Category") | |
} | |
, | |
new PatchRequest | |
{ | |
Type = PatchCommandType.Set, | |
Name = "Raven-Clr-Type", | |
Value = new RavenJValue("RavenDbPatching.StringIdPatching.Category, RavenDbPatching") | |
} | |
} | |
} | |
}, false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment