Search code examples
ruby-on-railspostgresqlactiverecordherokuheroku-postgres

postgres production database isn't synced with heroku app


I first noticed trouble after I dropped an Active Record table using this migration:

class DropDelayedJobTables < ActiveRecord::Migration
  def change
    drop_table :delayed_jobs
  end
end

bundle exec rake db:migrate seemed to successfully drop the table; it disappeared from schema following rake db:schema:dump; and the records no longer seemed to exist via the heroku console. But, the table still exists in my postgres production database!

$ psql myapp_production
myapp_production=# \dt
 Schema |       Name        | Type  |   Owner   
--------+-------------------+-------+-----------
 public | delayed_jobs      | table | leoebrown
 public | foods             | table | leoebrown
 public | lists             | table | leoebrown
 public | quantities        | table | leoebrown
 public | schema_migrations | table | leoebrown
 public | users             | table | leoebrown

I also noticed that when I delete records via heroku console (Food.find(1234).destroy), the records still exist in the myapp_production postgres database. For example, when I check the number of foods in the postgres database, it is:

myapp_production=# SELECT COUNT(*) from FOODS;
 count 
-------
  6716
(1 row)

But when I run Food.count in the heroku console, the result is In general, my postgres database seems disconnected from my live heroku app.

$heroku run console
irb(main):001:0> Food.count
=> 6161

When I check the number of foods in the live website, it is 6161.

Clearly, I am doing something fundamentally wrong. The production database seems totally disconnected from the app. I didn't even notice, because it wasn't affecting performance. It's almost as though it doesn't matter what is in the production database. Though surely it does matter.

Below is my database.yml file in case it provides a clue. For full disclosure, I'm not sure if/where some of these ENV variables are stored or, if they do exist, where to find them, and what they should be set to.

Thank you for any guidance you may provide.

database.yml:

development:

  host: localhost
  adapter: postgresql
  encoding: UTF8
  pool: 5
  username: <%= ENV['USERNAME'] %>
  password: <%= ENV['PASSWORD'] %>
  database: myapp_development

test:
  host: localhost
  adapter: postgresql
  encoding: UTF8
  pool: 5
  username: <%= ENV['USERNAME'] %>
  password: <%= ENV['PASSWORD'] %>
  database: myapp_test

production:
  host: <%= ENV['IP'] %>
  adapter: postgresql
  encoding: UTF8
  pool: 5
  username: <%= ENV['USERNAME'] %>
  password: <%= ENV['PASSWORD'] %>
  database: myapp_production

Solution

  • Well from your post, I think you are saying that your local database named my_app_production is not synced with your heroku database. which will never happen.

    You should note that to login to psql session of heroku you need to use following command

    heroku pg:psql --app <your app_name here>
    

    psql myapp_production lets you open psql session with your local database. It is not your heroku production database.

    More on this https://devcenter.heroku.com/articles/heroku-postgresql