40

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?

vich
  • 11,836
  • 13
  • 49
  • 66

3 Answers3

79

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.

Chowlett
  • 45,935
  • 20
  • 116
  • 150
33

For a rails schema, try:

$ heroku run "bundle exec rake db:schema:dump && cat db/schema.rb"
kch
  • 77,385
  • 46
  • 136
  • 148
  • The [Heroku docs](https://devcenter.heroku.com/articles/rake) state that "Rake tasks that write to disk, such as rake db:schema:dump, are not compatible with Heroku’s ephemeral filesystem." Won't this command fail? – vich Apr 11 '13 at 17:28
  • 2
    It works. You can rely on the file existing for the while the `run` dyno is up, which is inherently long enough for it to be `cat`'d. – kch Apr 15 '13 at 12:52
  • 1
    Best answer: It copies the full Heroku schema to your terminal – user3763682 Dec 11 '16 at 16:39
  • This is it. Thanks so much for answering this. I realized that my schema on heroku isn't matching the most updated version of my schema. I took two steps to make them match. I double checked it by running `heroku run bundle exec rails db:migrate:status -a application-name` which showed me that my most recent migration was down. Then, I ran `heroku run bundle exec rails db:migrate -a application-name` to run that migration! Hope this helps someone in the future. – boyd Jan 03 '20 at 20:48
  • To whoever like me thought this could affect the app, it doesn't bother production files. You'll need to collect the result of the cat dump and that'll be your schema.rb – Carlos J García May 16 '22 at 17:11
1

You can use rateaux:

rake db:view:schema
skywinder
  • 21,291
  • 15
  • 93
  • 123
kartouch
  • 11
  • 1