rails-sqlserver / activerecord-sqlserver-adapter

SQL Server Adapter For Rails
MIT License
968 stars 558 forks source link

Fix matching view's real column name #1133

Closed aidanharan closed 8 months ago

aidanharan commented 8 months ago

Fix for issue https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues/1131

The regex to match a view column to table column name was too vague, which could cause a similarly named view column to be matched.

The issue was found in v7.0.5.0 (containing https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1126) but the issue was actually present in previous releases (eg: v7.0.4.0). The order that the view columns were declared affected whether the issue appeared or not.

For example if you had view declaration:

CREATE VIEW bug_tests AS SELECT id AS id, dt_field as dt, dt_fieldX as dt_x FROM bug_test_tables

And performed regex of CREATE\s+VIEW.*AS\s+SELECT.*\W([\w-]*)\s+AS\s+dt on it then you would get match:

  1. dt_fieldX

If you changed the view declaration so that dt_x is before dt:

CREATE VIEW bug_tests AS SELECT id AS id, dt_fieldX as dt_x, dt_field as dt FROM bug_test_tables

Then the same regex would match:

  1. dt_field

In the fixed I remove the CREATE VIEW ... AS SELECT ... clause to simplify the regex to match the column name followed by a non-word character.