Migrations not updating

Topics: Troubleshooting, Writing modules
Oct 30, 2012 at 9:49 PM

Really having problems getting migrations to work.  It's hard to pin point the problem exactly cause it doesn't happen always.

I currently have a migration which is at version 3 but if I check the [Orchard_Framework_DataMigrationRecord] table I see Orchard thinks it's at 2.

Is there something I need to do get the migration to update? I'm under the impression it's an automatic.  Is there a way to force the migration manually?

Maybe you're thinking, well you got to version 2 ok, so what's the problem.  But getting there was very much a trial / error / magically working after a restart kind of thing.

(Also having problems with dynamic compilation but that seems to be a known bug http://orchard.codeplex.com/workitem/19191)

There's nothing in the logs. 

Below is my migration which is stuck on version 2.

Any suggestions?

Thanks

 

using System.Data;
using Orchard.ContentManagement.MetaData;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using Ignite.Apartments.Models;

namespace Ignite.Apartments{
    public class Migrations : DataMigrationImpl {

        public int Create() {
		SchemaBuilder.CreateTable("ApartmentRecord", table => table
				.ContentPartRecord()
                .Column("FurtherDescription", DbType.String)
			);

            ContentDefinitionManager.AlterPartDefinition(
                typeof(ApartmentPart).Name, cfg => cfg.Attachable());

          
            ContentDefinitionManager.AlterTypeDefinition(typeof(ApartmentPart).Name,
               cfg => cfg
                   .WithPart("MapPart")
                   .WithPart("CommonPart")
                   .WithPart("TitlePart")
                   .WithPart("AutoroutePart", builder => builder
                       .WithSetting("AutorouteSettings.AllowCustomPattern", "true")
                       .WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "false")
                       .WithSetting("AutorouteSettings.PatternDefinitions", "[{Name:'Title', Pattern: '{Content.Slug}', Description: 'apartments'}]")
                       .WithSetting("AutorouteSettings.DefaultPatternIndex", "0"))
                   .WithPart("AdminMenuPart", p => p.WithSetting("AdminMenuPartTypeSettings.DefaultPosition", "3"))
               );


            return 1;
        }

        public int UpdateFrom1()
        {
            ContentDefinitionManager.DeleteTypeDefinition(typeof(ApartmentPart).Name);
            return 2;
        }

        public int UpdateFrom2()
        {
          ContentDefinitionManager.AlterTypeDefinition("Apartment",
               cfg => cfg
                   .WithPart("MapPart")
                   .WithPart("ApartmentPart")
                   .WithPart("CommonPart")
                   .WithPart("TitlePart")
                   .WithPart("AutoroutePart", builder => builder
                       .WithSetting("AutorouteSettings.AllowCustomPattern", "true")
                       .WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "false")
                       .WithSetting("AutorouteSettings.PatternDefinitions", "[{Name:'Title', Pattern: '{Content.Slug}', Description: 'apartments'}]")
                       .WithSetting("AutorouteSettings.DefaultPatternIndex", "0"))
                   .WithPart("AdminMenuPart", p => p.WithSetting("AdminMenuPartTypeSettings.DefaultPosition", "1"))
                   .Creatable()
                   
               );

            return 3;
        }
    }
}

Oct 31, 2012 at 11:37 AM

Well I seem to have fixed this problem by duplicating the last migration and incrementing the function name and return number.

I'm feels like if the migration doesn't run correctly the first time then orchard doesn't try again. 

However that theory doesn't correspond to what's in my migrations table.

Oct 31, 2012 at 5:00 PM
Edited Oct 31, 2012 at 6:39 PM

Migrations are tricky for me as well, particularly when developing because you may change your mind about how something should work. It's not clear whether doing an "UpdateFrom'n'" (where 'n' is the update you are performing and the ending "return" is incremented by +1) will delete/clear/erase something you've done before. However, you are on the right track. Also, I've learned that after you are done developing you can try to roll up the migrations you've updated into the first one (ending in "return 1") - useful when you are done and want to deploy your module. Just remember at that point that you are starting from scratch and so it would be UpdateFrom1 / return 2, UpdateFrom2, return 3 for any additional changes.

Be sure to check out the Orchard Docs which discusses data access (including migrations).