Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Migrations - Managing In Branches

I've been using Entity Framework code first migrations for a while now on a brand new project, and they've been working fine so far on a the main trunk version of the application.

However, we're now at a point in the project where branches are being created as we have multiple workstreams going on. As part of the last lot of work, we realised that using migrations across branches can be problematic - so my question is what have people found the best way of managing this?

For example (I've obviously simplified these for the sake of discussion):

Branch A: Developer 1 adds an 'Add-UserDateCreated' migration which adds a field to the User entity. The migration file contains the code to add the field, and has the model state at the time.

Branch B: Developer 2 adds an 'Add-UserMiddleName' migration which adds another field to the User entity. The migration file contains the code to add the field, and the has the model state at the time (but it obviously DOESN'T have the field added in the other migration).

These migrations work fine on their branches, but when you merge them back into the trunk, you get stuck:

  • You can't just keep the individual migration files as the stored model state won't be correct. For example, the 'Add-UserMiddleName' migration SHOULD have the state of the model with the 'Add-UserDateCreated' field added - but it won't.
  • You can't merge the migrations into one file as you hit the same issue*

Which means the only way to truely avoid these issues is to work on a different copy of the database for each branch, ignore the migrations when you do the merge into the trunk, and add a one shot master migration when the merge is complete (on the trunk version of the database) - but then you potentially end up with lots of changes in one migration which is never a good idea (and also lose any custom code you've written in your migration class).

So how do other people manage these kind of situations? I'd be curious to know other peoples' opinions.

*I'm curious what issues this would actually cause in the real world, if anyone knows?

like image 885
stevehayter Avatar asked May 09 '13 09:05

stevehayter


1 Answers

When you are working on a branch and require a migration, you always need to consider the state of the other (active) branches. For example, if there are migrations on trunk that you do not have on the branch, you should merge them to the branch before creating your new migration. Once you have created the migration on the branch, it is probably a good idea to merge it back to trunk.

So in your example, developer A and B needs to communicate with each other. Developer B, realizing that a migration is needed, should check if other migrations have been done and merge them to her branch before creating the "middle user name" migration.

I find that it is useful to think of migrations as a dead-lock for the entire team (if that makes sense to you.) New migrations or plans to create them should be mentioned in daily stand ups. Sometimes, when things are hectic, it might be useful to keep a list of all migrations on a white board for all team members to see.

I also find that it is crucial that the migrations are small commits, making them easily portable between branches.

It should also be noted that it helps to use a version control system that is good with branching, merging and cherry-picking, like Git, for example.

like image 66
martin Avatar answered Oct 29 '22 15:10

martin