Skip to content

Commit bd046bd

Browse files
committed
do not encrypt in values
Take the following example ``` ActiveRecord::Encryption.config.support_unencrypted_data = true EncryptedBook.find_by(name: "Dune") ``` Before: ``` EXEC sp_executesql N'SELECT [encrypted_books].* FROM [encrypted_books] WHERE [encrypted_books].[name] IN (@0, @1) ORDER BY [encrypted_books].[id] ASC OFFSET 0 ROWS FETCH NEXT @2 ROWS ONLY', N'@0 nvarchar(max), @1 nvarchar(max), @2 int', @0 = N'{"p":"BbyqDSWICbMEBTtrN4iv0paan00brX1ly6wT92lFrgOQZomP5jBjhPlalbTvucTFX249bJHp7j6usa7SBpAuQFzN+sZFa3dMnlTcaei5","h":{"iv":"2esXR4qnvSteG+o7","at":"pXZzi+SpMNxpAvA8eQYVqA=="}}', @1 = N'{"p":"DIohhw==","h":{"iv":"wEPaDcJP3VNIxaiz","at":"X7+2xvvcu1k1if6Dy28Esw=="}}', @2 = 1 [["name", nil], ["name", nil], ["LIMIT", nil]] ``` After: ``` EXEC sp_executesql N'SELECT [encrypted_books].* FROM [encrypted_books] WHERE [encrypted_books].[name] IN (@0, @1) ORDER BY [encrypted_books].[id] ASC OFFSET 0 ROWS FETCH NEXT @2 ROWS ONLY', N'@0 nvarchar(max), @1nvarchar(max), @2 int', @0 = N'{"p":"DIohhw==","h":{"iv":"wEPaDcJP3VNIxaiz","at":"X7+2xvvcu1k1if6Dy28Esw=="}}', @1 = N'Dune', @2 = 1 [["name", nil], ["name", nil], ["LIMIT", nil]] ```
1 parent f5fc1c4 commit bd046bd

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
#### Added
1212

1313
- [#972](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/972) Support `ActiveRecord::QueryLogs`
14+
- [#981](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/981) Support `find_by` an encrypted attribute
1415

1516
Please check [6-1-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/6-1-stable/CHANGELOG.md) for previous changes.

lib/arel/visitors/sqlserver.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ def visit_Arel_Nodes_HomogeneousIn(o, collector)
8383
# Monkey-patch start. Add query attribute bindings rather than just values.
8484
column_name = o.column_name
8585
column_type = o.attribute.relation.type_for_attribute(o.column_name)
86+
# Use cast_type on encrypted attributes. Don't encrypt them again
87+
column_type = column_type.cast_type if column_type.is_a?(ActiveRecord::Encryption::EncryptedAttributeType)
8688
attrs = values.map { |value| ActiveRecord::Relation::QueryAttribute.new(column_name, value, column_type) }
8789

8890
collector.add_binds(attrs, &bind_block)

0 commit comments

Comments
 (0)