dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.62k stars 1.96k forks source link

'dotnet ef database update' should mention that it reverts newer migrations than the target one #3061

Open raymens opened 3 years ago

raymens commented 3 years ago

The dotnet ef database update command does not mention a very important "feature". When you give a specific MIGRATION as argument, it will revert any newer migrations. My intent was to apply a specific migration of the past but it actually reverted earlier applied migrations.

I understand the reasoning of this process but I would say this could really be specifically mentioned as it can cause major issues and reverting a database is no fun task.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

roji commented 3 years ago

My intent was to apply a specific migration of the past but it actually reverted earlier applied migrations.

To understand better, what was your expectation when specifying a specific migration, given that your database was at a newer one? Or were you not aware at which migration your database was?

raymens commented 3 years ago

We had a migration that had an older date (with regard to the other "newer" migrations) as it was in a long-lived feature branch but was merged later than the "newer" migrations. Something like this:

  1. Migration applied
  2. Migration NOT applied (was in a separate branch, merged later than the migrations below
  3. Migration applied
  4. Migration applied

So in this scenario I wanted to only apply migration 2. Without paying attention to 3 or 4 as I knew they were not related (as in, other tables etc.). Part of me only wanting to apply the 2nd one is that it was older than the other ones which were already applied. So I thought I could reduce the risk to see what would happen if I would only apply a specific migration. According to the documentation of dotnet ef database update I did not see anything conflicting with this intent.

But when applying the migration I saw the notice of Reverting migration 3 and Reverting migration 4. I was fortunately able to clean that up nicely.

But to possibly make sure others don't make a similar mistake I created this issue.

The documentation I'm thinking of is something in the lines of:

Updates the database to the last migration or to a specified migration.
Specifying a specific migration will apply all migrations to to that migration and reverting any newer migrations already applied.

Update: Thinking more about this, a simple command to get the list of applied migrations like dotnet ef database list.

A dotnet ef database update --output sql that would export the SQL that will be executed would be interesting aswell. Also for a confirmation and verification step within the deployment.

But that's more of a feature request than a documentation issue :)

ajcvickers commented 3 years ago

We should consider improving the docs here.