Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Apply a Migration that comes "Before" Newer Migrations

I am not even sure how to phrase this one. I have migrations applied to the db: Migration One. Migration Two. Migration Three. I just pulled a "Migration 2.5" - if I try to revert to migration Two and `Update-Database', the db updates successfully and says there are pending changes. These "pending changes" are Migration 2.5, even though it's been applied - EF wants me to add them again.

Now, normally, I get around the problem like this - revert to Migration One or Two, delete Migration 2.5, apply changes, and create a new "Migration 4" that comes LAST. This way the problem is avoided. However, I cannot change around the order of the migrations now. How do I solve this?


Edit: I think it's important to note that my db does work properly. If there is a db/code mismatch, EF throws an error stating so - this isn't happening. The problem is that the moment I do need to Add-Migration with a legitimate change, it will try to duplicate the "2.5" changes along with the actual legitimate changes.

like image 292
VSO Avatar asked Sep 27 '22 02:09

VSO


1 Answers

This isn't really an answer to the original question - I have not found a way to make EF recognize that it has applied an "out of order" migration. However, here are two ways to get around the problem, which, more specifically, is getting the quoted error when trying to pull an "out of order" migration:

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.

  1. Create an empty "Fix" migration - this is a fairly simple, but dirty solution. You simply run Add-Migration. You then delete migration actions, so you should be left with empty blocks like so:

    public override void Up()
    {
    }
    
    public override void Down()
    {
    }
    

    You then run Update-Database. The reason this works is simple - as pointed out earlier, EF the "out of order" migration was not applied, despite the fact that it was actually applied the first time you ran Update Database- note that you should be able to verify that it was applied with Get-Migrations. Anyway, for some reason EF wants to duplicate it, but will be placated by an empty migration instead. Makes no sense to me.

This solution is undesirable as it leaves a pointless migration, but may be your only route if you cannot easily get to the environments which the out of order migration has already been pushed to.

a. Update-Database Target-Migration "The Migration Before The Out of Order One" to revert to the migration before this problem began.
b. Now manual delete the "Out of Order" migration file from your migrations folder.
c. Run Update-Database again to bring your db current with all changes EXCEPT the "Out of Order" migration.
d.Run Add-Migration - this will recreate the "Out of Order" migration you deleted in the beginning, but in its proper,or at least functional, place.
e. Run Update-Database and you are done.

Note that this will create problems unless synchronized with your team for reasons I am too lazy to go into. #2 is better, but #1 is easier and faster.

P.S. Stackoverflow nested list syntax is too much for me to handle.

like image 100
VSO Avatar answered Nov 15 '22 06:11

VSO