Unit test for missing migrations

Viewed 158

Can you write an unit test that verifies that all migration scripts are generated?

I wanted to implement this so the CI build fails when someone forgets to Add-Migration. But the ef core in-memory DB does not allow any "relational-specific" stuff and the SqLite DB fails because "no such table: __EFMigrationsHistory", which seems to be a known issue that won't be fixed.

I was also looking into the generated snapshot and if there is any way to compare this to my current code-base, but did not find anything helpful.

This test should be lightweight and run without an real DB.

1 Answers

A kind of hackish solution would be comparing the Migrations/*Snapshot.cs before and after running dotnet ef migrations add <migration>.

I think this could work:

before="$(sha256sum Migrations/*Snapshot.cs | sha256sum)"
dotnet ef migrations add dummy
after="$(sha256sum Migrations/*Snapshot.cs | sha256sum)"
if [ "$before" != "$after" ]; then
    exit 1
fi
Related