Blake Rivell
Blake Rivell

Reputation: 13875

Mistake in an EF Migration better to rollback, delete, re-add or just simply add new

I created a large migration that creates my initial schema and ran database update.

Afterwards I noticed that a field that is supposed to be of type DateTime is accidentally of type string. I fixed it in my model.

Now should I update the database to a migration prior to the one with the mistake, delete the bad migration, then re-add the migration again. Or just simply add a new migration that only patches the fix.

The reason I am leaning towards rolling back, deleting, then re-adding is because I wouldn't want any messed up migrations with incorrect info would I? I feel like it is pointless to have migrations that are incorrect.

I believe the confusion comes in for me because I have never used migrations in a production environment so I don't think I am aware of the power of them. So I would like someone to clear up whether old migrations can have mistakes or if they should all be free of mistakes and show the entire progress of the database as I add new models / entities.

Upvotes: 0

Views: 336

Answers (1)

bricelam
bricelam

Reputation: 30345

Migrations are just a tool to help you preserve data between schema changes. They're especially handy for managing multiple database servers like dev/test/production or dev-local databases in team environments.

If you only have one server, then rewriting a migration is cheap, so go ahead and correct it this way.

If everyone on your team has already applied the migration, or you've applied it to databases with data you would like to preserve, then it would be simpler to update the column using a new migration.

In other words, there is no right or wrong way to use Migrations, only a cost associated with rewriting history.

Upvotes: 1

Related Questions