Elementary Rails deployment - ruby-on-rails

I am trying to deploy my app on Rails for the first time using Heroku.
I have spent quite a lot of time but there's a gap somewhere.
Git: created private repo, pushed to git successfully
Heroku: created free app and pushed successfully (but app 'crashes')
Local:
rake db:schema:dump #success
rake db:schema:load RAILS_ENV=production #failure: production database is not configured
rake db:create db:load RAILS_ENV=production #failure: undefined method '[]' for nil:NilClass
active_record/railties/databases.rake:59:in 'rescue in create_database'
active_record/railties/databases.rake:39:in 'create_database'
My database.yml file:
defaults: &defaults
adapter: mysql
username: root
password: password
host: localhost
development:
<<: *defaults
database: project_dev
test:
<<: *defaults
database: project_test
Just added:
production:
<<: *defaults
database: project_production
I may be making a total rookie mistake. Do you know where I might be going wrong?

Use the command heroku rake db:schema:load, which simply executes the command rake db:schema:load on Heroku's environment.
You do not need to worry about the database environments are they are automatically configured by Heroku on the compilation of the slug.

The rake commands which you are running, run on your development machine. If you want to run rake commands on the server, use the heroku command (example):
heroku rake db:create
Note, if you want to push data, you are doing it wrong. Go to heroku.com and look at the docs there.

Related

Postgres db not working on Heroku

I've spend several hours figuring out how to get my database up and running. I created a new rails app and wanted to deploy it to heroku. I followed the instructions from heroku (to switch from sqlite3 -> postgresql) but it just doesn't work.
This is in my database.yml file:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
url: <%= ENV['DATABASE_URL'] %>
I can't create or seed any data in the database. Sometimes it executes the db:migrate, but even then it doesn't create anything. This is what I get when running:
heroku run rake db:create
FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
Does anyone has an idea on how to solve this? I don't have a clue anymore ...
Thanks!
By default you don't need to create a db on heroku.
Just run heroku run rails db:migrate and rest of the stuff will be handled by heroku itself.
Also your database.yml should be changed to following for Production env.
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
Also your rails app is by default assigned with a Postgres addon.
You can check this by running command heroku addons in the console.
Source -
Heroku Getting Started Guide
Heroku Postgres Addon Guide
You cannot create a database on Heroku using db:create (you cannot drop it neither). Your database is created when you add an add-on (such as Heroku Postgres). You can only migrate and seed. And if you want to start over, you can use pg:reset (instead of drop and create)
So the correct sequence should be:
add the Heroku add-on (such as Heroku Postgres). Add-ons are located here: https://elements.heroku.com/addons.
rake db:migrate
rake db:seed
if you want to start over
rake pg:reset
rake db:migrate
rake db:seed
From Heroku documentation: https://devcenter.heroku.com/articles/heroku-postgresql
The PostgreSQL user your database is assigned doesn’t have permission to create or drop databases. To drop and recreate your database use pg:reset.
As per the given stacktrace, it seems like you are trying to create a database on heroku which in turn is giving you Permission Denied Error.
Firstly, you do not need to run
heroku run rake db:create
Instead run
heroku run rake db:migrate
and it should migrate the migrations which are down.
For checking the current status of migrations run the following command:
heroku run rake db:migrate:status
Other Point you mentioned:
-> I can't create or seed any data in the database
As already mentioned above you can't create a database as heroku does it for you .
For seeding data in the database run the following command:
heroku run rake db:seed

Added to Rails app on Heroku. How to setup database in local environment?

I was recently added to a Rails application via Heroku. I'm able to clone the app to my local machine using:
heroku git:clone -a appname
However, I have issues running local tests and other tasks related to not having a local database. The production environment uses Postgres and the development environment uses sqlite3. What's the best way to get a local database setup?
Simply pull the production database from Heroku. Run the following once you have heroku_toolbelt and you have access to the app on Heroku:
heroku pg:pull DATABASE_URL app_development --app app_name_on_heroku_here
This will create a new database locally named "app_development" so rename it to your app. See config/database.yml for your development database name.
You will need to setup the databases required for development. This can be done using the built in rake db tasks:
rake db:create
rake db:migrate
This should see your local database created and migrated up to the latest migration.
As you have successfully clone rails project from Heroku and your production database is PostgreSQL which is different from your local database SQLite you should update some files from your local project
1. remove or comment "gem pg" and add gem 'sqlite3' and run bundle install
2. Make changes in your database.yml file
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
3 run rake db:create
4 run rake db:migrate
5 if you have data in seed file run rake db:seed

Converting from SQLITE3 to postgres for Heroku

I know there are numerous topics on this. I have tried them all to no success. Note, I do not need to maintain my previous data. I just need my app to work in Heroku.
Following this guide, this is one thing I've tried
Change from SQLite to PostgreSQL in a fresh Rails project
So I have my application that works using sqlite3.
Go into 'gemfile' and change
gem 'sqlite3' into gem 'pg'
run $ bundle in terminal (to change Gemfile.lock)
Go to my database.yml file and replace it with
development:
adapter: postgresql
encoding: utf8
database: project_development
pool: 5
username:
password:
test: &TEST
adapter: postgresql
encoding: utf8
database: project_test
pool: 5
username:
password:
production:
adapter: postgresql
encoding: utf8
database: project_production
pool: 5
username:
password:
cucumber:
<<: *TEST
I then pushed this to github.
On the Heroku website I connect the github directory to my app and deploy it.
It successfully deploys but the page only says
We're sorry, but something went wrong.
If you are the application owner check the logs for more information.
I am pretty certain I am missing a step as whenever I try to run it on my local machine it says
ActiveRecord::NoDatabaseError
FATAL: database "project_development" does not exist
Extracted source (around line #661):
659 rescue ::PG::Error => error
660 if error.message.include?("does not exist")
661 raise ActiveRecord::NoDatabaseError.new(error.message, error)
662 else
663 raise
664 end
--
EDIT
When I input
heroku run rake db:create
I get the response
▸ Error: No app specified
▸ Usage: heroku run --app APP
▸ We don't know which app to run this on.
▸ Run this command from inside an app folder or specify which app to use with --app APP
When I run
heroku run rake db:create -app nguyen-andrew-a2
I get the response
Running rake db:create nguyen-andrew-as2 on ⬢ pp... !!!
▸ Couldn't find that app.
Macbook-Pro-5:try2 andrew$
--
On my local machine, after running it the page now says
fe_sendauth: no password supplied
Extracted source (around line #651):
649 # connected server's characteristics.
650 def connect
651 #connection = PGconn.connect(#connection_parameters)
652
653 # Money type has a fixed precision of 10 in PostgreSQL 8.2 and below, and as of
654 # PostgreSQL 8.3 it has a fixed precision of 19. PostgreSQLColumn.extract_precision
Did you run rake db:create on your local machine and on Heroku?
Try these steps:
bundle exec rake db:create
bundle exec rake dg:migrate
bundle exec rails s
For Heroku, just add heroku run before any command, like this: heroku run rake db:create, and heroku run rake db:migrate.
If you encounter the couldn't find that app error, that means that you passed an invalid app name as the -a, --app option for the heroku command. Valid app names are obtained with the heroku apps command.
I also see that you ran ... -app <app-name> ... instead of ... --app <app-name> ..., which is likely to cause errors. Try the following command replacement:
// before
heroku run rake db:create -app nguyen-andrew-a2
// after
heroku run rake db:create --app nguyen-andrew-a2

Setting up PostgresQL on Heroku Rails 4

its been a while since i have used postgresql and deployed an app to Heroku, think I have made an error somewhere with my setup.
I have created my app on Heroku, there is a Hobby Dev database setup (I ran Heroku run rake db:setup) which set up my database, but I'm wondering in my database.yml file do i have an error
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
production:
<<: *default
database: my_app_production
username: my_app
password: <%= ENV['MY_APP_DATABASE_PASSWORD'] %>
When I run heroku run rake db:create i get
FATAL: permission denied for database
DETAIL: User does not have CONNECT privilege.
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"databaseName", "username"=>"userName", "password"=>"password", "port"=>5432, "host"=>"hostNamehere"}
I have set the database password using heroku config:set
What have i missed here?
Thanks
Heroku ignores your database.yml configuration, they generate one when you deploy your application and they also take care of database creation.
All you need to do is heroku run rake db:migrate and maybe a heroku run rake db:seed in case you need to seed your database.

Adding a production database to a Rails 4 site on Heroku

Well I've deployed my site on Heroku. When I navigate to the app page it shows a database listed on the Add ons section
I navigate ahead to the database details and this is what I get to see
This is my production database code
production:
adapter: mysql2
encoding: utf8
database: dfu332icuvq6bs
pool: 5
username: cekapynykihldm
password: b1XuQq9m1m8Ok3zeyABEgNiiz3
socket: /tmp/mysql.sock
My question is when I fill in the details from Heroku into the production database and push it bask to the server, the app doesnt talk to the database as none of my routes except the home#index works. How do I make my app talk to the database on Heroku.
From your comment, it appears that you need to run the database migrations and then restart your instance:
heroku run rake db:migrate
heroku ps:restart
Guess I missed the most important step. I was able to fix the problem by running
heroku run rake db:migrate

Resources