Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Søren Erland Vestø 19 posts 42 karma points
    Jan 03, 2020 @ 16:18
    Søren Erland Vestø
    0

    Custom migration not being run

    (Edit: I can't seem to post this to a proper category. My apologies.)

    I am trying to use a custom migration to alter a custom table in the database.

    My problem is that although the umbracoMigration table is updated with seemingly correct information about my custom migration after using Execute on MigrationRunner the migration has not been run.

    I am using this code to run migrations

            private void RunMigrations(string productName)
        {
            // Find current (if any) version
            var currentVersion = new SemVersion(0, 0, 0);
            var latestMigration = ApplicationContext.Current.Services.MigrationEntryService.GetAll(productName).OrderByDescending(x => x.Version).FirstOrDefault();
            if (latestMigration != null)
            {
                currentVersion = latestMigration.Version;
            }
    
            var thisAssembly = Assembly.GetExecutingAssembly();
    
            // Find target version (migration with largest semver)
            var migrations =
                from t in thisAssembly.GetTypes()
                let attributes = t.GetCustomAttributes(typeof(MigrationAttribute), true)
                where attributes != null && attributes.Length > 0
                select new { Type = t, Attribute = attributes.Cast<MigrationAttribute>().OrderByDescending(attr => attr.TargetVersion).First() };
    
            var targetVersion = migrations.OrderByDescending(m => m.Attribute.TargetVersion).FirstOrDefault()?.Attribute.TargetVersion;
            if (targetVersion != null)
            {
                var semTarget = new SemVersion(targetVersion);
                if (semTarget == currentVersion)
                {
                    // No migrations missing
                    return;
                }
    
                // Run migration(s)
                var migrationsRunner = new MigrationRunner(
                    ApplicationContext.Current.Services.MigrationEntryService,
                    ApplicationContext.Current.ProfilingLogger.Logger,
                    currentVersion,
                    semTarget,
                    productName
                );
    
                try
                {
                    migrationsRunner.Execute(UmbracoContext.Current.Application.DatabaseContext.Database, true);
                }
                catch (HttpException e)
                {
                    // Do nothing
                }
                catch (Exception e)
                {
                    LogHelper.Error<DatabaseEvent>("Error running migrations", e);
                }
            }
        }
    

    This is my migration:

        [Migration("1.0.1", 1, "productname")]
    public class _20200103_ImageURI_on_EventContent : MigrationBase
    {
        public _20200103_ImageURI_on_EventContent(ISqlSyntaxProvider sqlSyntax, ILogger logger) : base(sqlSyntax, logger) {}
    
        public override void Down()
        {
            Delete.Column("ImageUri")
                .FromTable(DbConstants.Tables.EventContent);
        }
    
        public override void Up()
        {
            Alter.Table(DbConstants.Tables.EventContent)
                .AddColumn("ImageUri")
                .AsString(512)
                .Nullable();
        }
    }
    

    The code running the migration is being hit and is finding the correct current and target versions. No exceptions are being caught.

Please Sign in or register to post replies

Write your reply to:

Draft