fractaledmind / enlitenment

A Rails application template script to lead your app to SQLite enlightenment.
44 stars 3 forks source link

ActiveRecord::EnvironmentMismatchError after installing #15

Open Shaglock opened 2 weeks ago

Shaglock commented 2 weeks ago

Describe the bug Maybe I am doing something wrong, but after running the template generator (bin/rails app:template LOCATION=https://raw.githubusercontent.com/fractaledmind/enlitenment/main/template.rb SKIP_LITESTREAM=true SKIP_SOLID_ERRORS=true INSTALL_INTO=application), I am trying to make a new model and do rails db:migrate and then reset the db with rails db:reset I get the error:

bin/rails aborted!
ActiveRecord::EnvironmentMismatchError: You are attempting to modify a database that was last run in `test` environment. (ActiveRecord::EnvironmentMismatchError)
You are running in `development` environment. If you are sure you want to continue, first set the environment using:

        bin/rails db:environment:set RAILS_ENV=development

Tasks: TOP => db:drop => db:check_protected_environments
(See full trace by running task with --trace)
Running the same command with `--trace`
rails db:reset --trace
** Invoke db:reset (first_time)
** Invoke db:drop (first_time)
** Invoke db:load_config (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:load_config
** Invoke db:check_protected_environments (first_time)
** Invoke db:load_config
** Execute db:check_protected_environments
bin/rails aborted!
ActiveRecord::EnvironmentMismatchError: You are attempting to modify a database that was last run in `test` environment. (ActiveRecord::EnvironmentMismatchError)
You are running in `development` environment. If you are sure you want to continue, first set the environment using:

        bin/rails db:environment:set RAILS_ENV=development

/Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/activerecord-7.2.1/lib/active_record/tasks/database_tasks.rb:654:in `block in check_current_protected_environment!'
/Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/activerecord-7.2.1/lib/active_record/tasks/database_tasks.rb:554:in `with_temporary_pool'
/Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/activerecord-7.2.1/lib/active_record/tasks/database_tasks.rb:644:in `check_current_protected_environment!'
/Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/activerecord-7.2.1/lib/active_record/tasks/database_tasks.rb:69:in `block in check_protected_environments!'
/Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/activerecord-7.2.1/lib/active_record/tasks/database_tasks.rb:68:in `each'
/Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/activerecord-7.2.1/lib/active_record/tasks/database_tasks.rb:68:in `check_protected_environments!'
/Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/activerecord-7.2.1/lib/active_record/railties/databases.rake:19:in `block (2 levels) in 
' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:281:in `block in execute' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:281:in `each' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:281:in `execute' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:219:in `block in invoke_with_call_chain' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:199:in `synchronize' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:199:in `invoke_with_call_chain' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:243:in `block in invoke_prerequisites' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:241:in `each' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:241:in `invoke_prerequisites' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:218:in `block in invoke_with_call_chain' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:199:in `synchronize' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:199:in `invoke_with_call_chain' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:243:in `block in invoke_prerequisites' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:241:in `each' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:241:in `invoke_prerequisites' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:218:in `block in invoke_with_call_chain' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:199:in `synchronize' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:199:in `invoke_with_call_chain' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:188:in `invoke' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:188:in `invoke_task' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:138:in `block (2 levels) in top_level' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:138:in `each' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:138:in `block in top_level' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:147:in `run_with_threads' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:132:in `top_level' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/railties-7.2.1/lib/rails/commands/rake/rake_command.rb:27:in `block (2 levels) in perform' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:214:in `standard_exception_handling' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/railties-7.2.1/lib/rails/commands/rake/rake_command.rb:27:in `block in perform' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/railties-7.2.1/lib/rails/commands/rake/rake_command.rb:44:in `block in with_rake' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/rake-13.2.1/lib/rake/rake_module.rb:59:in `with_application' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/railties-7.2.1/lib/rails/commands/rake/rake_command.rb:41:in `with_rake' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/railties-7.2.1/lib/rails/commands/rake/rake_command.rb:20:in `perform' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/railties-7.2.1/lib/rails/command.rb:156:in `invoke_rake' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/railties-7.2.1/lib/rails/command.rb:73:in `block in invoke' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/railties-7.2.1/lib/rails/command.rb:149:in `with_argv' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/railties-7.2.1/lib/rails/command.rb:69:in `invoke' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/railties-7.2.1/lib/rails/commands.rb:18:in `
' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `require' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `block (2 levels) in replace_require' /Users/me/.local/share/mise/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require' bin/rails:4:in `
' Tasks: TOP => db:reset => db:drop => db:check_protected_environments

To Reproduce

Minimal reproduction I was able to do:

  1. rails new reproduction-enlitement
  2. cd reproduction-enlitement/
  3. rails db:migrate
  4. rails db:setup
  5. bin/rails app:template LOCATION=https://raw.githubusercontent.com/fractaledmind/enlitenment/main/template.rb SKIP_LITESTREAM=true SKIP_SOLID_ERRORS=true INSTALL_INTO=application
  6. rails db:reset (first time it seems to work fine)
  7. rails db:reset
  8. Rails suggests bin/rails db:environment:set RAILS_ENV=development to fix this issue, but it doesn't help

Expected behavior No issues with doing rails db:reset multiple times

Additional context Using rails 7.2.1, ruby 3.3.5.

I also noticed rails by default has sqlite3 gem version >= 1.4 while in this template it's ~> 2.0, and running the template doesn't update the gem. However I tried to update it manually and did NOT affect anything at all.

I also noticed when running reset for the first time it seems to try to do the same thing multiple times for some reason (delete and create cache\queue\cable dbs twice), not sure if that's related

Dropped database 'storage/development.sqlite3'
Dropped database 'storage/development-queue.sqlite3'
Dropped database 'storage/development-cache.sqlite3'
Dropped database 'storage/development-cable.sqlite3'
Dropped database 'storage/test.sqlite3'
Database 'storage/development-queue.sqlite3' does not exist
Database 'storage/development-cache.sqlite3' does not exist
Database 'storage/development-cable.sqlite3' does not exist
Created database 'storage/development.sqlite3'
Created database 'storage/development-queue.sqlite3'
Created database 'storage/development-cache.sqlite3'
Created database 'storage/development-cable.sqlite3'
Created database 'storage/test.sqlite3'
Database 'storage/development-queue.sqlite3' already exists
Database 'storage/development-cache.sqlite3' already exists
Database 'storage/development-cable.sqlite3' already exists
Shaglock commented 2 weeks ago

Actually I figured out the issue myself.

Test db uses same configuration as development (storage/<%= Rails.env %>-cache.sqlite3).

Changing database names for test environment fixed the problem.

diff --git a/config/database.yml b/config/database.yml
index 49fc1b3..0c42d6d 100644
--- a/config/database.yml
+++ b/config/database.yml
@@ -39,10 +39,15 @@ test:
   primary:
     <<: *default
     database: storage/test.sqlite3
-  queue: *queue
-  cache: *cache
-  cable: *cable
-
+  queue:
+    <<: *queue
+    database: storage/test-cable.sqlite3
+  cache:
+    <<: *cache
+    database: storage/test-cache.sqlite3
+  cable:
+    <<: *cable
+    database: storage/test-queue.sqlite3

 # SQLite3 write its data on the local filesystem, as such it requires
 # persistent disks. If you are deploying to a managed service, you should

@fractaledmind Do you have any other suggestions to fix the issue? If not, maybe the defaults should be changed to avoid this issue?