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:in
execute’ /usr/lib/ruby/gems/1.9.1/gems/mysql2-0.2.11/lib/active_record/connection_adapters/mysql2_adapter.rb:413:incolumns' /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:680:in
columns’ /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:693:incolumn_names' /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:706:in
column_methods_hash’ /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:1036:inblock in all_attributes_exists?' /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:1035:in
each’ /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:1035:inall?' /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:1035:in
all_attributes_exists?’ /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:984:inmethod_missing' /var/lib/jenkins/jobs/Rails/workspace/app/models/reseller.rb:13:in
class: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:in
require’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:inblock in require' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:227:in
load_dependency’ /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:346:in
require_or_load’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:491:inload_missing_constant' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:183:in
block in const_missing’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:181:ineach' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:181:in
const_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:in
load’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:inblock in load' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:227:in
load_dependency’ /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/activeadmin-0.2.2/lib/active_admin.rb:185:in
block in load!’ /usr/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:185:ineach' /usr/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:185:in
load!’ /usr/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:212:inroutes' /var/lib/jenkins/jobs/Rails/workspace/config/routes.rb:3:in
block in <top (required)>’ /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/routing/route_set.rb:233:ininstance_exec' /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/routing/route_set.rb:233:in
draw’ /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:in
load’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:inblock in load' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:227:in
load_dependency’ /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/railties-3.0.7/lib/rails/application.rb:127:in
block in reload_routes!’ /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:127:ineach' /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:127:in
reload_routes!’ /usr/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:123:inblock 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:ininitialize' /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:33:in
new’ /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:33:inbuild' /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:in
block in build’ /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:ineach' /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:in
inject’ /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:inbuild' /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:162:in
app’ /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application/finisher.rb:35:inblock in <module:Finisher>' /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:25:in
instance_exec’ /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:25:inrun' /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:50:in
block in run_initializers’ /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:49:ineach' /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:49:in
run_initializers’ /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:134:ininitialize!' /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:77:in
method_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:in
require’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:inblock in require' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
block in load_dependency’ /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:innew_constants_in' /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
load_dependency’ /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/railties-3.0.7/lib/rails/application.rb:103:in
require_environment!’ /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:217:inblock in initialize_tasks' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:636:in
call’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:636:inblock in execute' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:631:in
each’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:631:inexecute' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:597:in
block in invoke_with_call_chain’ /usr/lib/ruby/1.9.1/monitor.rb:201:inmon_synchronize' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:590:in
invoke_with_call_chain’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:607:inblock in invoke_prerequisites' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:604:in
each’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:604:ininvoke_prerequisites' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:596:in
block in invoke_with_call_chain’ /usr/lib/ruby/1.9.1/monitor.rb:201:inmon_synchronize' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:590:in
invoke_with_call_chain’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:583:ininvoke' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2051:in
invoke_task’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2029:inblock (2 levels) in top_level' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2029:in
each’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2029:inblock in top_level' /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:2023:intop_level' /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2001:in
block in run’ /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:1998:in
run’ /usr/lib/ruby/gems/1.9.1/gems/rake-0.8.7/bin/rake:31:in<top (required)>' /usr/bin/rake:19:in
load’ /usr/bin/rake:19:in `<main>’
About this issue
- Original URL
- State: closed
- Created 13 years ago
- Comments: 28 (11 by maintainers)
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 inapp/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.