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?
After seeing the same issue as you, I did some digging (as my comment on your answer below mentions).
As it turns out, the basic piece I was missing was in my config/environment.rb
, I was using
MyApp::Application.initialize
which was not throwing an error when it tried to read my poorly migrated environments/{development,test, ...}.rb
files. I replaced the initialize call with
MyApp::Application.initialize!
Then the db:migrate
call started failing with errors that showed the real issues (which were getting swallowed by the initialize
(without a bang) calls). My config/development.rb
file was not written properly, also probably a result of the rails2 => rails3 migration.
The reason the db:create
saved the day is only because it does the ActiveRecord::Base.configurations
setup and doesn't depend on the environment to load that stuff so it was not failing while trying to read my faulty environments/ files.
After getting my environment files up to speed, I was on my way.
I hope this helps.
I was able to get the migrations to work by adding db:create to the rake command line as shown below:
bundle exec rake db:create db:migrate RAILS_ENV="production"
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
class CreatePages < ActiveRecord::Migration
def self.up
create_table :pages do |t|
t.column :title, :string
t.column :permalink, :string
t.column :body, :text
t.column :created_at, :datetime
t.column :updated_at, :datetime
t.timestamps
end
# Page.create(:title => "Home",
# :permalink => "welcome-page",
# :body => "Welcome to Home")
end
def self.down
drop_table :pages
end
end
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:
bundle exec rake db:create db:seed RAILS_ENV="production" --trace
and I'm getting the error:
uninitialized constant Page
I was able to fix this error by requiring the model in db/seeds.rb as shown below:
require File.expand_path('../../app/models/page', __FILE__)
Page.create(:title => "Home",
:permalink => "welcome-page",
:body => "Welcome to Home")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.