Orchard Data Migration Classes Automatically Update in Orchard 1.4

Orchard Data Migration Classes Automatically Update in Orchard 1.4

I've talked about the main new features in Orchard 1.4 in a number of Orchard CMS Tutorials. Various features like Autoroute, Projector Module, new custom fields, and Orchard performance enhancements are a wonderful addition to Orchard. Now I would like to write a series of blog posts about those features in Orchard 1.4 that aren't so obvious and probably not even documented. Unless you are an Orchard Web Developer who has been developing Orchard Websites for quite some time, you are probably not even aware of these changes. First Up - Orchard Data Migration Classes Automatically Update in Orchard 1.4.

Orchard Data Migration Classes

If you have been developing custom Orchard Modules, you are no doubt aware of Orchard Data Migration Classes. Data Migration Classes in Orchard allow you to systematically create database tables in Orchard, modify the schema of Orchard database tables, create and alter content types, and do anything else in an automated fashion. A Data Migration Class in Orchard will look something like this.

public class Migrations : DataMigrationImpl {
    public int Create() {
        SchemaBuilder.CreateTable("CommonPartRecord",              table => table                 .ContentPartRecord()                 .Column<int>("OwnerId")                 .Column<DateTime>("CreatedUtc")                 .Column<DateTime>("PublishedUtc")                 .Column<DateTime>("ModifiedUtc")                 .Column<int>("Container_id")         );
        return 1;     }
    public int UpdateFrom1() {
        SchemaBuilder.AlterTable("CommonPartRecord",              table => table                 .DropColumn("OwnerId")         );
        return 2;     }
    public int UpdateFrom2() {
        SchemaBuilder.AlterTable("CommonPartRecord",              table => table                 .AddColumn<string>("SomeData")         );
        return 3;     } }

Normally when you come out with your Orchard Module it only has a Create() Method that instructs Orchard to run that method during installation of the module. As you come out with newer versions of the module you create additional UpdateFromX() Methods where X is an integer that occurs in sequential order. Orchard keeps track of the last update method it ran so that if it detects a new version of the module installed it will run any later update methods.

Manually Updating Orchard Modules Prior to Orchard 1.4

Prior to Orchard 1.4 whenever Orchard detected a newer version of an Orchard Module that required a migration to be run it prompted you in the Orchard Admin Panel and required you to click the Update Link on the Orchard Module to be updated.

Data Migration Class Update in Orchard CMS Update Module

This prompt and manual action was somewhat nice in that it warned you of an impending update. It did have a serious drawback, however, in that the code for the module was already updated at this point and it probably depended on those changes in the data migration file to operate correctly. Since those changes don't occur until you click the Update Link, there is a good chance your Orchard Website will not operate correctly until you click the Update Link. In other words, you really don't have any choice but to Update the module, anyway, so the warning and manual action is pretty much useless.

Data Migration Classes Automatically Run in Orchard 1.4

In Orchard 1.4 the Orchard Web Developer or Administrator is no longer prompted to manually update the Orchard modules. Orchard automatically runs any new UpdateFromX() Methods in the Orchard Data Migration Class. This was bewildering the first time it occurred as I thought the data migration updates didn't run. On the otherhand, it was really a false sense of security the way it was implemented and could cause problems with the Orchard Website so I am glad the manual intevention was removed in its previous implementation.

Conclusion

For more information on Orchard 1.4, check out What's New in Orchard 1.4 as well as my numerous Orchard Tutorials.

I am available for hire if you need an Orchard Web Developer for your next project.