Running rails project on heroku - ruby-on-rails

I a little bit new for heroku and postgresql and dont know how works translting from mysql to postgresql.
My application was developed on mysql and to run it from heroku i made some steps:
1. Added a gem 'pg' and gem 'rails_12factor' like that:
group :production do
gem 'pg'
gem 'rails_12factor'
end
And bundle it without production
2. I also changed my database.yml into:
(Also i have question, how can i use mysql in devolepment and postgre in production?)
# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
# gem install pg
# On OS X with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
But!
When i tryed to work via heroku(downloading works fine) my dynamic pages wasnt working, and i get this errors:
So, i guessed that DB just doesnt migrate, OK, i runned via console this:
heroku run rake db:migrate --app name
Aaand i now i have this error:
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "schoolings" does not exist
: ALTER TABLE "students" ADD CONSTRAINT "fk_rails_e33c769d03"
FOREIGN KEY ("schooling_id")
REFERENCES "schoolings" ("id")
My questions:
1. Why it happens? Did i miss some steps when deployed my app?
2. How can i separate production and dev DB in deployment.yml?
Thanks, for any help!

First you have to create the database on heroku.
heroku run rake db:create --app-name
Then you can run the migrations.

Related

Rails 5: Attempting to create and use new test database

I need to create a new test database (postgres) and am having trouble. It seems that when I try to run any tests it is attempting to connect to the production database which is worrisome for many reasons. Here is my database.yml currently.
test:
adapter: postgresql
encoding: unicode
database: xxxxxx-test
pool: 5
username: xxx
host: localhost
But when I run a test I see:
An error occurred while loading ./spec/models/recipe_spec.rb.
Failure/Error: ActiveRecord::Migration.maintain_test_schema!
PG::ConnectionBad:
FATAL: no pg_hba.conf entry for host "IP_ADDRESS", user "PRODUCTION_USER", database "PRODUCTION_DATABASE", SSL off
I'm not sure why this is happening or how I am supposed to set this up.
Here's my gemfile for test:
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15', '< 4.0'
gem 'selenium-webdriver'
# Easy installation and use of chromedriver to run system tests with Chrome
gem 'chromedriver-helper'
gem 'rspec-rails', '~> 3.5'
gem 'database_cleaner'
gem 'factory_bot_rails'
end
It's worth noting that I do not have database_cleaner doing anything at the moment.
The database configuration from database.yml is merged with ENV["DATABASE_URL"]. But ENV["DATABASE_URL"] ALWAYS takes precedence over the YML configuration. See the Rails guides on configuration.
The bad news is that you have set ENV["DATABASE_URL"] to point to your production database which could have been catastrophic.
The good news is that you seem to have a IP whitelist on the production DB that denied your local IP. Otherwise you would be clobbering your production DB!
To fix this you need to determine where ENV["DATABASE_URL"] is being set and get rid of it. Depending on your setup this can be anywhere from your ~/.profile to the docker container configuration. Then confirm that you have the correct config:
$ rails runner -e test "puts ActiveRecord::Base.connection_config"
This should print something like:
{"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"xxxxxx-test"}
At this point its safe to create the databases for dev/test with:
$ rails db:create
$ rails db:schema:load
You can use ENV["DATABASE_URL"] to let developers use their own local configuration, but you should NEVER let it point to the production DB on anything except the actual production server!

Database migrations to heroku don't work

I've been trying to migrate my database to heroku, without success. I have added a few more users to my database and when trying to log them in with email address and password on my heroku app, I get invalid email/password error, even though this works perfectly fine on my local server. NO errors at all when doing all the steps described below.
As suggested in a previous post, I've tried the following:
Made changes to local code
Ran any migrations LOCALLY - I used bundle exec rake db:migrate
Added all changed files to Git git add -A
Commit all added files to git git commit -m "Adding features"
Pushed the changes to Heroku git push heroku master
Ran heroku run rake db:migrate
After I run this I get:
astelvida:~/workspace/sample_app (master) $ heroku run rake db:migrate
Running rake db:migrate on ⬢ shrouded-ravine-80000... up, run.2794
ActiveRecord::SchemaMigration Load (0.8ms)
SELECT "schema_migrations".* FROM "schema_migrations"
Following migrations do heroku restart
I've also checked my .sqlite3 file to check that the new users actually exist in the database.
I've also tried this: $ bundle exec rake db:migrate RAILS_ENV=production
I've also updated my gemfile.lock.
My gems in dev & production:
group :development, :test do
gem 'sqlite3', '1.3.9'
gem 'byebug', '3.4.0'
gem 'web-console', '2.0.0.beta3'
gem 'spring', '1.1.3'
end
group :production do
gem 'pg', '0.17.1'
gem 'rails_12factor', '0.0.2'
gem 'puma', '3.1.0'
end
Note: I run bundle install --without production, however this is how I've always been using it, and the login data works for some of the users i've created in the past. Also I'm using rails 4.2.2.
Ok... let's get something clear here.
Rake db:migrate doesn't migrate the data of the database. It runs all migrations (Table creations, updates, etc) so that the database structure is the same, however the data isn't! It's a fresh new database with the same structure.
What you're doing is making sure your PG database has the same structure as your sqlite3 database and it does. But if you want to pass the data from one to another It's gonna be hard i would say. You have to create a dump file from your sqlite 3 database, change it to pg and run in your heroku app.
Here is a question about it.
Convert SQLITE SQL dump file to POSTGRESQL

Unable to push git repo from cloud9 to Heroku [duplicate]

I'm trying to deploy my first app to Heroku. I'm using SQLite as the database. As far as I know Heroku doesn't use SQLite - it switches to Postgres in the backend.
When I'm deploying I get the following error:
/usr/ruby1.8.7/lib/ruby/gems/1.8/gems/bundler-1.0.0/lib/bundler/runtime.rb:64:in
`require': no such file to load --
sqlite3 (LoadError)
My Gemfile (which is what I assume is causing this problem) looks as follows:
source 'http://rubygems.org'
gem 'rails', '3.0.0'
gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
What am I doing wrong?
Heroku doesn't support SQLite databases. You need to use PostgreSQL on production, as I also explained in this post.
group :production do
gem "pg"
end
group :development, :test do
gem "sqlite3", "~> 1.3.0"
end
Actually, it's recommended to use in development/test an environment as close as possible to production. Therefore, I suggest you to switch all your environments to PostgreSQL.
# replace gem "sqlite3" with
gem "pg"
Simone Carletti is correct and so is Joost. You only need to group the sqlite3 gem or remove it entirely from your Gemfile. Heroku just needs to know that you don't want to use sqlite3 for production
So this:
...
group :development, :test do
gem "sqlite3-ruby", "~> 1.3.0", :require => "sqlite3"
end
...
Or this:
...
#No reference to sqlite3-ruby
...
If you remove the reference entirely you will probably mess up your local db though
After banging my head against this problem, I realized I was pushing the master branch of my repo to heroku, while I was making all of my postgres changes in my deploy-postgres branch of my repo!
I merged my deploy-postgres branch with my local master [git checkout master; git merge deploy-postgres] and then could run git push heroku master as per the heroku documentation.
I was stuck on this for hours looking at every answer here, but I couldn't get enough details to make it come together. This paged walked me through everything. http://railsapps.github.io/rails-heroku-tutorial.html
Good luck.
i was facing similar issue, but I realized that I was on a different branch - new_layout and was pushing master.
So I pushed my desired branch to heroku using following command and everything worked fine.
git push heroku new_layout:master
You can use clearDB addon
and gem 'mysql2' instead of gem 'sqlite3'
I'm using sqlite3 and deploy to Heroku no problem. Here is my database.yml
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
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

Ruby on Rails - Heroku deployment issues

I seem to have reached a bump, searched here and on other foruns but nothing. I'm running rails 3.2.3 and ruby 1.9.3 and want to deploy my app on heroku.
I've created the cedar and although I can git push heroku master I am getting a complete 500 server error.
I suspect is it because my DB isn't there. However, I can't seem to get it there.
I've run:
heroku run rake db:create -> This gives out some warnings about deprecation and then dkhgclqccm already exists
So it exists already? So lets migrate it:
heroku run rake db:migrate
However this outputs:
<deprecation errors>
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::Error: ERROR: relation "hphotos" does not exist
: ALTER TABLE "hphotos" ADD COLUMN "description" character varying(255)
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
That particular migration is:
class AddDescriptionToHphotos < ActiveRecord::Migration
def change
add_column :hphotos, :description, :string
end
end
It seems good to me, don't know why its giving me this error :/
One Last thing, even if this migrations worked, my DB would be empty and me seeds.rb doesn't have all the necessary data for the database. So I though about pushing the hole DB.
heroku db:push
! Taps Load Error: cannot load such file -- sqlite3
! You may need to install or update the taps gem to use db commands.
! On most systems this will be:
!
! sudo gem install taps
Why is this showing?
I installed the 'heroku' gem and the 'taps' gem and I got this in my gem file:
group :development, :test do
gem 'mysql2'
end
group :production do
gem 'pg'
end
Also, when I run heroku run rake db:version, it shows: Current version: 20120508130957
This is actually 5 migrations short on my currrent version, but I can't migrate it as shows the error I spoke of above...
Thanks in advance
Heroku comes with a database set up (which is why db:create didn't work). have you tried heroku run rake db:schema:load? This should take your schema.rb file and load it up into the DB. This is a much better way of doing it than db:migrate every time you want to setup a new DB
Edit:
For your last question about taps, it looks like it's trying to use sqlite locally but you only have pg in your Gemfile. You probably have in config/database.yml adapter: sqlite. So either you need to use postgres locally and change that adapter to postgres, or go the easier route and use sqlite locally and add that to the :development group.
Note that heroku only uses postgres so I would not suggest developing off of mysql since there are some inconsistencies in some syntax and how you do a couple of things between the two platforms. Then again, if you're using only ANSI-compatible queries or just using rails' methods to activate queries then you should be ok either way.
I think you need to check your migrations closely to see if you actually have a file which says:
def up
create_table :hphotos do |t|
[...]
end
It seems that the table hasn't been created remotely and you are attempting to modify it.
The solution is to add not only taps gem but also sqlite3 gem into the Gemfile, into the :development group. If you are using sqlite3 in your development, then adding taps gem would be enough.
But since you are using mysql2 on your development so to solve that problem you have to add both.
group :development do
gem 'taps'
gem 'sqlite3'
end

Deploying RoR app to Heroku with SQLite 3 fails

I'm trying to deploy my first app to Heroku. I'm using SQLite as the database. As far as I know Heroku doesn't use SQLite - it switches to Postgres in the backend.
When I'm deploying I get the following error:
/usr/ruby1.8.7/lib/ruby/gems/1.8/gems/bundler-1.0.0/lib/bundler/runtime.rb:64:in
`require': no such file to load --
sqlite3 (LoadError)
My Gemfile (which is what I assume is causing this problem) looks as follows:
source 'http://rubygems.org'
gem 'rails', '3.0.0'
gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
What am I doing wrong?
Heroku doesn't support SQLite databases. You need to use PostgreSQL on production, as I also explained in this post.
group :production do
gem "pg"
end
group :development, :test do
gem "sqlite3", "~> 1.3.0"
end
Actually, it's recommended to use in development/test an environment as close as possible to production. Therefore, I suggest you to switch all your environments to PostgreSQL.
# replace gem "sqlite3" with
gem "pg"
Simone Carletti is correct and so is Joost. You only need to group the sqlite3 gem or remove it entirely from your Gemfile. Heroku just needs to know that you don't want to use sqlite3 for production
So this:
...
group :development, :test do
gem "sqlite3-ruby", "~> 1.3.0", :require => "sqlite3"
end
...
Or this:
...
#No reference to sqlite3-ruby
...
If you remove the reference entirely you will probably mess up your local db though
After banging my head against this problem, I realized I was pushing the master branch of my repo to heroku, while I was making all of my postgres changes in my deploy-postgres branch of my repo!
I merged my deploy-postgres branch with my local master [git checkout master; git merge deploy-postgres] and then could run git push heroku master as per the heroku documentation.
I was stuck on this for hours looking at every answer here, but I couldn't get enough details to make it come together. This paged walked me through everything. http://railsapps.github.io/rails-heroku-tutorial.html
Good luck.
i was facing similar issue, but I realized that I was on a different branch - new_layout and was pushing master.
So I pushed my desired branch to heroku using following command and everything worked fine.
git push heroku new_layout:master
You can use clearDB addon
and gem 'mysql2' instead of gem 'sqlite3'
I'm using sqlite3 and deploy to Heroku no problem. Here is my database.yml
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
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

Resources