Skip to content

Commit f5a2f4d

Browse files
committed
✨ feat(evolution): answer for drift against the database the model runs on
Entity Framework cannot answer this for itself. Its history table records which migrations ran; has-pending-model-changes compares the model to the migrations. Neither looks at the schema, so neither reports a column altered by hand on staging, a migration that stopped halfway, or an environment restored from a backup older than the last release. Only looking answers those. AddDriftCheck<TContext>() registers an IDatabaseSnapshotSource over the context. It describes what the model says the tables should be — straight from EF's own metadata, GetColumnType() included — and what the database actually has, read from the catalogue. Both sides land in the provider's own store-type vocabulary, which is the whole reason a healthy schema compares silently: EF resolves character varying(50) and PostgreSQL reports character varying(50). PostgreSQL, MySQL and SQL Server are covered. Any other provider says it cannot answer rather than answering wrongly. Generating migrations is deliberately not here. Entity Framework already does that, from the same model, and better than a second generator could; adding a worse one beside it would be a disservice. What this adds is the part it leaves out. Depends on eQuantic.Core.Data.Abstractions 6.9.0, which moved DriftComparer and DriftReport into the contracts — before that, a provider could describe both sides of a check without the engine and still not compare them. Verified against a real PostgreSQL through a new Testcontainers suite, the repository's first: a schema Entity Framework itself created reports nothing, and a column dropped, narrowed, or made optional by hand, a dropped table, and a column nobody mapped are each found by changing the database out from under the model. 47 tests across the five suites; the new one is in the CI matrix.
1 parent e159496 commit f5a2f4d

22 files changed

Lines changed: 531 additions & 58 deletions

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ jobs:
6060
- tests/eQuantic.Core.Data.EntityFramework.SqlServer.Tests/eQuantic.Core.Data.EntityFramework.SqlServer.Tests.csproj
6161
- tests/eQuantic.Core.Data.EntityFramework.MongoDb.Tests/eQuantic.Core.Data.EntityFramework.MongoDb.Tests.csproj
6262
- tests/eQuantic.Core.Data.EntityFramework.CosmosDb.Tests/eQuantic.Core.Data.EntityFramework.CosmosDb.Tests.csproj
63+
- tests/eQuantic.Core.Data.EntityFramework.PostgreSql.Tests/eQuantic.Core.Data.EntityFramework.PostgreSql.Tests.csproj
6364
steps:
6465
- uses: actions/checkout@v4
6566
- uses: actions/setup-dotnet@v4

‎.github/workflows/release.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ jobs:
7171
- tests/eQuantic.Core.Data.EntityFramework.SqlServer.Tests/eQuantic.Core.Data.EntityFramework.SqlServer.Tests.csproj
7272
- tests/eQuantic.Core.Data.EntityFramework.MongoDb.Tests/eQuantic.Core.Data.EntityFramework.MongoDb.Tests.csproj
7373
- tests/eQuantic.Core.Data.EntityFramework.CosmosDb.Tests/eQuantic.Core.Data.EntityFramework.CosmosDb.Tests.csproj
74+
- tests/eQuantic.Core.Data.EntityFramework.PostgreSql.Tests/eQuantic.Core.Data.EntityFramework.PostgreSql.Tests.csproj
7475
steps:
7576
- uses: actions/checkout@v4
7677
- uses: actions/setup-dotnet@v4

‎README.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,43 @@ runtime is published as its **own package major** so the EF Core lines never mix
165165
> must not be read as a .NET version. You normally consume only the provider package for your runtime
166166
> (8.x / 10.x), which pulls the right shared assemblies transitively.
167167
168+
## Checking the database against your model
169+
170+
Entity Framework records which migrations *ran*, and `has-pending-model-changes` compares the model to
171+
the migrations. Neither looks at the schema — so neither can tell you that a column was altered by hand
172+
on staging, that a migration stopped halfway, or that an environment was restored from a backup older
173+
than the last release. **Only looking answers those.**
174+
175+
```csharp
176+
services.AddDbContext<ShopContext>(options => options.UseNpgsql(connectionString));
177+
services.AddDriftCheck<ShopContext>();
178+
```
179+
180+
That registers `IDatabaseSnapshotSource`, which describes both sides of the comparison — what the model
181+
says the tables should be, and what the database actually has — in the provider's own store-type
182+
vocabulary, so a healthy schema compares silently:
183+
184+
```csharp
185+
var source = provider.GetRequiredService<IDatabaseSnapshotSource>();
186+
var report = DriftComparer.Compare(source.Expect(), await source.ObserveAsync());
187+
188+
if (report.Breaks)
189+
{
190+
// something the application reads on every query is not there, or not as it expects
191+
}
192+
```
193+
194+
`report.Breaks` separates the differences that stop the application from the ones that do not: a column
195+
the model does not map belongs to somebody else — databases get shared — and tables you do not map are
196+
not read at all. The command-line tool [`eqdata drift`](https://github.com/eQuantic/core-data/blob/master/docs/guides/migrations/tooling.md)
197+
runs the same check and exits non-zero on the first kind, which makes it a deployment gate.
198+
199+
Reading the catalogue is provider-specific: PostgreSQL, MySQL and SQL Server are covered. Any other
200+
provider says it cannot answer rather than answering wrongly.
201+
202+
**Generating migrations is deliberately absent.** Entity Framework already does that, from the same
203+
model, and better than a second generator could. What this adds is the part it leaves out.
204+
168205
## Learn more
169206

170207
- [Repository Pattern walkthrough](Repository.md) — data entities, unit of work, repository and

‎eQuantic.Core.Data.EntityFramework.sln‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ VisualStudioVersion = 15.0.26430.6
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{0CA2F610-6D57-4D1F-92E3-EDCCEDAD8297}"
77
EndProject
8-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F72DED29-7BF7-4870-81B4-7AFE91270530}"
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "root", "root", "{F72DED29-7BF7-4870-81B4-7AFE91270530}"
99
ProjectSection(SolutionItems) = preProject
1010
.gitignore = .gitignore
1111
LICENSE = LICENSE
1212
README.md = README.md
1313
Repository.md = Repository.md
14-
.github\workflows\dotnetcore.yml = .github\workflows\dotnetcore.yml
1514
EndProjectSection
1615
EndProject
1716
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SqlServer", "SqlServer", "{5B80C186-5E75-413B-B825-0B5B2FE9A3E4}"
@@ -66,6 +65,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "eQuantic.Core.Data.EntityFr
6665
EndProject
6766
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "eQuantic.Core.Data.EntityFramework.CosmosDb.Tests", "tests\eQuantic.Core.Data.EntityFramework.CosmosDb.Tests\eQuantic.Core.Data.EntityFramework.CosmosDb.Tests.csproj", "{59E690D6-CE93-423B-9B56-4C28B1A5359D}"
6867
EndProject
68+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{5518B209-EAD5-436D-85C4-215E29C0F4E6}"
69+
ProjectSection(SolutionItems) = preProject
70+
.github\workflows\release.yml = .github\workflows\release.yml
71+
.github\workflows\ci.yml = .github\workflows\ci.yml
72+
EndProjectSection
73+
EndProject
6974
Global
7075
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7176
Debug|Any CPU = Debug|Any CPU
@@ -322,6 +327,7 @@ Global
322327
{709CD1F1-48C2-40C3-8470-9111FB2E0C86} = {0CA2F610-6D57-4D1F-92E3-EDCCEDAD8297}
323328
{29C8E041-4A5E-4D97-B1F2-EF9C31A951EC} = {9B074A5E-5D97-BD47-3CDB-A58654D0504C}
324329
{59E690D6-CE93-423B-9B56-4C28B1A5359D} = {EE71F02F-413D-4BC0-832F-726B6D8C0AD7}
330+
{5518B209-EAD5-436D-85C4-215E29C0F4E6} = {F72DED29-7BF7-4870-81B4-7AFE91270530}
325331
EndGlobalSection
326332
GlobalSection(ExtensibilityGlobals) = postSolution
327333
SolutionGuid = {83FECDD1-8A97-40B1-8529-3D5216E674C3}

‎src/eQuantic.Core.Data.EntityFramework.CosmosDb/eQuantic.Core.Data.EntityFramework.CosmosDb.Net10.csproj‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44
<PropertyGroup>
55
<Description>Core Data library for Entity Framework and Azure Cosmos DB</Description>
66
<AssemblyTitle>eQuantic.Core.Data.EntityFramework.CosmosDb</AssemblyTitle>
7-
<Version>10.4.0.0</Version>
7+
<Version>10.5.0.0</Version>
88
<TargetFramework>net10.0</TargetFramework>
99
<AssemblyName>eQuantic.Core.Data.EntityFramework.CosmosDb</AssemblyName>
1010
<PackageId>eQuantic.Core.Data.EntityFramework.CosmosDb</PackageId>
1111
<PackageTags>eQuantic;Core;Data;Library;Repository;Pattern;CosmosDB;Azure</PackageTags>
1212
<PackageReleaseNotes>Entity ignorant persistance with Repository Pattern for Entity
1313
Framework</PackageReleaseNotes>
1414

15-
<AssemblyVersion>10.4.0.0</AssemblyVersion>
16-
<FileVersion>10.4.0.0</FileVersion>
15+
<AssemblyVersion>10.5.0.0</AssemblyVersion>
16+
<FileVersion>10.5.0.0</FileVersion>
1717

1818
<ImplicitUsings>enable</ImplicitUsings>
1919
<Nullable>enable</Nullable>
2020
</PropertyGroup>
2121

2222
<ItemGroup>
2323
<PackageReference Include="eQuantic.Core" Version="1.8.4" />
24-
<PackageReference Include="eQuantic.Core.Data.Abstractions" Version="6.7.0" />
24+
<PackageReference Include="eQuantic.Core.Data.Abstractions" Version="6.9.0" />
2525
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.10" />
2626
<PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="10.0.10" />
2727
</ItemGroup>

‎src/eQuantic.Core.Data.EntityFramework.CosmosDb/eQuantic.Core.Data.EntityFramework.CosmosDb.Net8.csproj‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44
<PropertyGroup>
55
<Description>Core Data library for Entity Framework and Azure Cosmos DB</Description>
66
<AssemblyTitle>eQuantic.Core.Data.EntityFramework.CosmosDb</AssemblyTitle>
7-
<Version>8.5.0.0</Version>
7+
<Version>8.6.0.0</Version>
88
<TargetFramework>net8.0</TargetFramework>
99
<AssemblyName>eQuantic.Core.Data.EntityFramework.CosmosDb</AssemblyName>
1010
<PackageId>eQuantic.Core.Data.EntityFramework.CosmosDb</PackageId>
1111
<PackageTags>eQuantic;Core;Data;Library;Repository;Pattern;CosmosDB;Azure</PackageTags>
1212
<PackageReleaseNotes>Entity ignorant persistance with Repository Pattern for Entity
1313
Framework</PackageReleaseNotes>
1414

15-
<AssemblyVersion>8.5.0.0</AssemblyVersion>
16-
<FileVersion>8.5.0.0</FileVersion>
15+
<AssemblyVersion>8.6.0.0</AssemblyVersion>
16+
<FileVersion>8.6.0.0</FileVersion>
1717

1818
<ImplicitUsings>enable</ImplicitUsings>
1919
<Nullable>enable</Nullable>
2020
</PropertyGroup>
2121

2222
<ItemGroup>
2323
<PackageReference Include="eQuantic.Core" Version="1.8.4" />
24-
<PackageReference Include="eQuantic.Core.Data.Abstractions" Version="6.7.0" />
24+
<PackageReference Include="eQuantic.Core.Data.Abstractions" Version="6.9.0" />
2525
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.29" />
2626
<PackageReference Include="Microsoft.EntityFrameworkCore.Cosmos" Version="8.0.29" />
2727
</ItemGroup>

‎src/eQuantic.Core.Data.EntityFramework.MongoDb/eQuantic.Core.Data.EntityFramework.MongoDb.Net10.csproj‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44
<PropertyGroup>
55
<Description>Core Data library for Entity Framework and Mongo DB</Description>
66
<AssemblyTitle>eQuantic.Core.Data.EntityFramework.MongoDb</AssemblyTitle>
7-
<Version>10.4.0.0</Version>
7+
<Version>10.5.0.0</Version>
88
<TargetFramework>net10.0</TargetFramework>
99
<AssemblyName>eQuantic.Core.Data.EntityFramework.MongoDb</AssemblyName>
1010
<PackageId>eQuantic.Core.Data.EntityFramework.MongoDb</PackageId>
1111
<PackageTags>eQuantic;Core;Data;Library;Repository;Pattern;MongoDB</PackageTags>
1212
<PackageReleaseNotes>Entity ignorant persistance with Repository Pattern for Entity
1313
Framework</PackageReleaseNotes>
1414

15-
<AssemblyVersion>10.4.0.0</AssemblyVersion>
16-
<FileVersion>10.4.0.0</FileVersion>
15+
<AssemblyVersion>10.5.0.0</AssemblyVersion>
16+
<FileVersion>10.5.0.0</FileVersion>
1717

1818
<ImplicitUsings>enable</ImplicitUsings>
1919
<Nullable>enable</Nullable>
2020
</PropertyGroup>
2121

2222
<ItemGroup>
2323
<PackageReference Include="eQuantic.Core" Version="1.8.4" />
24-
<PackageReference Include="eQuantic.Core.Data.Abstractions" Version="6.7.0" />
24+
<PackageReference Include="eQuantic.Core.Data.Abstractions" Version="6.9.0" />
2525
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.10" />
2626
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.10">
2727
<PrivateAssets>all</PrivateAssets>

‎src/eQuantic.Core.Data.EntityFramework.MongoDb/eQuantic.Core.Data.EntityFramework.MongoDb.Net8.csproj‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44
<PropertyGroup>
55
<Description>Core Data library for Entity Framework and Mongo DB</Description>
66
<AssemblyTitle>eQuantic.Core.Data.EntityFramework.MongoDb</AssemblyTitle>
7-
<Version>8.5.0.0</Version>
7+
<Version>8.6.0.0</Version>
88
<TargetFramework>net8.0</TargetFramework>
99
<AssemblyName>eQuantic.Core.Data.EntityFramework.MongoDb</AssemblyName>
1010
<PackageId>eQuantic.Core.Data.EntityFramework.MongoDb</PackageId>
1111
<PackageTags>eQuantic;Core;Data;Library;Repository;Pattern;MongoDB</PackageTags>
1212
<PackageReleaseNotes>Entity ignorant persistance with Repository Pattern for Entity
1313
Framework</PackageReleaseNotes>
1414

