efcore: How to do a missing migration check in CI
Ask a question
I have an idea to add “missing migration” check to my CI pipeline. This check should validate that DbSnapshot that is present in a branch matches models in that branch (in another words, it should validate that if I will create a migration, Up
and Down
would be empty).
Straightforward way of doing this would be to execute dotnet ef migrations add Test
and validate that resulting files match “empty migration” ones, but this seems like a hack.
Is there an easier way of doing this? Like dotnet ef migrations --verify-snapshot-up-to-date
or something?
Include provider and version information
EF Core version: Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 6.0 Operating system: Linux (for CI pipeline) IDE: Visual Studio 2022
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 11
- Comments: 26 (12 by maintainers)
Looks like we started talking about two separate issues here.
React to this comment with 🚀 if you’re interested in a command that checks whether there are any migrations that haven’t been applied to a database (i.e. if you need to run
dotnet ef database update
)React with 👀 if you’re interested in a command that checks there are any model changes that haven’t yet been added to a migration (i.e. if you need to run
dotnet ef migrations add
)React with both if you want both.
if you are ok with using some internal EF Core this does what you’re asking for:
@maxkoshevoi We discussed this in triage; there isn’t an easy way to do this now, but we agree that it would be useful.
Notes from triage:
migrations list
Note from triage. This issue is still tracking having a command-line experience for asking specifically if there are pending migrations A command that returns zero if there are no pending migrations and non-zero if there are would be useful in the C.I. where the return value is used to stop/continue the process.
This command would be something like
dotnet ef migrations check-pending
.Most people I’ve seen write a unit test:
#22105 is also about adding a simpler API (something like
context.Database.HasPendingModelChanges()
) to do this.The issue here is not that migration hasn’t been applied, but that it hasn’t been created (there’re some model changes that are not reflected in
DbContextModelSnapshot.cs
), so I don’t think your solution is applicable here.I’ve created a PR adding the
check-pending
command if people subscribed here have any notes! #31164I’ve started some work on implementing this but would need sign off from the team before making a PR.
Based on @ajcvickers comment it looks like the team was more leaning towards this being it’s own command but from a simplicity standpoint it would be easier if it were an optional switch on the
migrations add
command. The experience for using such a switch would be like:The weirdness of this usage is that a lot of us want it to fail if a migration with actual operations is created. It’s possible to invert that check based on exit code in a CI pipeline but generally it’s more annoying. The other way would be to fail on there being actual operations but I personally haven’t thought of a name that I like. Things I have thought about:
--require-empty
,--verify-no-migrations
, and the one thrown out in the issue body--verify-snapshot-up-to-date
. I’m completely open to suggestions.If we were to make it it’s own command the design I have thought about is:
It feels a little extra to have it’s own command for this to me since the code for it would essentially need emulate getting to this part of the code like
add
. I’m open to any design but would love to start a discussion here about it because I’m serious about implementing this if a PR would be welcome.For anyone using this, if the type ‘DesignTimeServicesBuilder’ can not be found, make sure “Microsoft.EntityFrameworkCore.Design” is not marked as a private asset in your .csproj file. (It is by default)
@aboryczko Thanks btw!
Checking if a migration is missing in your PR pipeline seems like a no-brainer to me, hoping ef core will get native support for this.
My GitHub Action to ensure that no new migrations are needed
FYI, you can test for unapplied migrations today using grep:
Correct.
after starting to write a response, I think I’m understanding now; let me confirm: you’re saying there’s no need to connect to a specific database, because all the information needed to determine “is a new migration needed?” can be determined from code alone? that feels right, and I’ll test that now.
@BenMakesGames yes, you need to create the context. I use something more complex to get the context via IServiceProvider in a base test class, but what you have written should work fine. For a unit test this would be enough:
of course if you use a different Database Provider you should change
UseSqlServer()
to something else.