Heroku run rake db:migrate command doesn't update database - ruby-on-rails

I'm reading railstutorial.org Chapter 2 (about toy_app) using cloud IDE and I've run into a problem. I'm newbie so I would appreciate a simple sollution and explanation what I did wrong.
I followed every step as described in tutorial: I created my 'hello world' index page and I pushed it to heroku. I renamed my heroku site as well (it wasn't required in tutorial). Everything was fine at that moment.
I added user and micropost models. I've started a local server (on second terminal by typing rails server -b $IP -p $PORT) and added some users and microposts. Again, everything was working as expected.
I didn't close local server. I committed changes to git and I pushed it to Bitbucket and then to heroku. Finally, I typed heroku run rake db:migrate. It was the final step of this tutorial.
Everything seemed to be Ok, but I noticed that posts and users I added on local server weren't visible on my heroku site. Since I didn't close my local server, I checked it and posts I created were still available. As I said, situation on my heroku site was different.
I didn't get any errors during that process, only one warning after typing git push: Warning: Permanently added the RSA host key for IP address ... to the list of known hosts. Next time it didn't show up.
After my first try of every step I got promising outputs. After second and next tries it returned
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean`
and
Everything up-to-date
So still nothing worrying.
I tried to restart server, to follow those steps again after closing my local server but it didn't help. I searched for solution but only what I found were problems with massive errors or different problems.
Actually, It is problem I can go along with because I don't need it updated at this stage, but I would like to hear an answer and find out if there is something wrong with heroku or it it normal behavior or even if it is newbie's blindness.

First your local database and heroku database are different. Any user created on local app won't be available on heroku app unless you use a common database.
rake db:migrate, in its most basic form, just runs the change or up method for all the migrations that have not yet been run. If there are no such migrations, it exits. It will run these migrations in order based on the date of the migration.
Try creating user and post on heroku app, it should work all fine.

Related

how to relaunch rails app on heroku

My app runs properly locally and has a crash when it runs on Heroku. The app does fine until I run a new_post_path call. I tried git push to see if a change to the new.html.erb file or the _form.html.erb file was the issue - but I think during the last push to heroku - my database may have taken a hit. At the time I was adding a t.text field (longtext) and it seems to be the issue.
So I wanted to roll back or totally re-start at heroku. But I have not found anyway to do it. Since this is not near production yet. Is there anyway to just create a new instance at heroku and push up to it?
Again, local version runs perfect. Thanks in advance for any ideas.
I would say there are several ways to handle this problem
restarting heroku
Run $ heroku restart in the rails dir. Sometimes I believe in magic.
debugging
Use $ heroku log to debug what is causing the error.
migrate database
Perhaps you made a database change in local rails, while heroku rails didn't.
Run heroku run rake db:migrate
drop database and remigrate database
See this thread. Sometimes old migration might be in conflict with current migration. Do a clean migration, if the data in your database isn't important.
recreate heroku instance
run $ heroku apps:destroy –app appname and replace appname with your appname. Then delete the current heroku remote repository location from git. git remote rm heroku. Then recreate heroku. run $ heroku create. go through pushing your code and migrating database like usual.
Relaunching might be the least of your problems, so I suggest a few things:
Review this Heroku article on releases, including the Heroku rollback feature.
Add the PG Backups add-on to your app.
I also recommend using something like the exception_notification gem in combination with the SendGrid add-on, so that you can get an email with details on the error as well.

Why do I get "We're sorry, but something went wrong." after restoring via pgbackups:restore with no migrations

This one took me a while to figure out. It's pretty tricky and seems like a bug in Heroku, so I will post my own answer in case it helps someone.
Scenario:
Rails application using postgresql adapter works fine on localhost.
All migrations are complete.
Deploying to Heroku via "git push heroku master" successfully updates Heroku instance.
Heroku PGBackups addon is installed and works fine.
pg_dump was used to generate a local db dump, and the file uploaded to an accessible internet location.
Problem:
After running
heroku pgbackups:restore DATABASE_URL 'http://mywebsite.com/pgbackup.dump'
I get the "something went wrong" message. Strange, considering all that happened was a database load, a database whose data works fine on the local machine. No migrations were performed, everything has been committed previously, no code updates... Only data in the database was changed, so why does the app no longer work if just data in the database changed?
Checking the heroku logs shows that it can't find tables that are clearly there. Lines like:
ActionView::Template::Error (PG::UndefinedTable: ERROR: relation "users" does not exist
Verifying with heroku pg:psql just makes the situation even stranger, because doing select * from users brings back the real results. Even performing a heroku restart at this stage won't get rid of the problem. I also tried combinations of that with heroku pg:reset and heroku run rake db:migrate.
Why does updating data in the database shut down the app?
Solution:
Run the pgbackups:restore as originally intended (and get error).
Run heroku restart.
Run heroku pg:psql (profit).
For some reason, it seems necessary to restart and then pg:psql before the database works after a pg:restore.
Even though pg:psql will gladly show that the tables are indeed there (contrary to the log messages), the app won't work until the dyno is restarted (and then it STILL won't work), not until you pg:psql.
As far as I know, pg:psql only establishes a connection to the remote database, and should not have any consequence on the state of the database server (though it seems it does).

Rails 3.2.6: Deleting all data and public files from production server

I have successfully managed to set up a VPS production server (Ubuntu 10.04 LTS) from the excellent Railscasts episode. It's an "internal" server (not live yet), so I've been building my app locally and doing a cap deploy at regular intervals to check that things are running smoothly.
However, what I'd like to do now is delete all records on the production server (as I've just been testing stuff) - that is, to start with a completely empty database for when the site actually goes live to the public.
Obviously, I can do this locally by running something like rake db:reset, but how do I do this on the production server? Should I be adding some code to my deploy.rb file?
I'm a bit of a noob at this, but I've been unable to find anything via a Google search.
** EDIT ** Oh, and obviously this is a one-off - once things go live, I'll remove any code which deletes records!
You can ssh into the server and run any rake command from the application directory. You could create a Capistrano task just to run this one rake task, but since this task is obscenely dangerous with any real system I would not recommend it. The last thing you would want is to accidentally run it.

Rails/Heroku : db:push runs correctly but does not create the DB

I've been having problems yesterday with Heroku shared Postgres DB. It looks like my db is not correctly 'pushed' to my heroku DB.
Configuration
Using 'pg' gem for both development and production environments.
Using Heroku's 'shared-database' add-on.
Running Rails 3.1.1.
What I'm trying to do
Push my source code to Heroku and then synchronize my postgres DB with Heroku's one. No problem for the source itself, but I can't get my DB up and running.
What I've tried so far
1: Source code push : git push heroku master. OK. This works.
2: Then I try to run : heroku db:push. Seems to be running normally, no error, the schema is sent to Heroku, the different tables are correctly detected and seem to be sent as well :
3: Let's check it out, is everything allright ? heroku info :
Doh ! The database is still empty (and therefore I get DB-related errors when accessing dynamic pages). Can't understand what's happening, I've spent time on this issue yesterday evening but only been mpessing with Heroku for a little while and never encountered this issue. Any clues ?
I'm not convinced that the Database size figure is a live figure. Here's a clean deployment of an app to Heroku and the output of heroku info at each stage. The first run is post application creation, the second after a push of the code, the third after a heroku db:push.
The database was uploaded before the third output and the application is running however the DB:size is not reporting the figure.
Is your application actually erroring because the database isn't present?
The problem is in the commit process.
You should run manually after each commit from your console :
heroku run rake db:migrate
Enjoy
daniel

Heroku: Deploying rails application troubles

I'm trying to deploy my rails application with heroku (as shown here). I've created a very simple rails application (using ruby 1.9.2 and rails 3.0.3; i'm sure heroku supports these - see heroku docs), created and pushed github repo, created heroku repo and pushed it (all commiting is done). And when i'm trying to access my application controller, it throws 404 rails page like it's saying 'there is no such controller'. I've done heroku rake db:migrate but first time i ran it i got 'host not found' error. Running this again fixed that. Well, i'm not sure if i should run heroku addons add:postgresql - i though postgres is on by default, but heroku says i should pay in order to get DB (running command i've mentioned asks me to confirm billing it).
May be it sounds stupid, but how can i deploy my rails application (it's a very simple one) without paying any fees and such troubles as 404 pages like i mentioned in the beginning of my post? (and this is my question). Maybe i should choose other hosting (if it exists in our world) or am i doing something wrong with heroku?
You forgot to push your quotes_controller.rb to git and heroku probably.
git add controllers/quotes_controller.rb
it seems you forgot models also, and probably lot of files.

Resources