Skip to content

Commit

Permalink
Improved generator for Active Record encryption and MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Nov 11, 2024
1 parent 11fb1a2 commit 753011c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.3.2 (unreleased)

- Improved generator for Active Record encryption and MySQL

## 2.3.1 (2024-09-09)

- Fixed deprecation warning with Rails 7.1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ rails generate ahoy:messages --encryption=lockbox
rails db:migrate
```

To use Active Record encryption (Rails 7+, experimental), run:
To use Active Record encryption (Rails 7+), run:

```sh
rails generate ahoy:messages --encryption=activerecord
Expand Down
15 changes: 13 additions & 2 deletions lib/generators/ahoy/messages/activerecord_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ def to_column
when "lockbox"
"t.text :to_ciphertext\n t.string :to_bidx, index: true"
else
# TODO add limit: 510 for Active Record encryption + MySQL?
"t.string :to, index: true"
if encryption == "activerecord" && mysql?
"t.string :to, limit: 510, index: true"
else
"t.string :to, index: true"
end
end
end

Expand Down Expand Up @@ -63,6 +66,14 @@ def lockbox_method
"has_encrypted"
end
end

def mysql?
adapter =~ /mysql|trilogy/i
end

def adapter
ActiveRecord::Base.connection_db_config.adapter.to_s
end
end
end
end
Expand Down

0 comments on commit 753011c

Please sign in to comment.