Skip to content
florisrobbemont edited this page Sep 16, 2014 · 2 revisions

Usage

In order to use uMigrations you will need to things:

  • A migrations provider
  • An Umbraco application handler (existing handlers can be used).

Migration Provider

Simply add a class and implement the IMigrationsProvider interface. Or use the AbstractMigrationsProvider class which does the same and adds some handy methods

public class MigrationsProvider : AbstractMigrationsProvider
{
    public MigrationsProvider()
    {
        AddMigration("Add Home content type", 
                     application => application.Services.ContentTypeService.Save(new ContentType(-1)));
        AddMigration("Add Content content type", 
                     application => application.Services.ContentTypeService.Save(new ContentType(-1)));

        AddMigration("Adding Home page", 
                     application => application.Services.ContentService.CreateContent("Home", -1, "Home"));
        AddMigration("Adding About page", 
                     application => application.Services.ContentService.CreateContent("About", -1, "Content"));

        AddMigration("Complex migration", application =>
        {
            var parentPage = application.Services.ContentService.GetById(1001);
            application.Services.ContentService.CreateContent("About", parentPage, "Content");
        });
    }
}

Each migration will be run in the order they've been added to the collection. A migration can be a simple instruction or contain multiple lines of code. The name of the migration has to be unique. If there is a duplicate name uMigrations will throw an exception before running the migrations (this will keep your database correct).

A migration can also do more complex stuff like converting pages from one document type to another.

Running the migrations

To run the migrations you need the Migrator class. Simply create an instance and add your MigrationProvider:

var migrator = new Migrator(new MigrationProvider());
migrator.RunMigrations(applicationContext);
Clone this wiki locally