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

Rails 5.2 support #67

Merged
merged 1 commit into from
May 4, 2020
Merged

Rails 5.2 support #67

merged 1 commit into from
May 4, 2020

Conversation

pat
Copy link
Contributor

@pat pat commented Nov 5, 2018

This is built on top of #66, but I'm just going to focus here on what's been done in 169f021 (and further commits after that, should there be changes):

  • Instead of manually crafting dirty state support, I'm opting to use Rails' own approaches for this, which arguably makes the behaviour more reliable and consistent - even though there's now some allowances of slightly different behaviour across versions.
  • The catch is that these helpers don't exist in Rails 4.1. Maybe this is a good opportunity to end 4.1 support?
  • I've left in references to model instance variables (e.g. @ssn), given they're covered by specs - though I personally would prefer to scrap that support, which makes the code a little more simpler. They're ideally not part of any public API, I'd hope?
  • The spec change is purely because the dirty state methods are lazily added to models, and so initially not available via klass.instance_methods. klass cannot be instantiated (no table exists) hence switching to use Person instead.

@kjakub
Copy link

kjakub commented Feb 11, 2019

@pat this is great work, why is not merged yet ? I have also forked vault-rails repo with your changes and bringing up following, you might be interested to see #70

Copy link
Contributor

@justincampbell justincampbell left a comment

Choose a reason for hiding this comment

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

This looks pretty good! It needs to be rebased, and I have some minor style suggestions.

I agree on dropping support for 4.1.

lib/vault/encrypted_model.rb Outdated Show resolved Hide resolved
lib/vault/encrypted_model.rb Show resolved Hide resolved
lib/vault/encrypted_model.rb Outdated Show resolved Hide resolved
lib/vault/encrypted_model.rb Show resolved Hide resolved
All the instance variable wrangling has been kept for compatibility’s sake (given they’re in the specs).

Instead of hand-crafting dirty support, this commit makes use of Rails’ own approach, though it does differ slightly between versions.
@pat
Copy link
Contributor Author

pat commented May 19, 2019

Thanks for the feedback Justin - just made most of the changes you requested (and provided a reason for the one I didn't). If there's any further tweaks required, do let me know!

@justincampbell
Copy link
Contributor

@madding Thanks! I wasn't able to allocate time to get Rails 5.2 support into the 0.5.0 release, but I hope to find time to review this and get another release out soon. Thanks for your patience!

@@ -267,12 +252,16 @@ def __vault_persist_attribute!(attribute, options)

# Only persist changed attributes to minimize requests - this helps
# minimize the number of requests to Vault.
if !changed.include?("#{attribute}")
return
if ActiveRecord.gem_version >= Gem::Version.new("5.2")
Copy link
Contributor

Choose a reason for hiding this comment

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

I thinks it would be a good idea to use Rails previous_changes here.

previous_changes exists from Rails 3.0 and it works on the latest Rails 6(master) also.

previous_changes_include? is dropped in Rails 5.2.3

Something like this:

previous_changes.include?(attribute)

Copy link

Choose a reason for hiding this comment

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

Method https://github.com/rails/rails/blob/6-0-stable/activemodel/lib/active_model/dirty.rb#L227 based on changes, but changes didn't work for attributes like this.

Copy link
Contributor

@dixpac dixpac Aug 8, 2019

Choose a reason for hiding this comment

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

They behave differently, changes returns hash of changes before save, previous_changes after it's saved.

person = Person.first
person.name = "New name"
person.changes  #=> {"name"=>["Old name", "New name"]}
person.previous_changes #=> {}

person.save

person.changes #=> {}
person.previous_changes  #=> {"name"=>["Old name", "New name"],...}

Copy link
Contributor

@dixpac dixpac Aug 8, 2019

Choose a reason for hiding this comment

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

I've just pull this PR and test it against rails master. With previous_changes_include?(dropped method) it doesn't work, with previous_changes works as expected

Copy link

@madding madding Aug 8, 2019

Choose a reason for hiding this comment

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

This is not my pull request, I have other one:
https://github.com/hashicorp/vault-rails/pull/76/files

I try show previous_changes here https://github.com/hashicorp/vault-rails/pull/76/files#diff-ba19de511d4dbf96b22d51bf1ecbf448R268 and it was empty when changed_attributes present.

May be it specific only for my PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dixpac thanks for the suggestion, and you're right, if I make that change and get the latest Rails 6 RC into the Appraisals file, the test suite is green.

@justincampbell did you want the Rails 6 fix added to this PR? Or would you prefer to merge what's here in first, and then I can create a smaller one for just Rails 6?

Copy link

Choose a reason for hiding this comment

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

Hello @pat and @justincampbell we are using this branch since September and would like to know if there is a todolist of outstanding items for the merge, now we already need support for Rails 6 as well.

We can work on this pull request if needed

Choose a reason for hiding this comment

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

Hello! I'm interested in using this in production as well, but also would need this merged to master. Can I help at all?

Copy link

Choose a reason for hiding this comment

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

Hello @pat Is there any work left to proceed with the merge ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mod if you're finding it works for you, then I think it's ready? I'm not an owner of the repo, I can't merge things.

And also: the organisation I worked for when creating this PR no longer exists, and so I'm no longer using this gem – so your experience with it is going to be far more valid and recent than mine.

Copy link
Contributor

@Valarissa Valarissa left a comment

Choose a reason for hiding this comment

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

Running locally, I see no test failures running 5.2. As such, since there hasn't been much movement here, I'm looking to merge this.

@Valarissa Valarissa dismissed justincampbell’s stale review May 4, 2020 22:52

I reviewed it myself and the changes requested were provided by the contributor(s).

@Valarissa Valarissa merged commit 69d7e4b into hashicorp:master May 4, 2020
@pat
Copy link
Contributor Author

pat commented May 5, 2020

Thanks @Valarissa :)

chrisarcand added a commit that referenced this pull request Sep 17, 2020
Pull up a chair, and join me on this very fun walk-through of why, as of
release 0.6.0, this gem doesn't actually support Rails 4.2 anymore and
probably shouldn't.

In #67, Rails 5+ support was added in the form of ditching our own dirty
attribute logic and instead utilizing the fancy new Attributes API,
which was officially introduced in Rails 5 but present (sorta) in Rails
4.2 as well. This very obviously cut off support of vault-rails for
Rails 4.1, which doesn't have this API at all. Tests passed for Rails
4.2, so it looked good.

However, the Attributes API in Rails 4.2 is actually totally private API
- it's no more than a refactoring in anticipation of the real API in 5,
and some of the API was accidentally exposed in documentation anyway in
the 4.2 release.

The whole point of the Attributes API is to provide a separation between
ActiveModel attributes and the real database columns behind them (if
any!), allowing you to flexibly cast types when putting and retrieving
data from the database. However, in Rails 4.2 the API is simply a
refactoring and the separation there doesn't exist: attributes are
one-to-one with database columns.

When vault-rails began to use the Attributes API in 0.6.0, a subtle
behavior change occurred (for all versions of Rails): a
`vault_attribute` is now registered as a real model attribute.

This change is fine...in Rails 5+. In modern versions, ActiveRecord is
smart enough to know that this defined attribute is _not_ backed by a
database column, and is omitted from things that expect a column to
exist.

In Rails 4.2, it's not. The newly discovered attribute is expected to be
backed by a database column, and things depend heavily on this
expectation. The case that led me down finding this issue are complex
queries utilizing joins and where conditions, which generate aliases
through intermediate tables, e.g.:

```
SELECT "dogs"."id" AS t0_r0, "dogs"."owner_id" AS t0_r1, ........
```

These queries are generated with a model's columns in mind, so you will
now get an error with 0.6.0 and Rails 4.2 (where current_thought is a
vault_attribute):

```
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column dogs.current_thought does not exist
LINE 1: ...AS t3_r38, "dogs"."current_thought" AS t3_r39, "dogs"."u...
```

Conclusion: As Rails 4.2 is long EOL'd anyway and we do *not* wish to
maintain the old implementation alongside using the Attributes API, it
makes sense to consider 0.6.x as supporting only Rails 5+. 0.5.0 will be
the last known compatible version with Rails =< 4.2
chrisarcand added a commit that referenced this pull request Sep 17, 2020
Pull up a chair, and join me on this very fun walk-through of why, as of
release 0.6.0, this gem doesn't actually support Rails 4.2 anymore and
probably shouldn't.

In #67, Rails 5+ support was added in the form of ditching our own dirty
attribute logic and instead utilizing the fancy new Attributes API,
which was officially introduced in Rails 5 but present (sorta) in Rails
4.2 as well. This very obviously cut off support of vault-rails for
Rails 4.1, which doesn't have this API at all. Tests passed for Rails
4.2, so it looked good.

However, the Attributes API in Rails 4.2 is actually totally private API
- it's no more than a refactoring in anticipation of the real API in 5,
and some of the API was accidentally exposed in documentation anyway in
the 4.2 release.

The whole point of the Attributes API is to provide a separation between
ActiveModel attributes and the real database columns behind them (if
any!), allowing you to flexibly cast types when putting and retrieving
data from the database. However, in Rails 4.2 the API is simply a
refactoring and the separation there doesn't exist: attributes are
one-to-one with database columns.

When vault-rails began to use the Attributes API in 0.6.0, a subtle
behavior change occurred (for all versions of Rails): a
`vault_attribute` is now registered as a real model attribute.

This change is fine...in Rails 5+. In modern versions, ActiveRecord is
smart enough to know that this defined attribute is _not_ backed by a
database column, and is omitted from things that expect a column to
exist.

In Rails 4.2, it's not. The newly discovered attribute is expected to be
backed by a database column, and things depend heavily on this
expectation. The case that led me down finding this issue are complex
queries utilizing joins and where conditions, which generate aliases
through intermediate tables, e.g.:

```
SELECT "dogs"."id" AS t0_r0, "dogs"."owner_id" AS t0_r1, ........
```

These queries are generated with a model's columns in mind, so you will
now get an error with 0.6.0 and Rails 4.2 (where current_thought is a
vault_attribute):

```
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column dogs.current_thought does not exist
LINE 1: ...AS t3_r38, "dogs"."current_thought" AS t3_r39, "dogs"."u...
```

Conclusion: As Rails 4.2 is long EOL'd anyway and we do *not* wish to
maintain the old implementation alongside using the Attributes API, it
makes sense to consider 0.6.x as supporting only Rails 5+. 0.5.0 will be
the last known compatible version with Rails =< 4.2.

This commit sets up dropping Rails 4.2 by demonstrating at this point
that tests will fail with Rails 4.2 and pass with Rails 5+
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.

8 participants