Skip to content

Instantly share code, notes, and snippets.

@MatthewRDodds
Created January 18, 2016 22:47
Show Gist options
  • Save MatthewRDodds/6f11273f18fededb8416 to your computer and use it in GitHub Desktop.
Save MatthewRDodds/6f11273f18fededb8416 to your computer and use it in GitHub Desktop.
Refresh Heroku Database (Rails 4)
heroku pg:reset DATABASE --confirm app_name
heroku run rake db:gis:setup
heroku run rake db:migrate
heroku run rake db:seed
@MatthewRDodds
Copy link
Author

To add postgis buildpack:

heroku buildpacks:set heroku/ruby
heroku buildpacks:add --index 1 https://github.com/cyberdelia/heroku-geo-buildpack.git#1.3

@MatthewRDodds
Copy link
Author

To test:

heroku run rails runner 'puts RGeo::Geos.supported?'

@MatthewRDodds
Copy link
Author

Add to application.rb

initializer "active_record.initialize_database.override" do |app|

      ActiveSupport.on_load(:active_record) do
        if url = ENV['DATABASE_URL']
          ActiveRecord::Base.connection_pool.disconnect!
          parsed_url = URI.parse(url)
          config =  {
            adapter:             'postgis',
            host:                parsed_url.host,
            encoding:            'unicode',
            database:            parsed_url.path.split("/")[-1],
            port:                parsed_url.port,
            username:            parsed_url.user,
            password:            parsed_url.password
          }
          establish_connection(config)
        end
      end
    end

Replace in puma config file:

on_worker_boot do
  config = ActiveRecord::Base.configurations[Rails.env] ||
    Rails.application.config.database_configuration[Rails.env]
  config['adapter'] = 'postgis'

  ActiveRecord::Base.establish_connection
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment