On Amazon’s EC2 using Ubuntu, when I execute rake db:migrate as:
bundle exec rake db:migrate RAILS_ENV="production" --trace
I get the error ActiveRecord::ConnectionNotEstablished as shown below
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
rake aborted!
ActiveRecord::ConnectionNotEstablished
/usr/local/rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:316:in `retrieve_connection'
I can login to mysql with the username and password in my config/database.yml file
and the database is there. So mysql is running, and that is not the problem.
My config/database.yml file looks like:
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: app_production
pool: 5
username: root
password: password
host: localhost
socket: /run/mysqld/mysqld.sock
Here are my gems from gem list:
abstract (1.0.0)
actionmailer (3.0.3)
actionpack (3.0.3)
activemodel (3.0.3)
activerecord (3.0.3)
activeresource (3.0.3)
activesupport (3.0.3)
arel (2.0.10)
builder (2.1.2)
bundler (1.0.10 ruby)
cgi_multipart_eof_fix (2.5.0)
daemons (1.0.10)
erubis (2.6.6)
eventmachine (0.12.10)
fastthread (1.0.7)
gem_plugin (0.2.3)
i18n (0.6.0)
mail (2.2.19)
mime-types (1.17.2)
mongrel (1.2.0.pre2)
mysql2 (0.2.7)
polyglot (0.3.3)
rack (1.2.5)
rack-mount (0.6.14)
rack-test (0.5.7)
rails (3.0.3)
railties (3.0.3)
rake (0.9.2.2)
rmagick (2.13.1)
thin (1.2.7)
thor (0.14.6)
treetop (1.4.10)
tzinfo (0.3.31)
xmpp4r (0.5)
The error in connection_pool.rb is happening at the same place as described in this post whose answer says that a connection needs to be established on ActiveRecord::Base. The code is failing in connection_pool.rb because it is getting sent klass = ActiveRecord::Base which doesn’t have a connection. So I tried creating a model my_connection_base.rb that looks like the following
require 'active_record'
class MyConnectionBase < ActiveRecord::Base
MyConnectionBase.establish_connection(
:adapter => "mysql2",
:host => "localhost",
:username => "<your database username>",
:password => "<your database password>",
:database => File.dirname(__FILE__) + "app_production"
)
self.abstract_class = true
end
And then all of my models inherit MyConnectionBase as in role.rb below:
require 'my_connection_base'
class Role < MyConnectionBase
has_and_belongs_to_many :users
end
But I still get ActiveRecord::ConnectionNotEstablished when I try to run a migration. Is there something else I need to do with Rails 3 to ensure that the connection is made to mysql before performing the migration?
I was able to get the migrations to work by adding db:create to the rake command line as shown below:
I don’t know why db:create is needed now. One thing I didn’t mention is that these migrations were transferred from a Rails 2 application to a Rails 3 application. I’m still having trouble creating pages in the migration, as shown below in the Page.create section that I commented out:
20101014205123_create_pages.rb
It also looks like the initial data creation can be done in db/seeds.rb which is where I transfered the Page.create code. I am still trying to get that to work using:
and I’m getting the error:
I was able to fix this error by requiring the model in db/seeds.rb as shown below: