Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ the work, and close it from the PR (`Closes #n`).
- **Literals — NEVER let an `IFormatProvider` reach the literal.** `Parse`/`TryParse` read the invariant literal and ignore the provider; formatting without a format string is the invariant literal whatever the provider (a provider applies only with an explicit format). Before 9.0.0, `th-TH` parsed `2026-01-01` as Gregorian 1483 and `de-DE` turned `$"{DecimalRange [1.5,2)}"` into `[1,5,2)` → `[1,52)`, silently. `LiteralProviderContractTests` and `LiteralCultureContractTests` sweep every type under five cultures
- **Literals — temporal element text is strict ISO 8601, NEVER a lenient BCL `Parse`.** `IsoTemporal` (BCL) and `NodaPatterns` (NodaTime) accept exactly what this library writes and what PostgreSQL prints in ISO `DateStyle` — space separator, `+05` / `+05:30` / `±HH:mm:ss` offsets, `infinity` / `-infinity` as the extremes — plus `HH:mm`. A `DateTime` offset is accepted and **never applied** — the wall clock is kept, as `Local`, which is PostgreSQL's `timestamp` input rule; converting it made one literal a different `tsrange` value on every machine. Before 9.0.0 `DateOnly.Parse(invariant)` read `[01.02.2027,05.03.2027)` month first and an offset-less `DateTimeOffset` took the machine's offset, silently. A wrapper arity parses as its primitive first and hands the element its round-trip form, and a closed temporal set reads its JSON elements through the same grammar whenever System.Text.Json's built-in converter would have (a registered converter still wins). `TemporalLiteralStrictnessContractTests` refuses a non-ISO element through every entry point of every discovered temporal type
- **EF Core**: new range types are wired exclusively through `RangeTypeRegistry.Register` (satellites call it from their options-builder extension); never bypass the registry
- **EF Core — NEVER replace, register or derive from `IMigrationsModelDiffer` or `IMigrationsSqlGenerator`.** EF resolves each as a single service, so when two packages replace one the last registration wins and the other's migration DDL disappears without an error. From 11.0.0 on, `CodoMetis.EFCore.SchemaKit` owns both in the CodoMetis family: a migration-aware feature goes into SchemaKit or plugs into its contract, never into a replacement here. The custom store types (`timerange`, `float8range`, `float4range`) are created through Npgsql's `HasPostgresRange`, which needs neither. `MigrationServiceOwnershipConventionTests` fails on a shipping type implementing either service and on any reference to them in code under `src/` (`ReplaceService`, `TryAdd`, `ServiceDescriptor`, a base list); comments may name them
- **EF Core — a temporal value written as text MUST be the value Npgsql would bind.** A JSON document and a SQL literal hold the microsecond truncated toward 2000-01-01 (`StoreTemporal` / `NodaStoreTemporal`, passed as a definition's `storeValue` — never folded into `normalizeValue`, which must stay the core's `NormalizeElement`) and spell the extremes Npgsql maps as `-infinity` / `infinity`; ranges are rebuilt through their factory afterwards and sets deduplicated. PostgreSQL's text input rounds half-to-even, so seven fraction digits make a document or constant a different value from the column. The JSON writer resolves its store text through the registries (`StoreText<T>`), never from state a definition's constructor set — a compiled model may never build a wrapper family's definition. `TemporalStoreParityTests` (integration) is the authority
- **EF Core — an inlined range constant arrives typed as its sealed variant.** EF evaluates a range built from query-independent values on the client and inlines it as `Constant(value, Int32Range.Finite)`, for which no mapping exists; `RangeVariantConstantRetyper` in the query interceptor retypes it to the union. Do not "fix" this by mapping the variant types — that would make variant-typed properties mappable
- **EF Core — a constant element operand must be typed.** The range operators are polymorphic (`anyrange @> anyelement`) and PostgreSQL resolves those without implicit coercions, so a bare numeric literal (which it types as `integer`) does not match `int8range`. Assert the whole operand in translation tests, never the `@> ` prefix, and execute at least one constant-operand query per element type against a live server
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.EntityFrameworkCore.Migrations;

namespace CodoMetis.ValueRanges.Conventions.Tests;

/// <summary>
/// No shipping package replaces EF Core's model differ or migrations SQL generator (#334).
/// </summary>
/// <remarks>
/// <para>
/// EF Core resolves <see cref="IMigrationsModelDiffer"/> and <see cref="IMigrationsSqlGenerator"/>
/// as single services, so when two packages replace one of them the last registration wins and the
/// other package's migration DDL disappears without an error. From 11.0.0 on,
/// <c>CodoMetis.EFCore.SchemaKit</c> is the one owner of both in the CodoMetis family: a
/// migration-aware feature goes into SchemaKit or plugs into its contract, never into a replacement
/// here. The custom store types (<c>timerange</c>, <c>float8range</c>, <c>float4range</c>) are
/// created through Npgsql's <c>HasPostgresRange</c>, not through a differ of ours.
/// </para>
/// <para>
/// Two checks, because either alone has a way around it: the compiled assemblies are searched for a
/// type that implements or derives from either service, and the sources under <c>src/</c> for any
/// reference to them in code — a <c>ReplaceService</c>, a <c>TryAdd</c>, a <c>ServiceDescriptor</c>
/// over <c>typeof(…)</c>, a base list. Comments and XML docs are not code and may name them.
/// </para>
/// <para>
/// Everything is discovered — projects by globbing <c>src/</c> — so a new EF satellite is covered
/// with no edit here.
/// </para>
/// </remarks>
[TestClass]
public sealed class MigrationServiceOwnershipConventionTests
{
private static readonly Type[] OwnedServices = [typeof(IMigrationsModelDiffer), typeof(IMigrationsSqlGenerator)];

/// <summary>
/// The names a reference to either service is spelled with: the interfaces, EF's base
/// implementations a replacement derives from, and Npgsql's generator.
/// </summary>
private static readonly string[] OwnedNames =
[
"IMigrationsModelDiffer", "MigrationsModelDiffer",
"IMigrationsSqlGenerator", "MigrationsSqlGenerator", "NpgsqlMigrationsSqlGenerator"
];

[TestMethod]
public void NoShippingType_ImplementsTheModelDifferOrTheMigrationsSqlGenerator()
{
var efAssemblies = RepoLayout.PackableProjects
.Select(LoadAssembly)
.Where(assembly => assembly.GetReferencedAssemblies()
.Any(reference => reference.Name == "Microsoft.EntityFrameworkCore.Relational"))
.ToList();

// A sweep over no assembly passes for the wrong reason: if the EF packages stop being found,
// this says so instead of going green.
Assert.IsTrue(efAssemblies.Count >= 2,
$"Expected at least the two shipping EF assemblies, found {efAssemblies.Count}: "
+ string.Join(", ", efAssemblies.Select(assembly => assembly.GetName().Name)));

var violations = efAssemblies
.SelectMany(assembly => LoadableTypes(assembly).Select(type => (assembly, type)))
.SelectMany(pair => OwnedServices.Where(service => service.IsAssignableFrom(pair.type))
.Select(service => $"{pair.assembly.GetName().Name}: {pair.type.FullName} "
+ $"implements {service.Name}"))
.ToList();

Assert.AreEqual(0, violations.Count,
"The model differ and the migrations SQL generator belong to CodoMetis.EFCore.SchemaKit; "
+ "a migration-aware feature goes into SchemaKit or plugs into its contract:"
+ Environment.NewLine + string.Join(Environment.NewLine, violations));
}

[TestMethod]
public void NoShippingSource_ReplacesOrRegistersTheModelDifferOrTheMigrationsSqlGenerator()
{
var violations = new List<string>();
int files = 0;
int registrations = 0;

foreach (var file in SourceFiles())
{
files++;
var root = CSharpSyntaxTree.ParseText(File.ReadAllText(file.FullName)).GetRoot();

registrations += root.DescendantNodes().OfType<GenericNameSyntax>()
.Count(name => name.Identifier.Text is "TryAdd" or "ReplaceService");

// DescendantNodes does not descend into trivia, so comments and XML docs are skipped.
violations.AddRange(root.DescendantNodes().OfType<SimpleNameSyntax>()
.Where(name => OwnedNames.Contains(name.Identifier.Text, StringComparer.Ordinal))
.Select(name => $"{Relative(file)}:{LineOf(name)} — {name.Identifier.Text} in `{LineText(name)}`"));

// A service named by string (Type.GetType, a descriptor read from configuration) is the
// same registration with the type checker taken out.
violations.AddRange(root.DescendantTokens()
.Where(token => token.IsKind(SyntaxKind.StringLiteralToken))
.Where(token => OwnedNames.Any(owned => token.ValueText.Contains(owned, StringComparison.Ordinal)))
.Select(token => $"{Relative(file)}:{LineOf(token.Parent!)} — names `{token.ValueText}` in a string"));
}

// The scan must be reading the files that register services, or it proves nothing.
Assert.IsTrue(files > 0 && registrations > 0,
$"Scanned {files} source files under src/ and found {registrations} TryAdd/ReplaceService calls; "
+ "expected the EF package's service registrations. Has the registration moved, or src/?");

Assert.AreEqual(0, violations.Count,
"The model differ and the migrations SQL generator belong to CodoMetis.EFCore.SchemaKit; "
+ "no package here may replace or register them:"
+ Environment.NewLine + string.Join(Environment.NewLine, violations));
}

private static Assembly LoadAssembly(PackableProject project)
{
var name = project.Property("AssemblyName") ?? Path.GetFileNameWithoutExtension(project.File.Name);

try
{
return Assembly.Load(name);
}
catch (FileNotFoundException exception)
{
throw new AssertFailedException(
$"Could not load '{name}', a shipping project under src/. Add a ProjectReference to it in "
+ "CodoMetis.ValueRanges.Conventions.Tests.csproj, which references every shipping project.", exception);
}
}

private static IEnumerable<Type> LoadableTypes(Assembly assembly)
{
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException exception)
{
return exception.Types.OfType<Type>();
}
}

private static IEnumerable<FileInfo> SourceFiles() =>
RepoLayout.PackableProjects
.SelectMany(project => project.Directory.EnumerateFiles("*.cs", SearchOption.AllDirectories)
.Where(file => !IsBuildOutput(project, file)))
.OrderBy(file => file.FullName, StringComparer.Ordinal);

// Generated sources under obj/ are the compiler's, not the package author's.
private static bool IsBuildOutput(PackableProject project, FileInfo file) =>
Path.GetRelativePath(project.Directory.FullName, file.FullName)
.Split(Path.DirectorySeparatorChar)[0] is "obj" or "bin";

private static string LineText(SyntaxNode node) =>
node.SyntaxTree.GetText().Lines[LineOf(node) - 1].ToString().Trim();

private static int LineOf(SyntaxNode node) =>
node.GetLocation().GetLineSpan().StartLinePosition.Line + 1;

private static string Relative(FileInfo file) =>
Path.GetRelativePath(RepoLayout.Root.FullName, file.FullName);
}