title: Rails 3.2 + ruby2 + mysql2 + NoMethodError: undefined method `query' for nil:NilClass
body: <p>We are upgrading to<br/> <strong>rails version 3.2.16.</strong><br/>
  <strong>Mysql gem: mysql2(0.3.14)<br/>
  ruby 2.0.0p247</strong></p>
  
  <p>when i am running rake db:migrate, i'm getting below exception:</p>
  
  <pre><code>NoMethodError: undefined method `query' for nil:NilClass: CREATE TABLE `recently_viewed_products` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `gid` bigint NOT NULL, `product_id` int(11) NOT NULL, `viewed_time` int(11)) ENGINE=InnoDB/usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:246:in `block in execute'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
  /usr/lib/ruby/gems/2.0.0/gems/activesupport-3.2.16/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  /usr/lib/ruby/gems/2.0.0/gems/ar-octopus-0.8.0/lib/octopus/rails3/abstract_adapter.rb:19:in `instrument'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:246:in `execute'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/mysql2_adapter.rb:213:in `execute'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract/schema_statements.rb:170:in `create_table'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:435:in `create_table'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:466:in `block in method_missing'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:438:in `block in say_with_time'
  /usr/lib/ruby/2.0.0/benchmark.rb:281:in `measure'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:438:in `say_with_time'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:458:in `method_missing'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:334:in `method_missing'
  /mnt/xxx/releases/20131218044833/db/migrate/20131202073728_create_recently_viewed_products.rb:5:in `up'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:370:in `up'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:410:in `block (2 levels) in migrate'
  /usr/lib/ruby/2.0.0/benchmark.rb:281:in `measure'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:410:in `block in migrate'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:389:in `migrate'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:528:in `migrate'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:777:in `call'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:777:in `ddl_transaction'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:719:in `block in migrate'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:700:in `each'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:700:in `migrate'
  /usr/lib/ruby/gems/2.0.0/gems/ar-octopus-0.8.0/lib/octopus/migration.rb:82:in `migrate_with_octopus'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:570:in `up'
  /usr/lib/ruby/gems/2.0.0/gems/ar-octopus-0.8.0/lib/octopus/migration.rb:105:in `up_with_octopus'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/migration.rb:551:in `migrate'
  /usr/lib/ruby/gems/2.0.0/gems/ar-octopus-0.8.0/lib/octopus/migration.rb:97:in `migrate_with_octopus'
  /usr/lib/ruby/gems/2.0.0/gems/activerecord-3.2.16/lib/active_record/railties/databases.rake:193:in `block (2 levels) in &lt;top (required)&gt;'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:236:in `call'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:236:in `block in execute'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:231:in `each'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:231:in `execute'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
  /usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:168:in `invoke_with_call_chain'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:161:in `invoke'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:149:in `invoke_task'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `each'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `block in top_level'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:115:in `run_with_threads'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:100:in `top_level'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:78:in `block in run'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:75:in `run'
  /usr/lib/ruby/gems/2.0.0/gems/rake-10.1.0/bin/rake:33:in `&lt;top (required)&gt;'
  /usr/bin/rake:23:in `load'
  /usr/bin/rake:23:in `&lt;main&gt;'
  </code></pre>
  
  <p>The exception is coming only when running migration for non default database.
  Here's my migration:</p>
  
  <pre><code>ActiveRecord::Base.establish_connection("big_#{Rails.env}")
  
  create_table :recently_viewed_products do |t|
    t.integer :gid, :null =&gt; false, :limit =&gt; 8
    t.integer :product_id, :null =&gt; false
    t.integer :viewed_time
  end
  
  add_index :recently_viewed_products, :gid
  add_index :recently_viewed_products, [:gid, :product_id], :unique=&gt;true, :name =&gt; "UNIQUE"
  
  ActiveRecord::Base.establish_connection(Rails.env)
  </code></pre>
  
  <p>Let me know if i am missing anything.</p>