Skip to content

Fix schema validator for Npgsql 5 #2992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2022
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
2 changes: 1 addition & 1 deletion src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
<PackageReference Include="FirebirdSql.Data.FirebirdClient" Version="6.6.0" />
<PackageReference Include="Npgsql" Version="4.0.3" />
<PackageReference Include="Npgsql" Version="5.0.11" />
</ItemGroup>
<ItemGroup Condition="$(NhNetFx)">
<Reference Include="System.Configuration" />
Expand Down
38 changes: 37 additions & 1 deletion src/NHibernate/Dialect/Schema/PostgreSQLMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,43 @@ public PostgreSQLColumnMetadata(DataRow rs)
this.SetNumericalPrecision(rs["NUMERIC_PRECISION"]);

Nullable = Convert.ToString(rs["IS_NULLABLE"]);
TypeName = Convert.ToString(rs["DATA_TYPE"]);
TypeName = NormalizeTypeNames(Convert.ToString(rs["DATA_TYPE"]));
}

private static string NormalizeTypeNames(string typeName)
{
switch (typeName)
{
case "double precision":
return "float8";
case "real":
return "float4";
case "smallint":
return "int2";
case "integer":
return "int4";
case "bigint":
return "int8";
}
Comment on lines +146 to +158
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can see the naming changes in Npgsql change commit, or infer it from PostgreSQL documentation.

It looks like we were already having an issue with older Npgsql drivers, because we were using the complete name, boolean, while older drivers were previously supposed to yield the short name, bool. That trouble would be fixed since we do not translate it to its short name here.

But then we keep an inconsistency: the dialect and now the metadata are using PostgreSQL type short aliases for most types excepted for the boolean type.

For the same reasons than in this comment, normalizing this would be breaking. So, let it stay that way.


if (typeName.StartsWith("character", StringComparison.Ordinal))
{
return typeName.Replace("character varying", "varchar").Replace("character", "char");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is likely we are fixing another old trouble by the way here: for fixed length character types, the older Npgsql drivers could yield bpchar, while our dialects are using char.

}

if (typeName.EndsWith(" with time zone", StringComparison.Ordinal))
{
return typeName.StartsWith("timestamp", StringComparison.Ordinal)
? typeName.Replace(" with time zone", string.Empty).Replace("timestamp", "timestamptz")
: typeName.Replace(" with time zone", string.Empty).Replace("time", "timetz");
}

if (typeName.EndsWith(" without time zone", StringComparison.Ordinal))
{
return typeName.Replace(" without time zone", string.Empty);
}

return typeName;
}
}

Expand Down