khalidabuhakmeh/RavenMigrations — pull request #2 by seankearon (2013-10-25 18:12:16+00:00)
Title: Collection migrations

Added the ability to migrate a collection document-by-document as the JObject level. Makes it easier to approach structural changes to the documents.

[Migration(1, "CollectionDocumentMigration")]
public class CollectionDocumentMigration : Migration
{
public override void Down()
{
Alter.Collection("People", JoinFirstNameAndLastNameIntoName);
}

public override void Up()
{
Alter.Collection("People", SplitNameToFirstNameAndLastName);
}

private void JoinFirstNameAndLastNameIntoName(RavenJObject obj)
{
var first = obj.Value("FirstName");
var last = obj.Value("LastName");

obj["Name"] = first + " " + last;
obj.Remove("FirstName");
obj.Remove("LastName");
}

private void SplitNameToFirstNameAndLastName(RavenJObject obj)
{
var name = obj.Value("Name");
if (!string.IsNullOrEmpty(name))
{
obj["FirstName"] = name.Split(' ')[0];
obj["LastName"] = name.Split(' ')[1];
}
obj.Remove("Name");
}
}