Ali
Ali

Reputation: 21

heroku rake db:migrate is not working in rails 4

When I do rake db:migrate, I get the error,below i have attached my databse.yml and my gem file, tried all the stuff on the internet but the error did'nt get resolved!

rake aborted!
LoadError: cannot load such file -- mysql2

development:
  adapter: mysql2
  encoding: utf8
  database: demo_project_development
  pool: 5
  username: root
  password: root
  socket: /var/run/mysqld/mysqld.sock
  host: localhost
test:
  adapter: mysql2
  database: demo_project_test
  database: db/development.mysql2
  username: root
  password: root
  pool: 5
  timeout: 5000
production:
  adapter: postgresql
  database: demo_project_production
  pool: 5
  timeout: 5000

Gemfile source 'https://rubygems.org' gem 'rails', '4.2.6' gem 'sass-rails', '~> 5.0' gem 'uglifier', '>= 1.3.0' gem 'coffee-rails', '~> 4.1.0' gem 'jquery-rails' gem 'turbolinks' gem 'jbuilder', '~> 2.0' gem 'sdoc', '~> 0.4.0', group: :doc

group :production do
  gem 'pg', '0.20'
  gem 'rails_12factor'
end

group :test do
  gem 'byebug'
  gem 'mysql2', '>= 0.3.13', '< 0.5'
end

gem 'spring'
gem 'devise'
gem "cancan"
gem 'ckeditor', '4.1.3'
gem "nested_form"
gem "paperclip", "~> 5.0.0"
gem 'bootstrap-sass', '~> 3.3.6'
gem 'kaminari'
gem 'ratyrate'
gem 'thinking-sphinx', '~> 3.3.0'
gem 'delayed_job_active_record'
gem 'rails-api'
gem 'active_model_serializers', '~> 0.10.6'
group :development do
  gem 'web-console', '~> 2.0'
  gem 'mysql2', '>= 0.3.13', '< 0.5'
end

Below is my application.rb

require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)
module DemoProject
  class Application < Rails::Application
    config.active_record.raise_in_transactional_callbacks = true
    config.active_job.queue_adapter = :delayed_job
    config.api_only = false
  end
end

config/envoirment.rb

require File.expand_path('../application', __FILE__)
Rails.application.initialize!

boot.rb

ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' # Set up gems listed in the Gemfile.

Upvotes: 0

Views: 253

Answers (1)

m. simon borg
m. simon borg

Reputation: 2575

I was able to reproduce your error in a dummy Rails 4.2.6 app with your Gemfile and database.yml. I couldn't even push the app to Heroku without raising LoadError: cannot load such file -- mysql2.

I fixed the problem by removing the mysql2 gem from the development and test groups and adding it to the default group. My guess is that running bundle install locally adds it as a dependency to Gemfile.lock which is checked into git and pushed to Heroku. Heroku doesn't install the gem and when the app tries to require it a LoadError is raised.

group :test do
  gem 'byebug'
end

group :development do
  gem 'web-console', '~> 2.0'
end

gem 'mysql2', '>= 0.3.13', '< 0.5'

A couple notes: if you want to add one gem to two or more specific groups, do not specify them separately. This just means there are two places where you're going to have to remember to change your version dependencies. So instead of this

group :test do
  gem 'byebug'
  gem 'another_gem'
end

group :development do
  gem 'web-console', '~> 2.0'
  gem 'another_gem'
end

do this

group :test do
  gem 'byebug'
end

group :development do
  gem 'web-console', '~> 2.0'
end

group :development, :test do
  gem 'another_gem'
end

Also, you shouldn't use MySQL locally and PostgreSQL in production, just use Postgres for both. They are not fully compatible, so you are bound to run into confusing issues down the road. Your best solution to this problem would be to remove mysql2 altogether and configure your dev and test databases for Postgres.

Upvotes: 0

Related Questions