Upgrade migration for 1.4 module

Topics: Writing modules
Mar 3, 2012 at 9:29 AM
Edited Mar 3, 2012 at 9:31 AM

I've got my module working in 1.4, but I'd like there to be a smooth story for people upgrading and I'm not sure the best way to write the migration.

I've currently just replaced the Create with this:

public int Create()
{
    ...
    // Content item: Bing Place
    ContentDefinitionManager.AlterTypeDefinition("BingPlace", cfg => cfg
        .WithPart("BingLocationPart")
        .WithPart("CommonPart")
//.WithPart("RoutePart") // No longer available in 1.4
.WithPart("TitlePart") .WithPart("AutoroutePart", builder => builder .WithSetting("AutorouteSettings.AllowCustomPattern", "true") .WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "false") .WithSetting("AutorouteSettings.PatternDefinitions", "[{Name:'Title', Pattern: '{Content.Slug}', Description: 'my-place'}]") .WithSetting("AutorouteSettings.DefaultPatternIndex", "0")) .WithPart("BodyPart") .WithPart("CommentsPart") .WithPart("TagsPart") .Creatable() ); return 1; }

 

I'm thinking I should change the create method to return 2 and add an UpdateFrom1 method that adds the new parts and removes the RoutePart, but I'm not sure how I'd remove the RoutePart, or even if I should because it might break the auto upgrade module.

So, what is the best thing to do to make sure everything goes smoothly.

Developer
Mar 3, 2012 at 11:17 AM
Edited Mar 3, 2012 at 11:18 AM

I don't think you should modify Create if your module has already been released into the wild. Orchard will never call it again if your migration's version is set to 1.

Instead, you should add an UpdateFrom1 and add the TitlePart and AutoroutePart to your content type. Just after that, loop though all of the content items, read in their RoutePart values, and assign those values to the justly attached AutoroutePArt and TitlePart.

After that, detach the RoutePart from your content type.

It should go smooth, but it goes without saying that you would need to test this locally.

Mar 3, 2012 at 3:20 PM

I thought some of that might happen with the auto upgrade module, but I guess I shouldn't rely on the user running that.

Developer
Mar 3, 2012 at 4:30 PM
Edited Mar 3, 2012 at 4:30 PM

You definitely should write UpdateFromX() methods but it's a good practice to merge changes into Create() too and let it return the value of the last Update(). This way you can ensure that new users won't go through all the updates when they install the module. That trick is employed in all Orchard modules too.

Developer
Mar 3, 2012 at 4:33 PM

You're right, thanks!

Mar 20, 2012 at 1:17 PM

Are you sure this is best practice? The blogs module just seems to just create with the new content types and makes no mention of the the old route content part.