How to Upload data to Heroku ? Rails 4 - ruby-on-rails

My Rails app is about an Online Shopping site.
I'm trying Deploy it to Heroku.
I've done push to heroku,and rake db:migrate.
But now in the My web,it doesn't have any Data.
In the localhost:3000,I can show Products,Phones,Users,etc.
But in my web:https://boiling-bayou-5793.herokuapp.com/welcome/home
It doesn't show anything.
So what should I do to upload data from my PC to my web

The data from your local development database is not automatically transferred to the production environment. There is no rake task to do that.
You can use seed files and the rake db:seed task to get some initial data in your (production) database.

A more heroku-specific method could be just loading a database dump.
Assuming you're using Postgresql, n your local computer do
pg_dump -h localhost -U username -Fc dbname > db.dump
upload the file somewhere on the internet (like Amazon S3 so you can get a direct download link) and then on heroku run
heroku pgbackups:restore SHARED_DATABASE http://www.example.com/db.dump
Remember to remove the db dump right away if it's any kind of sensitive data!

Related

Merging changes made to users in the Heroku production web app with my local development database

When I log onto the website (example.herokuapp.com) and create a user, the user is created in the Heroku Postgres::Database but when I go into Cloud9 and enter Rails Console, the user does not exist in my production database. How can I merge the changes made in production (Heroku) with development (local Rails) so that these users show up in Rails Console? Thanks.
If you need the users to show up in the console you can run
heroku run console --app APP_NAME
If you'd like to "merge" the database you have to export your heroku Postgres database and convert the table to SQL format.
For more information about exporting PG Backups: https://devcenter.heroku.com/articles/heroku-postgres-import-export
Another stackoverflow post that might help: How to I merge two databases with same schema that are on Heroku?

database file for rails app

6 months back I wrote a rails app (craigslist scraper) and all of the code was pushed to github and then deployed to heroku. Since this time I got a new laptop and am getting back into learning code. I am trying to get the same app running locally on my machine but am getting an error that no DB is connected (( Rails.application.database_configuration: Could not load database configuration. No such file )) how can I download the DB file from GitHub or get the DB going again and run a fresh scrape? thanks in advance!
You need to run.
rake db:create db:migrate
This will create the database from your database.yml file, then run the migrations.

Heroku remote database must be empty error after pg:reset

I have an app on Heroku and I'm trying to replace the database on Heroku with the database on my local machine.
I was under the impression that
heroku pg:reset DATABASE
would clear the remote database and allow me to push. However, after running pg:reset, I run
heroku pg:push
and I stil get this error:
Usage: heroku pg:push <LOCAL_SOURCE_DATABASE> <REMOTE_TARGET_DATABASE>
push from LOCAL_SOURCE_DATABASE to REMOTE_TARGET_DATABASE
REMOTE_TARGET_DATABASE must be empty.
Couple of questions:
1. do I have to specify local db and remote db? [I am in the root directory of my application on my local machine]
2. if so, how do I find what my local database name is? I know that heroku can divine the remote database via DATABASE_URL?
You have to specify BOTH the local database(where the data is coming from) and the remote database(where the data is going). The part about REMOTE_TARGET_DATABASE must be empty. is just a reminder to make sure you clear the Heroku database before you push.
You should be able to find your database from your database.yml file.
You can also find the heroku database by heroku pg:info
Reminder: Heroku runs PostgreSQL so you should be using PostgreSQL locally as well.

Syncing Heroku and localhost databases

Hey so I am following the One Month Rails guide to learning Ruby and I have hit a wall on one of the lessons. I have just finished uploading an image with Paperclip, and as I finished my work on my localhost and checked it on Heroku, something went wrong. The pin/image appears to have been pushed to my Heroku account, the only problem is that the username and password that works for my localhost:3000 won't work for my Heroku account. The same password should work for both, but for some reason something is wrong. I wish I could give you the action that is going on in my terminal, but the ruby rails is the only thing that has a continuous status flow. The problem may have been when I switched my password after not using my account on localhost for a few weeks, but i thought that once i "git pushed" that to heroku master, it would've synced. I have tried heroku run rake db:setup which didn't seem to do too much as well as wrestled by way through "Importing a Heroku Postgres Database with PG Backups", but I had some trouble working through that. Any ideas? Thanks for the help.
Your 'database.yml' should not be sent to Heroku, they take care of that, creating a new database.yml config file with the proper DB access details.
Try logging into your Heroku instance and deleting the file.
Edit: nevermind, assumed you were not able to connect to the DB, not to login into the website.
So if I'm understanding you correctly, you've deployed your application to Heroku and the login (to your application) that was working locally doesn't work on Heroku.
Deploying your application doesn't deploy data. Assuming you've run heroku run rake db:migrate then your database schemas will at least match.
At this point, you've got a couple of options.
Use a seeds.rb file which you can load with heroku run db:seed to setup some 'seed' data so that you can login.
Push your local database to Heroku - either via heroku db:push or using heroku pg:transfer provided by https://github.com/ddollar/heroku-pg-transfer
Use heroku run console to create your user account via the command line
User.create(email: 'someemail.com', password: 'somepassword', password_confirmation: 'somepassword')
I'd be inclined to go with the later option.
How did the user get in their in the first place? Perhaps going back to that step in the tutorial - just remember, if you are using rails console locally to use heroku run console on Heroku.