15-
<AssemblyVersion>8.5.0.0</AssemblyVersion>
16-
<FileVersion>8.5.0.0</FileVersion>
15+
<AssemblyVersion>8.6.0.0</AssemblyVersion>
16+
<FileVersion>8.6.0.0</FileVersion>
1717

1818
<ImplicitUsings>enable</ImplicitUsings>
1919
<Nullable>enable</Nullable>
2020
</PropertyGroup>
2121

2222
<ItemGroup>
2323
<PackageReference Include="eQuantic.Core" Version="1.8.4" />
24-
<PackageReference Include="eQuantic.Core.Data.Abstractions" Version="6.7.0" />
24+
<PackageReference Include="eQuantic.Core.Data.Abstractions" Version="6.9.0" />
2525
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.29" />
2626
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.29">
2727
<PrivateAssets>all</PrivateAssets>

‎src/eQuantic.Core.Data.EntityFramework.MySql/eQuantic.Core.Data.EntityFramework.MySql.Net10.csproj‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
<PropertyGroup>
55
<Description>Core Data library for Entity Framework and MySQL</Description>
66
<AssemblyTitle>eQuantic.Core.Data.EntityFramework.MySql</AssemblyTitle>
7-
<Version>10.3.0.0</Version>
7+
<Version>10.4.0.0</Version>
88
<TargetFramework>net10.0</TargetFramework>
99
<AssemblyName>eQuantic.Core.Data.EntityFramework.MySql</AssemblyName>
1010
<PackageId>eQuantic.Core.Data.EntityFramework.MySql</PackageId>
1111
<PackageTags>eQuantic;Core;Data;Library;Repository;Pattern;SQL</PackageTags>
1212
<PackageReleaseNotes>Entity ignorant persistance with Repository Pattern for Entity
1313
Framework</PackageReleaseNotes>
1414

15-
<AssemblyVersion>10.3.0.0</AssemblyVersion>
16-
<FileVersion>10.3.0.0</FileVersion>
15+
<AssemblyVersion>10.4.0.0</AssemblyVersion>
16+
<FileVersion>10.4.0.0</FileVersion>
1717
</PropertyGroup>
1818

1919
<ItemGroup>
2020
<PackageReference Include="eQuantic.Core" Version="1.8.4" />
21-
<PackageReference Include="eQuantic.Core.Data.Abstractions" Version="6.7.0" />
21+
<PackageReference Include="eQuantic.Core.Data.Abstractions" Version="6.9.0" />
2222
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.10" />
2323
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.10">
2424
<PrivateAssets>all</PrivateAssets>

‎src/eQuantic.Core.Data.EntityFramework.MySql/eQuantic.Core.Data.EntityFramework.MySql.Net8.csproj‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
<PropertyGroup>
55
<Description>Core Data library for Entity Framework and MySQL</Description>
66
<AssemblyTitle>eQuantic.Core.Data.EntityFramework.MySql</AssemblyTitle>
7-
<Version>8.4.0.0</Version>
7+
<Version>8.5.0.0</Version>
88
<TargetFramework>net8.0</TargetFramework>
99
<AssemblyName>eQuantic.Core.Data.EntityFramework.MySql</AssemblyName>
1010
<PackageId>eQuantic.Core.Data.EntityFramework.MySql</PackageId>
1111
<PackageTags>eQuantic;Core;Data;Library;Repository;Pattern;SQL</PackageTags>
1212
<PackageReleaseNotes>Entity ignorant persistance with Repository Pattern for Entity
1313
Framework</PackageReleaseNotes>
1414

15-
<AssemblyVersion>8.4.0.0</AssemblyVersion>
16-
<FileVersion>8.4.0.0</FileVersion>
15+
<AssemblyVersion>8.5.0.0</AssemblyVersion>
16+
<FileVersion>8.5.0.0</FileVersion>
1717
</PropertyGroup>
1818

1919
<ItemGroup>
2020
<PackageReference Include="eQuantic.Core" Version="1.8.4" />
21-
<PackageReference Include="eQuantic.Core.Data.Abstractions" Version="6.7.0" />
21+
<PackageReference Include="eQuantic.Core.Data.Abstractions" Version="6.9.0" />
2222
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.29" />
2323
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.29">
2424
<PrivateAssets>all</PrivateAssets>

0 commit comments

Comments
 (0)