Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions DiplomaticMailBot.Tests.Integration/Tests/EfMigrationsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using DiplomaticMailBot.Tests.Integration.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

namespace DiplomaticMailBot.Tests.Integration.Tests;

[TestFixture]
public sealed class EfMigrationsTests
{
/* see
https://www.meziantou.net/detect-missing-migrations-in-entity-framework-core.htm
https://github.com/dotnet/efcore/issues/26348#issuecomment-1535156915
*/
[Test]
public async Task EnsureMigrationsAreUpToDate()
{
var dbConnectionString = string.Empty;
var dbContextFactory = new TestDbContextFactory(dbConnectionString);
await using var dbContext = dbContextFactory.CreateDbContext();

// Get required services from the dbcontext
var migrationModelDiffer = dbContext.GetService<IMigrationsModelDiffer>();
var migrationsAssembly = dbContext.GetService<IMigrationsAssembly>();
var modelRuntimeInitializer = dbContext.GetService<IModelRuntimeInitializer>();
var designTimeModel = dbContext.GetService<IDesignTimeModel>();

// Get current model
var model = designTimeModel.Model;

// Get the snapshot model and finalize it
var snapshotModel = migrationsAssembly.ModelSnapshot?.Model;
if (snapshotModel is IMutableModel mutableModel)
{
// Forces post-processing on the model such that it is ready for use by the runtime
snapshotModel = mutableModel.FinalizeModel();
}

if (snapshotModel is not null)
{
// Validates and initializes the given model with runtime dependencies
snapshotModel = modelRuntimeInitializer.Initialize(snapshotModel);
}

// Compute differences
var modelDifferences = migrationModelDiffer.GetDifferences(
source: snapshotModel?.GetRelationalModel(),
target: model.GetRelationalModel());

// The differences should be empty if the migrations are up-to-date
Assert.That(modelDifferences, Is.Empty);
}
}