Detected Resolved Migration not Applied to Database
One of the joys of working on an Ef-based application is that it's easy to deploy new code without having to go through the painful process of making sure your database schema is up to date. However, if you ever run into an issue where all your code has been deployed but your database isn't being updated with new tables or columns, then this post will show you how to get back on track and get those migrations applied.
What is Detected Resolved Migration not Applied to Database
Suppose you worked on version 2.0 of migration and applied it to the database. Another developer then sends in their code modifications, which contain a new migration version 1.4. Because you have a migration to apply (version 1.4), which is prior to the most recent migration already applied to the database, you will get the error above when you deploy the application (2.0).
Why This May Occur
The detected resolved migration not applied to database error can happen for a variety of reasons. Often, the culprit is a missing required column in one or more tables in your database. This error can also occur if you're attempting to migrate from or to an unsupported version of SQL Server or if there are unsupported characters in your data file name.
This could happen if numerous individuals are working on Flyway migrations at about the same time because Flyway wants migrations to be applied in the order they were created, based on the version number.
The Solution
When we encountered a situation where all the code had been deployed but we were getting this error, it was because of a couple of issues. The first issue is that our migrations had been generated using an older version of Laravel and didn't match the current version. To fix this, you'll want to make sure that your composer.json file has all of your dependencies up-to-date with:
Python
composer update --optimize-autoloader
You must apply down migrations first, then up migrations. Down migrations are applied before up migrations and if you fail to apply them, your migration will fail with the error Detected Resolved Migration not Applied to Database.
Model changes were detected, but database change may not have been applied. To fix this, examine the model changes and apply them manually in order of creation date ascending. If you do not remember which model was created when, or want to know what has changed between two models, use the diff command for details
bundle exec rails
db:migrate:diff
We got this error when there was an issue connecting to the database from our application code (which meant there were no EF DbContexts related to our main database that got created when we ran the app). To fix this, grab the connection string from your environment variables and run an explicit migration command in PMC.
Here's an example of how you would do it:
PMC> Add-Migration Migration1 -OutputDirectory .\Migrations -Verbose -Force
ignore this migration
set -ignoreIgnoredMigrations=true
To allow executing this migration
spring.flyway.outOfOrder=true
Conclusion
I hope you enjoyed reading this blog post. If you have any questions or comments, please leave them in our comments section below!