-
Notifications
You must be signed in to change notification settings - Fork 12
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
Column aliases #27
Column aliases #27
Conversation
re code climate: that method has not been touched. ignoring hmm. I read the code climate wrong. it was the |
fa90532
to
d822af5
Compare
properly quote virtual attribute names we were passing a bogus value for a virtual attribute, which we will remove, but it still feels like this should be escaped
We have one use case where the arel is defined with an alias I'm not a big fan of this approach as it will break use of this column for where and order clauses, but will still work in select clauses
select changes a bit over time would be nice to get this monkey patch into arel_column instead arel_column is introduced in rails 6.0 (so we'd need to also patch arel_columns)
Checked commits kbrock/activerecord-virtual_attributes@33e966c~...8a34d40 with ruby 2.3.3, rubocop 0.69.0, haml-lint 0.20.0, and yamllint 1.10.0 |
@NickLaMuro can you 👀 the second commit... I'm not sure ... @kbrock what are we giving up by it not working for |
@jrafanie Yes, we really should not be using an We currently use an alias in miq's custom attributes. So I'm putting in the specs to test the actual code (and failure) that we have in miq. This PR quotes the column name, to fix the reason we had the alias in the first place. It also has a fix to allow aliases to work. In the future, I would like to remove the aliases from custom attributes. But that has been in our code base for a while, and I'm trying not to scope creep this too much - postponing that change for later |
Nick's 2cents™High level, I gave this response Keenan in a DM about this a few days ago:
And also linked to this image... But if I recall correctly, this was specifically being targeted in response to issue when trying to enable Which I think is something easier solved by fixing for US (MIQ) first, and deciding what to do here the #rightWay™ when it is understood better. Specifically:
Can this then not be fixed here and instead in MIQ for our specific use case? (see link above). If you yourself don't like this approach, I am having a hard time even starting to review this further. I think the first commit, however, is completely fine and I think makes sense to be merged in on it's own since I don't see it hurting anything. |
to clarify: I'm not a big fan of how we coded the alias in MIQ in the first place. (It was necessary at the time to avoid a bug. This bug was fixed in the first commit.
We have 2 options:
Looks like we both are leaning towards option 2? |
I want to keep this PR for prosperity case, oped #30 with just the first commit. Thanks |
Context
virtual attributes:
In
ActiveRecord
,build_select
(viaarel_columns
) convertsselect_values
into sql. But it does not properly detect a virtual attribute as a field. So it doesn't properly convert the field to arel (arel_attribute
) and it generated invalid sql.We monkey patched
select()
to detect a virtual attribute field, convert it to arel, and put the arel intoselect_values
. It now generates valid sql.ManageIQ/manageiq custom attributes:
The custom attributes mixin defines fields as virtual attributes. Unfortunately these attribute names are not always sql friendly. Since virtual attributes does not properly escape the field's sql aliases in the select clause and it generated invalid sql. #BUG1
The custom attribute mixin added a custom alias to the arel so it generated valid sql. YAY. But BUG1 still kinda exists.
Before
Our monkey patched
select()
skipped arel with an alias, so it put the custom attribute name directly intoselect_values
. This generated invalid sql. #BUG2In many cases,
build_select
calledarel_attribute
anyway, so BUG2 was hidden and all worked.Well, it worked until ManageIQ/manageiq#18822 introduced a custom
from
clause.build_select
no longer calledarel_attribute
for the custom attribute and it started generating invalid sql again.Also of note,
ActiveRecord
developers change the implementation ofbuild_select
a lot. Which makes sense since it never quite meets our needs either. But this also tells you that it would be dangerous to monkey patch it even though it feels like this is the right place to fix this problem.After
select_values
. #BUG2select()
to be code climate friendly.NOTE
Sql aliases do work in a
SELECT
clause but not in aWHERE
orORDER BY
clause.So if the attribute's arel contains an alias, it will work in a
SELECT
but not in aWHERE
orORDER BY
statement.To be honest, we should probably remove the custom alias for the custom attributes when defining the virtual attributes - but this PR is needed for that case as well.
We may want to look into modifying
build_select
and friendsarel_columns
.We may want to look into the attribute aliasing mechanism used by
includes().references()
.