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

Honor association views when wrapping #335

Merged
merged 1 commit into from Nov 2, 2019

Conversation

ianks
Copy link
Collaborator

@ianks ianks commented May 23, 2019

Previously, when wrapping a relation, the view for the association would not be honored. This would make the output of .wrap different from combine, and cause unexpected behavior.

This PR addresses that by first applying the view to the association target if there is one.

Also, since .wrap with prefix attributes, any previously aliased attributes would fail loudly since Sequel::SQL::AliasedExpression does not respond to .as. To fix this, we now will handle alias Sequel::SQL::AliasedExpression by constructing a new instance with the new alias name.

@ianks
Copy link
Collaborator Author

ianks commented Jul 8, 2019

Can i get a review on this?

Copy link
Member

@solnic solnic left a comment

Choose a reason for hiding this comment

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

Hey thanks for the PR <3 I left some comments

lib/rom/sql/relation/reading.rb Outdated Show resolved Hide resolved
spec/integration/wrap_spec.rb Outdated Show resolved Hide resolved
spec/integration/wrap_spec.rb Show resolved Hide resolved
expect(users[:id].as(:uid).as(:uuid).sql_literal(ds)).
to eql(%("users"."id" AS "uuid"))
end
end
Copy link
Member

Choose a reason for hiding this comment

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

It would be good to extract this into a separate PR. I think it's a good fix that can be shipped separately.

@@ -36,7 +36,7 @@ def self.[](*args)
# @api public
def aliased(alias_name)
super.with(name: name || alias_name).meta(
sql_expr: sql_expr.as(alias_name)
sql_expr: alias_sql_expr(sql_expr, alias_name)
Copy link
Member

Choose a reason for hiding this comment

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

it looks like we should have our own composable wrapper for aliased expressions, that case analysis in alias_sql_expr is leaking

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this will be challenging to do in this PR, i'm not sure exactly how to approach it since from what i can see, sql_expr is an actual Sequel::* class everywhere so it may have unexpected breakage. Can we postpone this refactoring?

@ianks
Copy link
Collaborator Author

ianks commented Oct 10, 2019

@flash-gordon I noticed a weird error when wrapping an aliased function. essentially, the attribute name has an extra prefix that is never unwrapped, so for the failing spec 27f4604, the wrapped key is users_extra_attribute rather than extra_attribute. Do you have any clues as to why this might happen? Stuck on it right now, but would like to get it fixed :)

@ianks
Copy link
Collaborator Author

ianks commented Oct 10, 2019

Fixed the issue with aliasing functions. But now I've discovered a new regression in 5.0, aliasing attributes in wrapped relations does not work. So working on that now.

@ianks
Copy link
Collaborator Author

ianks commented Oct 10, 2019

Alright, so I believe this PR is ready. It might not be the prettiest, but at least the behavior is correct now. With this patch, our (large) production app can now pass all specs on rom 5!

What this patch addresses

  • views are now honored in wrapped associations
  • attribute aliasing on wrapped attributes used the correct alias name
  • proper aliasing for functions with wrapped attributes

Excited to get this in so we do not have to run off a fork, and contribute upstream to bugfixes, features, etc.

  • Ian

lib/rom/sql/relation/reading.rb Outdated Show resolved Hide resolved
lib/rom/sql/associations/core.rb Show resolved Hide resolved
Copy link

@psparrow psparrow left a comment

Choose a reason for hiding this comment

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

Left a few minor comments. Looks good overall.

@ianks
Copy link
Collaborator Author

ianks commented Oct 15, 2019

PR comments have been addressed :)

@solnic
Copy link
Member

solnic commented Oct 22, 2019

@flash-gordon could you take a look as well, please?

Copy link
Member

@flash-gordon flash-gordon left a comment

Choose a reason for hiding this comment

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

It's good except for one place I pointed. The whole thing about aliased attributes is a solid candidate for refactoring in the next version. It's too error-prone and leaky now. Specifically, I don't like Sequel being mentioned. Instead of using case analysis via reflection we should always rely on duck typing.


def extract_wrapped_name(node)
_, _, meta_options = node
unwrapped_name = meta_options[:alias].to_s
Copy link
Member

Choose a reason for hiding this comment

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

in ruby 2.7, Symbol#to_s start returning frozen strings, this code will break

@ianks
Copy link
Collaborator Author

ianks commented Oct 31, 2019

@flash-gordon @solnic fixed the last issue. code climate seems to be failing for an unrelated reason. merge? 😄

@flash-gordon
Copy link
Member

@ianks nice, please squash the commits, I'll merge it shortly after

@ianks
Copy link
Collaborator Author

ianks commented Nov 1, 2019

@flash-gordon All squashed.

@flash-gordon flash-gordon merged commit 13a9b79 into rom-rb:master Nov 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants