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.
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.
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.
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");
}
}
}
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.
And I then notice I have made a mistake and forgot to add a column or added a column as the wrong type.
I then go and delete the table from the database.
Then delete the new migration row in the table you pointed out. umbracoKeyValue
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.
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
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.
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.
Next create a new method, which has the migration commands you need. Here's how I added two columns:
Hope this helps!
Is it posible to a default value on create ?
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..
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
And I then notice I have made a mistake and forgot to add a column or added a column as the wrong type.
I then go and delete the table from the database.
Then delete the new migration row in the table you pointed out. umbracoKeyValue
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
is working on a reply...