activeadmin: Migrations Fail for New Models

I created a new model and corresponding admin controller. When I attempt to run a migration, it fails due to the model’s table not existing. I have traced this problem to a callback, to_prepare, that is reloading routes during the migration.

For now I have added a condition on the call to ActiveAdmin.routes() call:

ActiveAdmin.routes(self) if $ARGV.find_all { |x| x =~ /migrate|rollback/i}.empty?

Here is a stack trace from the migration:

exoadmin@build-linux:/var/lib/jenkins/jobs/Rails/workspace$ rake RAILS_ENV=test db:migrate --trace (in /var/lib/jenkins/jobs/Rails/workspace) /usr/lib/ruby/gems/1.9.1/gems/tlsmail-0.0.1/lib/net/smtp.rb:806: warning: already initialized constant SMTPSession /usr/lib/ruby/gems/1.9.1/gems/tlsmail-0.0.1/lib/net/pop.rb:687: warning: already initialized constant POP /usr/lib/ruby/gems/1.9.1/gems/tlsmail-0.0.1/lib/net/pop.rb:688: warning: already initialized constant POPSession /usr/lib/ruby/gems/1.9.1/gems/tlsmail-0.0.1/lib/net/pop.rb:689: warning: already initialized constant POP3Session /usr/lib/ruby/gems/1.9.1/gems/tlsmail-0.0.1/lib/net/pop.rb:702: warning: already initialized constant APOPSession ** Invoke db:migrate (first_time) ** Invoke environment (first_time) ** Execute environment rake aborted! Table ‘office_ready_test.resellers’ doesn’t exist /usr/lib/ruby/gems/1.9.1/gems/mysql2-0.2.11/lib/active_record/connection_adapters/mysql2_adapter.rb:263:in query' /usr/lib/ruby/gems/1.9.1/gems/mysql2-0.2.11/lib/active_record/connection_adapters/mysql2_adapter.rb:263:inexecute’ /usr/lib/ruby/gems/1.9.1/gems/mysql2-0.2.11/lib/active_record/connection_adapters/mysql2_adapter.rb:413:in columns' /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:680:incolumns’ /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:693:in column_names' /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:706:incolumn_methods_hash’ /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:1036:in block in all_attributes_exists?' /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:1035:ineach’ /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:1035:in all?' /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:1035:inall_attributes_exists?’ /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:984:in method_missing' /var/lib/jenkins/jobs/Rails/workspace/app/models/reseller.rb:13:inclass:Reseller’ /var/lib/jenkins/jobs/Rails/workspace/app/models/reseller.rb:1:in <top (required)>' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:inrequire’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in block in require' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:227:inload_dependency’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in require' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:346:inrequire_or_load’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:491:in load_missing_constant' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:183:inblock in const_missing’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:181:in each' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:181:inconst_missing’ /var/lib/jenkins/jobs/Rails/workspace/app/admin/resellers.rb:14:in <top (required)>' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:inload’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in block in load' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:227:inload_dependency’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in load' /usr/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:185:inblock in load!’ /usr/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:185:in each' /usr/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:185:inload!’ /usr/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:212:in routes' /var/lib/jenkins/jobs/Rails/workspace/config/routes.rb:3:inblock in <top (required)>’ /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/routing/route_set.rb:233:in instance_exec' /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/routing/route_set.rb:233:indraw’ /var/lib/jenkins/jobs/Rails/workspace/config/routes.rb:1:in <top (required)>' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:inload’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in block in load' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:227:inload_dependency’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in load' /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:127:inblock in reload_routes!’ /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:127:in each' /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:127:inreload_routes!’ /usr/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:123:in block in setup' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:420:in_run_prepare_callbacks’ /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/callbacks.rb:40:in initialize' /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:33:innew’ /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:33:in build' /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:inblock in build’ /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:in each' /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:ininject’ /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:in build' /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:162:inapp’ /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application/finisher.rb:35:in block in <module:Finisher>' /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:25:ininstance_exec’ /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:25:in run' /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:50:inblock in run_initializers’ /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:49:in each' /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:49:inrun_initializers’ /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:134:in initialize!' /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:77:inmethod_missing’ /var/lib/jenkins/jobs/Rails/workspace/config/environment.rb:5:in <top (required)>' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:inrequire’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in block in require' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:inblock in load_dependency’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in new_constants_in' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:inload_dependency’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in require' /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:103:inrequire_environment!’ /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:217:in block in initialize_tasks' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:636:incall’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:636:in block in execute' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:631:ineach’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:631:in execute' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:597:inblock in invoke_with_call_chain’ /usr/lib/ruby/1.9.1/monitor.rb:201:in mon_synchronize' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:590:ininvoke_with_call_chain’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:607:in block in invoke_prerequisites' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:604:ineach’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:604:in invoke_prerequisites' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:596:inblock in invoke_with_call_chain’ /usr/lib/ruby/1.9.1/monitor.rb:201:in mon_synchronize' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:590:ininvoke_with_call_chain’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:583:in invoke' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2051:ininvoke_task’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2029:in block (2 levels) in top_level' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2029:ineach’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2029:in block in top_level' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2068:instandard_exception_handling’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2023:in top_level' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2001:inblock in run’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2068:in standard_exception_handling' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:1998:inrun’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/bin/rake:31:in <top (required)>' /usr/bin/rake:19:inload’ /usr/bin/rake:19:in `<main>’

About this issue

  • Original URL
  • State: closed
  • Created 13 years ago
  • Comments: 28 (11 by maintainers)

Most upvoted comments

please reopen this since is very annoying

+1 for reopen this bug

This is still a problem. The issue lies here.

Basically, when we load all the app/admin/ files, we reference pretty much every model constant. If you’ve just generated a model and a migration, but haven’t run it yet, and you create the file for the model in app/admin/, you’ll reference the model during the Rails boot process. That’s typically fine, Rails will go autoload the model file, but if the table doesn’t exist yet, since the migration hasn’t been run, it will raise an SQL exception, since part of initializing the ActiveRecord class involves asking the database for all the columns on that table.

So, just run your migrations, right? Wrong. The routes file gets included when booting the environment to run the rake task, which calls ActiveAdmin::Application.load!, which causes exactly what I described above while trying to run the migration to solve the problem you’re having in the first place. Pretty frustrating.

I’m working on a solution, but I don’t really like the things proposed above… Seems hacky.

Anyone like @pcreux or @gregbell got any ideas? I’m having this issue using the latest from this very repo.