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 504 posts 2072 karma points
    Jan 07, 2020 @ 03:46
    David Armitage
    1

    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. https://our.umbraco.com/documentation/Extending/database/

    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.

    David

  • Alan Mitchell 57 posts 279 karma points c-trib
    Feb 26, 2020 @ 11:54
    Alan Mitchell
    104

    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 https://our.umbraco.com/documentation/Extending/database/ 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
      migrationPlan.From(string.Empty).To<AddCommentsTable>("blogcomments-db");
    
      // Add a second step
      migrationPlan.From("blogcomments-db").To<AddExtraColumns>("blogcomments-AddExtraColumns");
    
     ...
    

    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"))
                {
                    Create.Column("Location").OnTable("BlogComments").AsString().Nullable().Do();
                    Create.Column("SessionId").OnTable("BlogComments").AsString(50).Nullable().Do();
                }
                else
                {
                    Logger.Debug<AddSignupSheetTable>("Additional columns (checked for Location) already exist, skipping migration");
                }
            }
        }
    

    Hope this helps!

  • Thomas 315 posts 602 karma points c-trib
    Feb 16, 2024 @ 13:06
    Thomas
    0

    Is it posible to a default value on create ?

  • David Armitage 504 posts 2072 karma points
    Jun 07, 2020 @ 04:22
    David Armitage
    0

    Hi Alan,

    Thanks for that. I revisited this issue in another project then came back over this thread. Sorry I must have missed the notification for this..

    Thanks for your help. You tip really pointed me in the right directed.

    It's very rare that I have to touch migrations and edit tables once created so it's very annoying if I forget to add a column I have to fiddle around recreating new migrations. I am not the best at using these since U7 was so easy.

    What I have found from your tip was..

    1. Once I created a migration / created tables. And there is a great turotial in this forum post. https://our.umbraco.com/forum/umbraco-8/98439-create-custom-database-table-in-umbraco-8

    2. And I then notice I have made a mistake and forgot to add a column or added a column as the wrong type.

    3. I then go and delete the table from the database.

    4. Then delete the new migration row in the table you pointed out. umbracoKeyValue

    5. I can then add my table changes to the NPoco class and then simply re-run that first migration.

    Seems to be the easy way. This will only be suitable for new tables. Obviously if this is for an existing table with lots of data then you wont want to do this and should look at migrating the correct way as per Alan's post.

    Thanks again.

    David

Please Sign in or register to post replies

Write your reply to:

Draft