How to view current database schema for Heroku app in Terminal? - ruby-on-rails

I am trying to view my Heroku app's schema in Terminal (Mac OS X Lion) and stumbled upon a command that does just that. In Terminal, I run heroku run more db/schema.rb but it seems to display an older schema version. I just migrated the Heroku db and I noticed that none of the new columns are listed.
I can't seem to find anything helpful in Heroku's documentation. Does anyone know a command to view the current database schema for a Heroku app?
By the way, I inherited the code for the app and for some reason all of the migration files are commented out (there are probably 40+ files) so I can't just run rake db:migrate locally to update the schema; hence, I'd like to see the Heroku app's schema directly.
Any suggestions?

You could run heroku pg:psql to fire up a Postgres console, then issue \d to see all tables, and \d tablename to see details for a particular table.

For a rails schema, try:
$ heroku run "bundle exec rake db:schema:dump && cat db/schema.rb"

You can use rateaux:
rake db:view:schema

Related

Local sql database and heroku postgres database files differ, how to sync again? Ruby on Rails

I am currently doing the Ruby on Rails tutorial by Michael Hartl. Somewhere along the way I messed up my database. In my database file there is only 1 user, by the name of Bob.
Locally in cloud9 IDE, when I do 'rails console' and then do Users.first, I get a user with a name of "Bob".
However when I do 'heroku rails run console' and do Users.first, I get a user with a different name. (I probably changed the name somewhere along the way)
How do I get Heroku to see the correct local database file again? Should I clear the heroku database, then use pg:pull to pull the local sql database to heroku?
Not sure if you've gotten to Chapter 9 yet but section 9.3.2 of the current book deals with creating sample users. This is done through the db/seeds.rb file.
Running $ bundle exec rake db:reset then resets your DB followed by $ bundle exec rake db:seed to fill it with your new data.
You can run the same procedure on your production application with:
heroku pg:reset DATABASE
heroku run rake db:migrate
heroku run rake db:seed
It is, of course, also possible to transfer data between local and production databases with tools such as heroku-pg-transfer but that's a little advanced if you're only starting out, and I think somewhat unnecessary if you only have one user to transfer over.
Hope this helps.
You can use yaml_db gem to dump your local data to file and then upload it to heroku.
On your development machine:
rake db:data:dump
Then commit changes, push to heroku and run:
heroku run rake db:data:load

Engine Yard rollback a migration

I deployed a new feature to Engine Yard that had migrations. Of course I passed the migration flag and it worked successfully. But then I decided to take the feature out.
Note: These migrations removed some columns
I then rolled back on github and deployed again, but now I'm getting a postgres error that a column doesn't exist (this is a column removed in the migration from before)
How do you rollback migrations on Engine Yard?
TMP,
While there is a rollback command built into the engineyard gem, it would be better to just deploy with a new migration that effectively adds the columns back in or update the code to not use the missing columns.
Evan
I've discovered that when you ssh into your engineyard app you can go to the current deploy's directory and run bundle exec rake ... thus you can run probably run bundle exec rake db:rollback

Migrate Data and schema from development to production rails heroku

I have facing problems with migrating data to my heroku app which has Postgresql as database for my hosted site(Production). At my development site i have rails 3.2.13 with Sqlite3 as database. I have followed Ruby on rails Tutorial by Michael Hartl
i have used git push heroku to update my site at heroku. i also want to update database along with data. But heroku run rake db:migrate migrates schema not data. I tried db:push to push data to heroku but i get error
dependency.rb in 'to_specs' :Could not find sequel (~) 3.20.0
also i have searched and found that i should first my sqlite data to dump.sql and then run
heroku pg:psql HEROKU_POSTGRESQL_COLOR --app app_name < file.sql as answered in https://stackoverflow.com/questions/15371394/...
but it failed with
the local psql command could not be located
please tell me what i am doing wrong. or what is the right way to update heroku postgresql with my development sqlite3 data.
Thanks in Advance
It is not a good idea to fill the production database with the data that you have now in the developement database. Because, if you have problems with your production database in the future, and you need to refill it again, your development db may changed (e.g dropped), and you are not going to be able to do it again.
For this need, Rails provides seeds in db/seeds.rb file. You should create all the neccessary objects there.
Then when you push your code to Heroku, Heroku is going to prepare the database, create the schema, and seed it. If you need to seed the db manually, you can run bundle exec rake db:seed, if you want to run it in Heroku: heroku run bundle exec rake db:migrate

How to update production database schema safely in rails 3.1.3?

We need constantly update our database schema in production for rails 3.1.3 app. The first db schema was created with the following rails command:
$rake RAILS_ENV=production db:schema:load
Question is: can we still use the command above to update db schema in production while safely retaining all current data?
Thanks so much.
I never used rake db:schema:load in production, but according to this answer to another question here on SO, I don't think you want to do that.
On the other hand, I have used RAILS_ENV=production rake db:migrate several times on the server with data already in the database and never experienced any problems.

How can I run a migration on my production database but not my development database?

I have a development database on my computer and a production database on Heroku. I need to run a migration on the production database, to clear certain data, that I don't want to run on the development one. So far I've only been doing migrations that I've wanted to run on both, so I just create it on my computer, run it, then when I upload to Heroku I run it on there too. How can I do a migration only on the production database? Thanks for reading.
Create your migration.
Commit, push, run on heroku with heroku rake db:migrate --app myapp.
Comment out the contents of the up block.
Run the (now-empty) migration locally.
Uncomment or git checkout/reset to get back to normal.
This way both your local db and production db will consider the migration to have been run and not try to run it again.
Migrations are intended to update the structure of your database, not to manipulate data. If you want to manipulate data, you should use the console or a script.
$ heroku console
RAILS_ENV=production rake db:migrate

Resources