-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Introduce compressor
option to ActiveRecord::Encryption::Encryptor
#51735
base: main
Are you sure you want to change the base?
Conversation
aeb3171
to
17ff722
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The naming of the custom compressor's methods seems inconsistent to me. Would you consider using Encryptor
's convention and name them compress/uncompress
instead of ZLib
's deflate/inflate
?
I also think this PR should update the Rails Guides page on encryption, guides/source/active_record_encryption.md
to explain how to implement a custom compressor.
17ff722
to
633cdc2
Compare
883a321
to
3231918
Compare
Hi, @flavorjones ! I changed signature of compressor ( |
a34e1dd
to
d2de562
Compare
CI failures seem unrelated, you might want to rebase just to be sure as I think most of those have been fixed |
5664b8c
to
d0a8297
Compare
@zzak I just rebased it, and it looks like all CI failures are fixed now. 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so I'm only realizing now that Rails already has prior art for this sort of compression API, see the docs for ActiveSupport::Cache::Store.new and the :compressor
option.
In summary, "Must respond to deflate
and inflate
"
So I hate to ask, especially since I asked you to change it in the first place, but can you update this PR to use that naming convention? I sincerely apologize for not realizing this sooner.
a7ff27d
to
a0be4ad
Compare
666952d
to
18b4cc2
Compare
There is a ci failure but it looks like unrelated.
|
18b4cc2
to
038b9a9
Compare
038b9a9
to
287ca48
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The feature seems like a good idea to me, thanks for working on it @heka1024
I left a comment about the API design and I also think this should provide a config option so apps that want the same custom compressor for every model can just set it once.
end | ||
|
||
class User | ||
encrypts :name, encryptor: ActiveRecord::Encryption::Encryptor.new(compressor: ZstdCompressor) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This API doesn't feel right. Having to initialize a new Encryptor in your app to set the compressor feels cumbersome and un-Rails like. I have two suggestions:
- I think if we want a compressor option for every model then Rails should do the initialization for the app. The AR code would check if the compressor option is set and then Rails would do the initialization for the application. Then it's a much cleaner API because it becomes:
encrypts :name, compressor: ZstdCompressor
- Rails should provide a config option so that applications can set all encryptors to use the same custom compressor.
config.active_record_encryption.compressor = ZstdCompressor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eileencodes Thank you for review! Your talk at the 2023 Rails conference inspired me to contribute to Rails, and I'm glad I got a review 😄
- This API seems nicer than the earlier one. I'll implement it. Additionally, I think we should accept the compress option in the
encrypts
method, as introduced in Allow encryption without compression #50876. I'll handle that in this PR. So, we can use like this :
encrypts :title, compress: false
encrypts :content, compressor: ZstdCompressor
- I've already implemented this feature in Config. Should I just add it to the CHANGELOG?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@heka1024 New config parameters require a little more documentation work than other features. Specifically, you'll need to update guides/source/configuring.md
.
There is a linting tool you should run to make sure configuring.md
changes are correct, run tools/railspect configuration .
Motivation / Background
There are more performant algorithms than
zlib
, likezstd
orsnappy
. So I made it possible to configure the compressor to take advantage of these more performant algorithms.Detail
Make
compressor
as argument which default value iszlib
.Additional information
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
Discussion
What do you thinks about make compressor as property of
ActiveRecord::Encryption
?