transfer db from one heroku app to another faster

Is there a faster way to transfer my production database to a test app?
Currently I'm doing a heroku db:pull to my local machine then heroku db:push --app testapp but this is becoming time consuming. I have some seed data but it is not nearly as accurate as simply testing with my real-world data. And since they're both stored on a neighboring AWS cloud, there must be a faster way to move the data?
I thought about using a heroku bundle, but I noticed the animate command is gone?
bundles:animate <bundle> # animate a bundle into a new app
It's quite common to migrate databases between staging, testing and production environments for Rails Apps. And heroku db:pull/push is painfully slow. The best way I have found so far is using Heroku PG Backups add-on and it's free. I followed following steps to migrate
production database to staging server:
1) Create the backup for the production-app db
heroku pg:backups capture --app production-app
This will generate b001 backup file from the main database (usually production db in database.yml)
2) To view all the backups (OPTIONAL)
heroku pg:backups --app production-app
3) Now use the pg:backups restore command to populate staging server database from the last backup file on production server
heroku pg:backups restore $(heroku pg:backups public-url --app production-app) DATABASE_URL --app staging-app
Remember that restore is a destructive operation, it will delete existing data before replacing it with the contents of the backup file.
So things are even easier now .. checkout the transfer command as part of pgbackups
heroku pgbackups:transfer HEROKU_POSTGRESQL_PINK sushi-staging::HEROKU_POSTGRESQL_OLIVE -a sushi
https://devcenter.heroku.com/articles/upgrading-heroku-postgres-databases#4b-alternative-transfer-data-between-applications
This has worked beautifully for me taking production code back to my staging site.
The correct answer has changed again as of March 11, 2015.
heroku pg:backups restore $(heroku pg:backups public-url --app myapp-production) DATABASE_URL --app myapp-staging
Note specifically that the argument is now public-url.
https://blog.heroku.com/archives/2015/3/11/pgbackups-levels-up
Update for mid-2015...
The pgbackups add-on has been deprecated. No more pgbackups:transfer. pg:copy is ideal for this scenario.
To copy a database from yourapp (example db name: HEROKU_POSTGRESQL_PINK_URL to yourapp_staging (example db name: HEROKU_POSTGRESQL_WHITE_URL)
# turn off the web dynos in staging
heroku maintenance:on -a yourapp-staging
# if you have non-web-dynos, do them too
heroku ps:scale worker=0 -a yourapp-staging
# backup the staging database if you are paranoid like me (optional)
heroku pg:backups capture -a yourapp-staging
# execute the copy to splat over the top of the staging database
heroku pg:copy yourapp::HEROKU_POSTGRESQL_PINK_URL HEROKU_POSTGRESQL_WHITE_URL -a yourapp-staging
Then when it's complete, turn staging back on:
# this is if you have workers, change '1' to whatever
heroku ps:scale worker=1 -a yourapp-staging
heroku maintenance:off -a yourapp-staging
Reminder: you can use heroku pg:info -a yourapp-staging (and yourapp) to get the database constants.
(source: https://devcenter.heroku.com/articles/upgrading-heroku-postgres-databases#upgrade-with-pg-copy-default)
psql -h test_host -c 'drop database test_db_name; create database test_db_name;'
pg_dump -h production_host production_db_name | psql -h test_host test_db_name`
This can be done on production_host or on test_host — will work both ways.
Have not tested this, but it might work.
Do this to get the URL of your source database:
heroku console "ENV['DATABASE_URL']" --app mysourceapp
Then try executing db:push with that.
heroku db:push database_url_from_before --app mytargetapp
This might not work if Heroku doesn't allow access to the DB machines from outside their network, which is probably the case. You could, perhaps, try using taps (gem that heroku db commands use internally) from within your app code somewhere (maybe a rake task). This would be even faster than the above approach because everything stays completely within AWS.
Edit:
Here's an (admittedly hacky) way to do what I described above:
Grab the database URL as in the first code snippet above. Then from a rake task (you could do it on console but you risk running into the 30 second timeout limit on console commands), execute a shell command to taps (couldn't easily determine whether it's possible to use taps directly from Ruby; all docs show use of the CLI):
`taps pull database_url_from_source_app #{ENV['DATABASE_URL']}`
The backticks are important; this is how Ruby denotes a shell command, which taps is. Hopefully the taps command is accessible from the app. This avoids the problem of accessing the database machine from outside Heroku, since you're running this command from within your app.
Heroku enables you to fork existing applications in production. Use heroku fork to copy an existing application, including add-ons, config vars, and Heroku Postgres data.
Follow the instructions on Heroku: https://devcenter.heroku.com/articles/fork-app
Update for mid-2016...
Heroku now have a --fast flag when creating forks, however they will be up to 30 hours out-of-date.
$ heroku addons:create heroku-postgresql:standard-4 --fork HEROKU_POSTGRESQL_CHARCOAL --fast --app sushi
https://devcenter.heroku.com/articles/heroku-postgres-fork#fork-fast-option

Resources