Search code examples
ruby-on-railspostgresqlherokurails-postgresql

Push new table to heroku without having to reset existing database


I'm at the early stages of learning RoR and working with Heroku. So i have a fledging app with local and production databases. I've used pg:push to create the initial production database but I appreciate this requires you to reset the db every time. I now have a new table in my local db that I wish to push to production, but I don't want to reset and lose the data.

It seems I could create a back up from production and then reload that. Or could if run pg:pull to get the data into my local db, and then run a pg:push so that when I add the new table I'm also reloading my existing data.

All of this seems a little long winded. From some knowledge of php and mysql, I would have simply ran an SQL query to add the new table, is there not a similar way?


Solution

  • I think what you are searching for is migrations:

    class CreateProducts < ActiveRecord::Migration[5.0]
      def change
        create_table :products do |t|
          t.string :name
          t.text :description
    
          t.timestamps
        end
      end
    end
    

    You can create migrations on your local development environment and then run them on the production server once you are satisfied. You can read about migrations more here:

    Active Record Migrations