Forum: Ruby on Rails rake db:migrate behaving different for different environments

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Febcde7f09d486161ab6b3180c8b85c7?d=identicon&s=25 Gourav Tiwari (Guest)
on 2008-12-26 19:30
(Received via mailing list)
I am wondering in my application for past few days, why the rake
db:migrate is behaving differently for different environments. Any
clue is appreciated

Application name : project
Database name : project_my_own
Environment name : my_own

If I use simply:
rake db:migrate

It works, but say I have a separate environment called "my_own" and if
I run:
rake db:migrate RAILS_ENV=my_own

It blows up and gives several line of stack trace:
C:\project>rake db:migrate RAILS_ENV=my_own --trace
(in C:/project)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
Mysql::Error: Table 'project_my_own.contents' doesn't exist: SHOW
FIELDS FROM `contents`
C:/project/vendor/rails/activerecord/lib/active_record/
connection_adapters/abstract_adapter.rb:189:in `log'
C:/project/vendor/rails/activerecord/lib/active_record/
connection_adapters/mysql_adapter.rb:309:in
`execute_without_reconnect'
C:/project/vendor/plugins/manage/initializers/
mysql_adapter_extension.rb:7:in `execute'
C:/project/vendor/rails/activerecord/lib/active_record/
connection_adapters/mysql_adapter.rb:440:in `columns'
C:/project/vendor/rails/activerecord/lib/active_record/base.rb:1220:in
`columns'
C:/project/vendor/rails/activerecord/lib/active_record/base.rb:1239:in
`content_columns'
C:/project/vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb:
409:in `create_versioned_table'
C:/project/vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb:
261:in `acts_as_versioned'
C:/project/app/models/content.rb:16
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
C:/project/vendor/rails/activesupport/lib/active_support/
dependencies.rb:148:in `require'
C:/project/vendor/rails/activesupport/lib/active_support/
dependencies.rb:507:in `new_constants_in'
C:/project/vendor/rails/activesupport/lib/active_support/
dependencies.rb:148:in `require'
C:/project/vendor/rails/activesupport/lib/active_support/
dependencies.rb:254:in `require_or_load'
C:/project/vendor/rails/activesupport/lib/active_support/
dependencies.rb:413:in `load_missing_constant'
C:/project/vendor/rails/activesupport/lib/active_support/
dependencies.rb:77:in `const_missing'
C:/project/vendor/rails/activesupport/lib/active_support/
dependencies.rb:89:in `const_missing'
C:/project/app/models/comment.rb:14
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
C:/project/vendor/rails/activesupport/lib/active_support/
dependencies.rb:148:in `require'
C:/project/vendor/rails/activesupport/lib/active_support/
dependencies.rb:507:in `new_constants_in'
C:/project/vendor/rails/activesupport/lib/active_support/
dependencies.rb:148:in `require'
C:/project/vendor/rails/activesupport/lib/active_support/
dependencies.rb:254:in `require_or_load'
C:/project/vendor/rails/activesupport/lib/active_support/
dependencies.rb:213:in `depend_on'
C:/project/vendor/rails/activesupport/lib/active_support/
dependencies.rb:133:in `require_dependency'
C:/project/config/../vendor/rails/railties/lib/initializer.rb:364:in
`load_application_classes'
C:/project/config/../vendor/rails/railties/lib/initializer.rb:363:in
`each'
C:/project/config/../vendor/rails/railties/lib/initializer.rb:363:in
`load_application_classes'
C:/project/config/../vendor/rails/railties/lib/initializer.rb:361:in
`each'
C:/project/config/../vendor/rails/railties/lib/initializer.rb:361:in
`load_application_classes'
C:/project/config/../vendor/rails/railties/lib/initializer.rb:182:in
`process'
C:/project/config/../vendor/rails/railties/lib/initializer.rb:112:in
`send'
C:/project/config/../vendor/rails/railties/lib/initializer.rb:112:in
`run'
C:/project/config/environment.rb:9
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
C:/project/vendor/rails/activesupport/lib/active_support/
dependencies.rb:148:in `require'
C:/project/vendor/rails/activesupport/lib/active_support/
dependencies.rb:507:in `new_constants_in'
C:/project/vendor/rails/activesupport/lib/active_support/
dependencies.rb:148:in `require'
C:/project/vendor/rails/railties/lib/tasks/misc.rake:3
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:617:in `call'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:617:in `execute'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:612:in `each'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:612:in `execute'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:578:in
`invoke_with_call_chain'
C:/ruby/lib/ruby/1.8/monitor.rb:242:in `synchronize'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:571:in
`invoke_with_call_chain'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:588:in
`invoke_prerequisites'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:585:in `each'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:585:in
`invoke_prerequisites'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:577:in
`invoke_with_call_chain'
C:/ruby/lib/ruby/1.8/monitor.rb:242:in `synchronize'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:571:in
`invoke_with_call_chain'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:564:in `invoke'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:2019:in
`invoke_task'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1997:in
`top_level'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1997:in `each'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1997:in
`top_level'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:2036:in
`standard_exception_handling'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1991:in
`top_level'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1970:in `run'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:2036:in
`standard_exception_handling'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1967:in `run'
C:/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/bin/rake:31
C:/ruby/bin/rake:16:in `load'
C:/ruby/bin/rake:16
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-26 20:51
(Received via mailing list)
On 26 Dec 2008, at 18:29, Gourav Tiwari wrote:

> rake db:migrate
>
> It works, but say I have a separate environment called "my_own" and if
> I run:
> rake db:migrate RAILS_ENV=my_own
>
That environment has class_caching turned on which means all models/
controllers are preloaded when the framework is loaded. If when the
model is loaded it tries to inspect its table (as the
acts_as_versioned plugins seems to make it do) and that table doesn't
exist yet then bad things will happen.

Fred
6a480fa2dbd6f2ca51ff4190d0f29d03?d=identicon&s=25 Ryan (Guest)
on 2008-12-26 20:53
(Received via mailing list)
Hey Gourav,

Are you using the Content model or the Comment model in either config/
environment.rb or config/environments/my_own.rb?

-Ryan
Febcde7f09d486161ab6b3180c8b85c7?d=identicon&s=25 Gourav Tiwari (Guest)
on 2008-12-26 22:08
(Received via mailing list)
Hi Fred

Awesome! you are right, class_caching was turned on. I turned it off
and it worked!
But, I would like to pre-load the the models and controllers when the
framework is loaded, so is there any other option to have caching
enabled and I can make it work?
On Dec 26, 2:50 pm, Frederick Cheung <frederick.che...@gmail.com>
Febcde7f09d486161ab6b3180c8b85c7?d=identicon&s=25 Gourav Tiwari (Guest)
on 2008-12-26 22:10
(Received via mailing list)
Hey Ryan,
I am not using Content and Comment model in any of these files (config/
environment.rb, config/environments/my_own.rb)

-Gourav
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-26 22:59
(Received via mailing list)
On 26 Dec 2008, at 21:07, Gourav Tiwari wrote:

>
> Hi Fred
>
> Awesome! you are right, class_caching was turned on. I turned it off
> and it worked!
> But, I would like to pre-load the the models and controllers when the
> framework is loaded, so is there any other option to have caching
> enabled and I can make it work?

There's not a really good solution to this. One way is to have a
special environment just for running migrations (that obviously uses
the same database as normal) that has cache classes set to false.

Fred
Febcde7f09d486161ab6b3180c8b85c7?d=identicon&s=25 Gourav Tiwari (Guest)
on 2008-12-27 00:43
(Received via mailing list)
Many thanks Fred :)

I think this solution works fine for now.

On Dec 26, 4:57 pm, Frederick Cheung <frederick.che...@gmail.com>
This topic is locked and can not be replied to.