heartcombo / devise

Flexible authentication solution for Rails with Warden.
http://blog.plataformatec.com.br/tag/devise/
MIT License
24.02k stars 5.55k forks source link

Database creation fails when using Devise model in combination with ActiveRecord Encryption ignore_case #5693

Open andreobrown opened 5 months ago

andreobrown commented 5 months ago

Pre-check

I searched both the devise and rails issues and found potentially related issues:

However, this issue seems to have a different root cause than those so I decided to report it.

Environment

Steps to reproduce

  1. Create a new rails application: rails new test_app cd test_app
  2. Add devise gem bundle add devise -v 4.9.4
  3. Install devise rails generate devise:install
  4. Generate a devise model with extra attributes name and original_name rails generate devise customers name:string original_name:string
  5. Add ActiveRecord Encryption configuration for name field to use deterministic encryption and to ignore case app/models/customer.rb encrypts :name, deterministic: true, ignore_case: true
  6. Attempt to create the database bin/rails db:create

Current behavior

Database creation fails with this error:

rails db:create                                                 
bin/rails aborted!
ActiveRecord::StatementInvalid: Could not find table 'customers' (ActiveRecord::StatementInvalid)
/Users/andre/dev/devise-ar-encrypts/app/models/customer.rb:7:in `<class:Customer>'
/Users/andre/dev/devise-ar-encrypts/app/models/customer.rb:1:in `<main>'
/Users/andre/dev/devise-ar-encrypts/config/routes.rb:2:in `block in <main>'
/Users/andre/dev/devise-ar-encrypts/config/routes.rb:1:in `<main>'
/Users/andre/dev/devise-ar-encrypts/config/environment.rb:5:in `<main>'
Tasks: TOP => db:create => db:load_config => environment
(See full trace by running task with --trace)

Expected behavior

Database creation should succeed:

rails db:create
Created database 'storage/development.sqlite3'
Created database 'storage/test.sqlite3'

Sample application

I've prepared a sample application here: https://github.com/andreobrown/devise-ar-encrypts/tree/scaffold-encrypts

It has three branches:

Something to note is that if you remove ignore_case: true the database is created.

Additionally, note line 28 in application.rb on the devise-encrypts branch. Uncommenting that line and using the workaround suggested here allows database creation to succeed.

There are a few things that seem different about this issue compared to one fixed in the PR: