heroku rake db:migrate is not working in rails 4 - ruby-on-rails

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.

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.

Related

Rails Test failed could not connect to server: No such file or directory (PG::ConnectionBad)

i'm trying to run tests on my ruby on rails application but when i type rails test, it return this error:
/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/pg-1.2.3/lib/pg.rb:58:in `initialize': could not connect to server: No such file or directory (PG::ConnectionBad)
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
it seems that it require the gem pg, or a PostgresSQL database, but i'd like to run it in sqlite3 database,so i check my Gemfile and database.yml
database.yml
default: &default
adapter: postgresql
encoding: unicode
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
#database: db/development.sqlite3
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
<<: *default
database: db/production.sqlite3
GemFile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.7.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.2', '>= 6.0.2.2'
# Use sqlite3 as the database for Active Record
#gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'sqlite3'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
gem 'sqlite3'
end
group :production do
gem "pg"
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'devise','~>4.7.1'
i've tried to run rails db:migrate and rails db:create the two sqlite database are present in the db direcotry, is there other things that i've miss?
thank's

Postgres gem not loaded on initial Rails 4 app deployment

I got my Rails 4 app running on my local machine and on heroku at the moment. Now I tried to set up an ubutunu 14 server. For deployment I am using capistrano and the connection between my local machine and the server is already set. Currently I am trying to do the initial deploy with this command:
cap production deploy:initial
I get following error at a certain point:
Running ~/.rvm/bin/rvm default do bundle exec rake assets:precompile on MYIP
DEBUG Command: cd /home/deploy/apps/savoir/releases/20160209113448 && ( export RAILS_ENV="production" ; ~/.rvm/bin/rvm default do bundle exec rake assets:precompile )
DEBUG rake aborted!
DEBUG Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
My gemfile looks like this:
source 'http://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.8'
# Use sqlite3 as the database for Active Record
# gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0.1'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin]
gem 'bootstrap-sass', '~> 3.3.5.1'
gem 'autoprefixer-rails'
gem 'bootstrap-sass-extras'
gem 'bootstrap-daterangepicker-rails'
gem 'bootstrap-datepicker-rails'
gem 'simple_form'
gem 'nested_form'
gem 'ckeditor'
gem 'coffee-script-source', '1.8.0'
gem 'carrierwave', github:'carrierwaveuploader/carrierwave'
gem 'carrierwave-crop'
gem 'rmagick'
gem 'mini_magick'
gem 'momentjs-rails'
gem 'gon'
gem 'jquery-ui-rails'
gem 'jquery-fileupload-rails'
gem 'jquery-turbolinks'
gem 'jquery-validation-rails'
gem 'devise'
gem 'ransack'
gem 'will_paginate'
gem 'rails_12factor'
gem 'pg', '~> 0.18.3'
gem 'geocoder'
gem 'gmaps4rails'
gem 'underscore-rails'
group :development do
gem 'capistrano', require: false
gem 'capistrano-rvm', require: false
gem 'capistrano-rails', require: false
gem 'capistrano-bundler', require: false
gem 'capistrano3-puma', require: false
end
gem 'puma'
And my database.yml:
development:
adapter: postgresql
database: savoir_development
pool: 5
username: postgres
password: root
test:
adapter: postgresql
database: savoir_test
pool: 5
timeout: 5000
username: postgres
password: root
production:
adapter: postgresql
database: deploy
pool: 5
timeout: 5000
username: USER
password: PASSWORD
So as far as I can see it, the gemfile and database.yml should be fine. Can anyone find the mistake(s) I made? I found a few other threads that worked this topic but they didn't help in my case.
Ok, I found the solution.
Since I was using windows on my local machine, rails used this version of pg:
//Gemlock.lock
pg (0.18.4-x86-mingw32)
Changing it to
//Gemlock.lock
pg (0.18.4)
and pushing it to the git solved my problem.

ActiveRecord::StatementInvalid (SQLite3::ReadOnlyException: when using postgres

I am trying to deploy my first rails application on digital ocean in Ubuntu, I am unsure if I am doing this right. I initially had my production database in postgres and test and production in sqllite. In frustration, I changed all my databases to postgres my changing the database.yml. I am unsure if I did that correctly But I had this error: ActiveRecord::StatementInvalid (SQLite3::ReadOnlyException: . When I was in sqllite. I was not sure if I had to tell the server to switch to production mode or if it was configured to use sqllite.
What I want to do is use postgres instead of sqllite. I am using nginx and Unicorn. MY database is blank so I dont need to transfer anything. I have made the postgres database within postgres, I just need to point my app to that database. (I am unaware if I need to do something else
I have used this database.yml
development:
adapter: postgresql
encoding: unicode
host: localhost
database: blog_development
pool: 5
username: bob
password: password
test:
adapter: postgresql
encoding: unicode
database: blog_test
host: localhost
pool: 5
username: bob
password: password
production:
adapter: postgresql
encoding: unicode
host: localhost
database: blog_production
pool: 5
username: bob
password: password
Gem file:
source 'https://rubygems.org'
ruby '2.0.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.4'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'jquery-ui-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
gem 'bootstrap-sass' # for using bootstrap-rails"
gem 'faker'
gem 'will_paginate'
gem 'annotate', '~> 2.6.5'
gem 'font-awesome-rails' # for using font-awesome icons
gem 'redcarpet', '~> 2.1.1'
gem 'coderay', '~> 1.1.0' # For nice code snippets
gem 'devise'
gem 'sidekiq'
gem 'haml-rails'
group :development do
gem 'better_errors'
gem 'binding_of_caller'
gem 'meta_request'
gem 'guard-rspec'
end
group :test do
gem 'capybara'
gem 'factory_girl_rails', '4.2.0'
end
gem 'pg', '0.15.1'
group :development, :test do
gem 'rspec-rails'
# Use sqlite3 as the database for Active Record in testing
end
group :production do
gem 'rails_12factor', '0.0.2'
end
I have the rest of the code on my github: https://github.com/RubyQuarry/Bootstrap_blog
Run rake db:drop db:create db:drop to drop your old database(s) and recreate the new ones in Postgres.

LoadError Specified 'sqlite3' for database adapter?

My Gemfile looks like this:
source 'https://rubygems.org'
gem 'rails', '4.0.2'
gem 'bootstrap-sass', '2.1.0.0'
gem 'activesupport', '4.0.2'
gem 'jquery-rails', '2.0.2'
gem 'railties', '4.0.2'
gem 'sass-rails', '4.0.1'
gem 'coffee-rails', '4.0.1'
gem 'rails_12factor'
gem 'actionpack', '4.0.2'
gem 'sqlite3', '1.3.5'
group :development, :test do
gem 'rspec-rails', '2.11.0'
gem 'guard-rspec', '1.2.1'
gem 'guard-spork', '1.2.0'
gem 'childprocess', '0.3.6'
gem 'spork', '0.9.2'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '4.0.1'
gem 'coffee-rails', '4.0.1'
gem 'uglifier', '1.2.3'
end
group :test do
gem 'capybara', '1.1.2'
gem 'factory_girl_rails', '4.1.0'
gem 'cucumber-rails', '1.2.1', :require => false
gem 'database_cleaner', '0.7.0'
# gem 'launchy', '2.1.0'
# gem 'rb-fsevent', '0.9.1', :require => false
# gem 'growl', '1.0.3'
end
group :production do
gem 'pg', '0.12.2'
end
I run bundle install and bundle update but when I visit my app locally I get this error:
Specified 'sqlite3' for database adapter, but the gem is not loaded.
Add gem 'sqlite3' to your Gemfile.
I tried bundle show sqlite3 and I get /Users/siaW/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/sqlite3-1.3.5 which means I do have the Gem right?
So why isn't it loading?
In my database.yml I have this:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
I think you may try this:
Go into your Gemfile.lock file and search for the 'sqlite3' entry. You'll note it reads sqlite3 (1.3.8-x86-mingw32). Change that to sqlite3 (1.3.8-x64-mingw32) and then run the bundle install command and everything should work like normal.
from Sqlite3 gem not loading, gem installed and specified in project
I had this on OSX for a spec/dummy app. Without rebuilding the gemset (just rebuilding sqlite3), it was unsuccessful.
I had to do the following to get everything built properly:
rvm gemset empty
brew update
brew install sqlite
bundle update

New Rails app Postgres won't install

I just started a Rails 4 app from scratch with the intention to use Postgres. I went and changed the database.yml file to the following:
development:
adapter: postgresql
encoding: unicode
database: personalapi_dev
pool: 5
username: nickoneill
password:
host: localhost
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test: &test
adapter: postgresql
encoding: unicode
database: personalapi_test
pool: 5
username: nickoneill
password:
host: localhost
Yet when I run rake db:create it gives me the following error:
uninitialized constant ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::PG
/Users/nickoneill/.rvm/gems/ruby-2.0.0-p247#personalapi/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:562:in `active?'
/Users/nickoneill/.rvm/gems/ruby-2.0.0-p247#personalapi/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:360:in `verify!'
/Users/nickoneill/.rvm/gems/ruby-2.0.0-p247#personalapi/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:458:in `block in checkout_and_verify'
/Users/nickoneill/.rvm/gems/ruby-2.0.0-p247#personalapi/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:373:in `_run__1536634998624253346__checkout__callbacks'
/Users/nickoneill/.rvm/gems/ruby-2.0.0-p247#personalapi/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
I've tried everything from upgrading the Postgres gem, making sure Postgres is running, etc, yet not sure why it's not working. I have other rails apps (including Rails 4) on my computer that run just fine yet this new one won't work. Any ideas?
Update
Here's my gemfile:
source 'https://rubygems.org'
ruby "2.0.0"
# gem 'bootstrap-sass', '~> 2.3.2'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'
gem 'addressable'
#
# Error handling
#
gem "sentry-raven" #, :git => "https://github.com/getsentry/raven-ruby.git"
#
# Sidekiq gems
#
gem 'sidekiq' , '2.5.4'
gem 'redis' , '~> 3.0'
gem 'unicorn' , '~> 4.5'
# For sidekiq interface
#gem 'sinatra', require: false
gem 'slim'
gem 'sinatra', '>= 1.3.0', :require => nil
# json parsing
gem 'oj' , '2.0.1'
# templates
gem 'haml'
# file storage
# gem "paperclip", "~> 3.1"
# gem 'aws-sdk', '1.8.0'
# Use postgres as the database for Active Record
gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
group :development , :test do
gem "better_errors"
gem 'fakeweb'
gem 'rspec-rails', '2.12.2'
gem 'shoulda-matchers'
gem 'guard-rspec'
end
group :test do
#gem "cucumber-rails", ">= 1.3.0"
gem 'database_cleaner'
gem 'factory_girl_rails' , '~> 4.0'
gem 'capybara'
gem 'guard-spork'
gem 'spork'
gem "launchy"
gem 'rb-fsevent', '~> 0.9.1'
gem 'faker'
gem 'annotate'
gem 'simplecov'
gem 'guard-rspec'
gem 'growl'
gem 'rb-fsevent', '~> 0.9.1'
end
From my experience updating the Gemfile to gem 'pg', '0.15.1' solved the problem.

Resources