Skip to content
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

Schema validation not working with NpgSql v5 #2876

Closed
MichalUssuri opened this issue Jul 26, 2021 · 6 comments · Fixed by #2992
Closed

Schema validation not working with NpgSql v5 #2876

MichalUssuri opened this issue Jul 26, 2021 · 6 comments · Fixed by #2992

Comments

@MichalUssuri
Copy link

MichalUssuri commented Jul 26, 2021

Database schema auto validation is not working after the update of NpgSql to version 5.0.0.
(Enabled e.g by: properties.SchemaAction = SchemaAutoAction.Validate;)
The validation always fails with errors like:

Wrong column type in test.systems for column ID. Found: **integer**, Expected **int4**; 
Wrong column type in test.systems for column Name. Found: **character varying**, Expected **varchar(30)**;
...

Exception stack:

Received unexpected error: Schema validation failed: see list of validation errors.
   at NHibernate.Cfg.Configuration.ValidateSchema(Dialect dialect, IDatabaseMetadata databaseMetadata)
   at NHibernate.Tool.hbm2ddl.SchemaValidator.Validate()
   at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
   at NHibernate.Cfg.Configuration.BuildSessionFactory()
   at NHibernate.NetCore.ServiceCollectionExtensions.<>c.<AddHibernate>b__5_1(IServiceProvider provider)

Problem description:
The code in Table.cs Table.ValidateColumns
https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Mapping/Table.cs
is comparing column types:

bool typesMatch = column.GetSqlType(dialect, mapping).StartsWith(columnInfo.TypeName, StringComparison.OrdinalIgnoreCase);

The columnInfo.TypeName is getting column type from NpgSql NpgsqlSchema.GetColumns
https://github.com/npgsql/npgsql/blob/main/src/Npgsql/NpgsqlSchema.cs
where the value is taken from data_type column from SQL query:

SELECT
    table_catalog, table_schema, table_name, column_name, ordinal_position, column_default, is_nullable,
    udt_name::regtype::text AS data_type, character_maximum_length, character_octet_length, numeric_precision,
    numeric_precision_radix, numeric_scale, datetime_precision, character_set_catalog, character_set_schema,
    character_set_name, collation_catalog
  FROM information_schema.columns");

Unfortunately in version 5.0.0, there were npgsql/npgsql#3012, which changed the returned value from udt_name AS data_type to udt_name::regtype::text AS data_type.
npgsql/npgsql@9036668#diff-9e5577d0cfb25bc8279488c960f8dc2427d580f90156386cda9d14953caafd1d
This change caused, that "user-facing type names" are now returned from the database.

Here are some example results returned from SQL query:

SELECT udt_name AS old_data_type, udt_name::regtype::text AS new_data_type
  FROM information_schema.columns
old_data_type	new_data_type
int8          	bigint
int4          	integer
char          	character
varchar       	character varying
bool          	boolean

Please, fix this issue.

@hazzik
Copy link
Member

hazzik commented Aug 3, 2021

Duplicate of #1895

@hazzik hazzik marked this as a duplicate of #1895 Aug 3, 2021
@hazzik hazzik closed this as completed Aug 3, 2021
@fredericDelaporte
Copy link
Member

For me it is not the same than #1895. Validation was mostly working with Npgsql prior to its v5, and #1895 is about some of the few cases it did not. But validation with Npgsql does mostly not work anymore from its v5 version, due to a large change in the retrieved types naming.

It may still be closed, but more for a reason like external issue, if such a driver behavior is not something NHibernate is meant to take into account.

@hazzik
Copy link
Member

hazzik commented Aug 5, 2021

@fredericDelaporte but the reason is exactly same. This way or another these tickets are duplicates. Maybe we should close the other one as a duplicate?

@fredericDelaporte
Copy link
Member

I doubt we will fix #1895 (with Npgsql < v5) if we only work on fixing #2876, and conversely, we will likely not fix #2876 if we only work on fixing #1895.

@hazzik
Copy link
Member

hazzik commented Jan 16, 2022

Ok, going to fix separately to #1895

@hazzik hazzik changed the title Schema validation not working with NpgSql v 5.0.0 Schema validation not working with NpgSql v5 Jan 16, 2022
@hazzik hazzik modified the milestones: next minor, 5.3.11 Jan 18, 2022
hazzik added a commit that referenced this issue Jan 23, 2022
@fredericDelaporte
Copy link
Member

Fixed by #2992.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants