Copied to clipboard

Flag this post as spam?

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

  • David Armitage 243 posts 923 karma points
    Jan 07, 2020 @ 03:46
    David Armitage

    Umbraco 8 | Migrations | Added Columns to Custom Table

    Hi Guys,

    I have just been learning how to do migrations with Umbraco 8. I am half the way their and successfully managed to create a custom table using composers / componets etc.

    I follow this guide.

    As a test I could pretty much copy and paste what was there into a class and it worked out of the box.

    I understood the comment at the end which basically said we shouldn't edit the schema directly once we have created the migration and we should use migrations to update / add columns to the new table.

    My question is...

    Has anyone got some example code how to deal with adding a new column to a table once the table has already been created using migrations? If anyone could use the example above as a starting point (possibly modifying it) would be awesome.

    I will be spending some time today to try and figure it out. I will obviously post here if I get to the bottom of it. In the meantime if anyone already knows the answer it would be much appreciated.

    Also does anyone know if it will be still safe to start from scratch as in delete the table from SQL server, add some extra columns into the migrations scheme and then run the code again. So basically only using migrations to handle generating the table.

    Thanks in advanced.


  • Alan Mitchell 41 posts 230 karma points c-trib
    Feb 26, 2020 @ 11:54
    Alan Mitchell

    Hi David,

    This reply maybe too late but I had the same question after creating a custom table and realising I needed a couple more columns. I already have the table on my dev environment, but not fully into live, and using migrations will keep everything in sync.

    Here is an example extending the code from You can add extra code into the same class which is good for simple changes like this.

    Start by adding an extra step to the Initialize method, which specifies a migration to follow on from the first step.

      // Each step in the migration adds a unique value
      // Add a second step

    This keeps track of which migrations have been run on the current database. It's like a code version of dbUp or roundhousE, but it chains the migrations together to keep things in order.

    Hot Tip: Look in the umbracoKeyValue database table to check the last migration step that was run. It stores the magic strings from this code, so for example if you used the example code successfully there will be a row for Umbraco.Core.Upgrader.State+BlogComments with a value of blogcomments-db.

    Next create a new method, which has the migration commands you need. Here's how I added two columns:

     public class AddExtraColumns : MigrationBase
            public AddExtraColumns(IMigrationContext context) : base(context) {}
            public override void Migrate()
                Logger.Debug<AddSignupSheetTable>("Running migration {MigrationStep}", "AddExtraColumns");
                if (!ColumnExists("BlogComments","Location"))
                    Logger.Debug<AddSignupSheetTable>("Additional columns (checked for Location) already exist, skipping migration");

    Hope this helps!

Please Sign in or register to post replies

Write your reply to: